-
math.h
i'm trying to use the log() function, and i've included math.h, but i still get this error on compilation:
Code:
util.c:15: undefined reference to `log'
someone please inform me of what i'm doing wrong so that i can bang my head against the wall for being stupid.
-
you need to compile against the math library. in gcc add -lm to the end of your linker call.
-
Please post the code of util.c so we can see your problem. Why would you need to add -lm to the end of your linker call? For example this works fine with gcc without needing any lm switch.
Code:
#include <math.h>
#include <stdio.h>
int main(void)
{
double x = log(10);
printf("%f\n", x);
return 0;
}
-
I've noticed that in my current version of gcc (3.3.5-20050130), you don't need to specify -lm to link against the math libraries, but in older versions, I believe that it is required. Thus, if you plan to distribute the source and a makefile, it's a good idea to include -lm just for backwards compatibility.
Edit: I just tested on my university's servers, and with gcc 3.3.4 (and likely earlier), you need to explicitly link to the math library.