Take the 2-minute tour ×

This simple program below does not build for some reason. It says "undefined reference to pow" but the math module is included and I'm building it with -lm flag. It builds if I use the pow like pow(2.0, 4.0), so I suspect there is something wrong with my type casting.

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

int main() {
   int i;

      printf("2 to the power of %d = %f\n", i, pow(2.0, (double)i));

    return EXIT_SUCCESS;

Here is the bulid log:

make all 
Building file: ../src/hello.c
Invoking: GCC C Compiler
gcc -O0 -g -pedantic -Wall -c -lm -ansi -MMD -MP -MF"src/hello.d" -MT"src/hello.d" -o "src/hello.o" "../src/hello.c"
Finished building: ../src/hello.c

Building target: hello
Invoking: GCC C Linker
gcc  -o "hello"  ./src/hello.o   
./src/hello.o: In function `main':
/home/my/workspace/hello/Debug/../src/hello.c:19: undefined reference to `pow'
collect2: ld returned 1 exit status
make: *** [hello] Error 1

**** Build Finished ****
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You've told it to use the math library in the wrong place -- you're specifying the math library when you compile (where it won't help) but leaving it out when you link (where it's actually needed). You need to specify it when you link:

gcc -o "hello" ./src/hello.o -lm
share|improve this answer
What Jerry's saying is that you're not passing it in the linking phase. See the line just after Invoking: GCC C Linker. Maybe can you post your Makefile? –  K.G. Mar 20 '12 at 14:52
Thanks K.G. I'm using Eclipse so instead of adding the -lm parameter to the GCC C Linker settings, I've added to GCC C Compiler settings. Passing it in the linking phase solved the problem. –  pocoa Mar 20 '12 at 14:56
add comment

Your Answer


