pow function in C

hamb picture hamb · May 27, 2012 · Viewed 59.8k times · Source

I write a C code that have power function that is from math.h library. when I compiled my program, I received an error which is " undefined reference to 'pow' function ", I compile my program using gcc compiler (fedora 9).

I insert -lm flag to gcc then, the error is omitted but the output of the pow function is 0.

#include<math.h>
main()
{
double a = 4, b = 2;
b = pow(b,a);
}

Can anyone help me? Is there is a problem in my compiler??

Thanks.

Answer

goFrendiAsgard picture goFrendiAsgard · May 27, 2012

For everyone else who seek such an answer:

This will not work:

gcc my_program.c -o my_program

It will produce something like this:

/tmp/cc8li91s.o: In function `main':
my_program.c:(.text+0x2d): undefined reference to `pow'
collect2: ld returned 1 exit status

This will work:

gcc my_program.c -o my_program -lm