tags:

views:

148

answers:

1

I am having issues with usage of log10f(). I am compiling the program on Linux (2.6.28-11-generic) and using gcc (3.4.6).

The following source compiles and prints 1.000000 on execution.

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

int main() {
   printf("%f\n", log10f(10));
   return 0;
}

while the below one doesn't and throws up link error:

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

int main() {
   printf("%f\n", log10f(100));
   return 0;
}

Error : Undefined reference to log10f

  1. Is the log10f() not defined as part of standard math library (Man pages indicate that it is part of math library)?

  2. Why is that the second example doesn't compile?

+4  A: 

That's because the required libm.a library is not linked into the executable automatically.

You have to add the -lm parameter to gcc. Then the linker will also link libm.a into your executable.

DR
DR, Thanks it works. For the first example I did not use -lm option, still compiled, this is puzzling to me.
pgm
That sounds like the linking step was not executed the first time.
DR