Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gcc do not find the library

42 views
Skip to first unread message

Sigmund Skjelnes

unread,
Aug 12, 2012, 10:15:11 AM8/12/12
to
Tried to compile this little program:
/* sqrt-number.c */

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main( int argc, char **argv ){

double f = 9.0;
printf("Kvadratroten av %f er %f\n", f, sqrt(f) );
}

Got this result:
sigmund@sigmund-desktop:~/csrc$ gcc -lm -o sqrt-number sqrt-number.c
/tmp/ccb750CF.o: In function `main':
sqrt-number.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status

Thought maybe libm is missing, it shold be in the package ( for Ubuntu 12.04 ) libc6, and that package is installed.

Tried to find some file which has the name libm.* and got /usr/lib/i386-linux-gnu/libm.so Then I'd tried to set the path to where gcc finds the libraries:

gcc -L/usr/lib/i386-linux-gnu -lm -o sqrt-number sqrt-number.c
again, undefined reference to 'sqrt'

Read somewhere that libraries that neeeded to be linked to the program need only to be listed in the arguments to gcc:
sigmund@sigmund-desktop:~/csrc$ gcc -o sqrt-number /usr/lib/i386-linux-gnu/libm.so sqrt-number.c
/tmp/ccLOlOSu.o: In function `main':
sqrt-number.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status

Obviously gcc cannot find the library, what am I doing wrong here?

Cincerely, Sigmund

FÖLDY Lajos

unread,
Aug 13, 2012, 2:47:55 PM8/13/12
to
Move -lm to the end. And Google gcc libraries link order.

regards,
Lajos

Sigmund Skjelnes

unread,
Aug 14, 2012, 6:22:38 PM8/14/12
to
-lm as the last argument to gcc did it. Thanks.
0 new messages