PDA

View Full Version : Problem compiling C


fjleal
2004-03-04, 03:58 AM CST
Greetings!

I'm doing a simple C exercise on FC1 like this one:

#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
double a=log(100.0);
printf("%f\n", a);
exit(0);
}

when I invoke gcc ("gcc -o test test.c") I get the following error:

"
/tmp/cckCz1r1.o(.text+0x20): In function `main':
: undefined reference to `log'
collect2: ld returned 1 exit status
"

The "log()" function is defined in <math.h>. If I do a "gcc -o test test.c -E" (just to invoke the preprocessor, not the compiler nor the linker) I can see the code for the log() function (the preprocessor seems to do its job well).

So what may be the problem? Is it something related to FC1?

Thanks a lot. ;)

Bana
2004-03-04, 06:02 PM CST
This is weird, it does it on mine too and all math.h functions seem to be broken (at least I tested log() and sqrt()). I wonder what this is.

rocian
2004-03-05, 08:00 AM CST
You need to link to the math library in order
to use math functions:
gcc -o test -lm test.c
bye

fjleal
2004-03-05, 08:46 AM CST
Originally posted by rocian
You need to link to the math library in order
to use math functions:
gcc -o test -lm test.c
bye
Ok, it works! Thanks a lot rocian! ;)

phoxis
2008-02-04, 08:32 AM CST
how to do it with fedora eclipse?