This forum has been archived. All content is frozen. Please use KDE Discuss instead.

error "undefined reference"

Tags: None
(comma "," separated)
fido_dido
Registered Member
Posts
5
Karma
0

error "undefined reference"

Fri Jun 30, 2017 7:48 pm
Hi there,

I am trying out an older German book called "C und Linux" by Martin Gräfe (latest edition dates back to 2010) and I am stuck when kdevelop is mentioned for the first time.

My application is divided in three files which should run in the console:

main.cpp

Code: Select all
#include <iostream>
#include <stdio.h>
#include "primzahl.h"

int main(int argc, char **argv) {
    int zahl;
   
    for (zahl=1; zahl<=100; zahl++)
        if (ist_primzahl(zahl))
            printf("%d\n", zahl);
    return(0);
}


primzahl.h
Code: Select all
int ist_primzahl(int zahl);


and primzahl.c

Code: Select all
#include <math.h>

int ist_primzahl (int zahl)
{
    int teiler=2;

    while (teiler <= sqrt(zahl))
    {
        if (zahl % teiler ==0)
            return(0); /* 'zahl' ist keine Primzahl */
        teiler++;
    }
    return(1); /* 'zahl' ist eine Primzahl */
}


While compiling main.cpp I am stuck with the error message undefined reference to `ist_primzahl(int)'


It looks as if I had to set a linker flag LDFLAGS -lm in order to include the other files while compiling?

I don't understand where in kdevelop this has to be done?

I am using kdevelop 5 with OpenSuSe Leap 42.2.

--

Thanks for your help,
Felix

Last edited by fido_dido on Sat Jul 01, 2017 1:52 am, edited 1 time in total.
User avatar
scummos
Global Moderator
Posts
1175
Karma
7
OS

Re: error "undefined reference"

Fri Jun 30, 2017 8:54 pm
Which build system do you use? You don't compile one of your .cpp files into the executable.


I'm working on the KDevelop IDE.
fido_dido
Registered Member
Posts
5
Karma
0

Re: error "undefined reference"

Sat Jul 01, 2017 1:56 am
scummos wrote:Which build system do you use? You don't compile one of your .cpp files into the executable.


Is that correct: C++ IDE with CMAKE or rather Standard-Terminal? (Sorry, I am not familiar with the term "build system"? How can I find out about the translation for the German version?)

Where can I set this linker flag LDFLAGS -lm in KDEVELOP5?
fido_dido
Registered Member
Posts
5
Karma
0

Re: error "undefined reference"

Sat Jul 01, 2017 2:55 am
Other question: I have just tried to install KDEVELOP 3. But this version dates back to 2007 and does not seem to have this "Configure Options" dialogue yet that I am desperately looking for?

I also tried KDEVELOP 4.7. But same problem: no "Configure Options" dialogue and it looks similar to current KDEVELOP 5.

So which version of KDEVELOP would you recommend for a book from the year 2010? Can my problem still be solved with KDEVELOP 5? KDEVELOP 4 in a version earlier than 4.7 does not seem to be available with my OpenSuSE repositories?
User avatar
scummos
Global Moderator
Posts
1175
Karma
7
OS

Re: error "undefined reference"

Sat Jul 01, 2017 11:04 am
Don't use KDevelop 3. KDevelop 4 might be an option for some cases but don't use it either. KDevelop 5 is just fine for your use case, you just have to understand how building works.

Building is handled by a tool called cmake, and that reads a file called CMakeLists.txt in your project directory. If you add a new cpp file, you have to add it to the list of files to be compiled there. You can also specify which libraries to link in that file, using target_link_libraries(), and ideally find_package() beforehand.


I'm working on the KDevelop IDE.
fido_dido
Registered Member
Posts
5
Karma
0

Re: error "undefined reference"

Sat Jul 01, 2017 12:11 pm
scummos wrote:using target_link_libraries(), and ideally find_package() beforehand


Unfortunately I did not get along with this. I just emailed the author of the book. But he is a professor now. So I think he won't have the time to support me...

Eventually I will try Eclipse as an alternative and then move on with the book which still seems interesting to me, because it also covers hardware programming issues.
User avatar
scummos
Global Moderator
Posts
1175
Karma
7
OS

Re: error "undefined reference"

Sat Jul 01, 2017 12:34 pm
Maybe you should just read a cmake tutorial instead of emailing book authors ;)
But you really don't even need a tutorial for that -- just

add_executable(main main.cpp foo.cpp bar.cpp)
target_link_libraries(main lib1 lib2)


I'm working on the KDevelop IDE.
fido_dido
Registered Member
Posts
5
Karma
0

Re: error "undefined reference"

Sat Jul 01, 2017 1:19 pm
Yep, finally got it working with Eclipse! Let's see what will turn out further on in the book...


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], Sogou [Bot]