1

Possible Duplicate:
Problem using pow() in C
Undefined reference to pow' andfloor'

I am using gcc 4.6.1.

Code snippet:

int main(void)
{

 int x= 2;
 int y = pow(3,x);
 printf("%d\n",y);

 return 0;
}

Terminal:

avinash@titanic:~/Desktop/DSF$ gcc power.c -o power
/tmp/ccTJ7vAH.o: In function `main':

power.c:(.text+0x25): undefined reference to `pow'
collect2: ld returned 1 exit status

If I replace x by 2 then it gets executed in an expected way. Doesn't pow function accepts variables as it's parameters?

Note: I have included stdio.h and math.h in the source file.

Community
  • 1
  • 1
Avinash Sonawane
  • 161
  • 1
  • 2
  • 5
  • If you have pow(3,2) the compiler can compute that directly and doesn't have to call the function. – Bo Persson Jan 26 '12 at 20:45
  • If you included stdio.h and math.h in the source file, then show us that in the code rather than showing us incomplete code and then telling us what's missing. – Keith Thompson Jan 26 '12 at 20:54
  • @KeithThompson- Sorry. But "include" term was appearing to be Bold without actually going for the bold type. So I dropped the #include lines, but i have mentioned that I have included both the libraries under note. – Avinash Sonawane Jan 26 '12 at 21:00

2 Answers2

8

You need to add the -lm command-line switch to link in the math library (-l... means "link in the __ library"; m is the math library):

gcc power.c -lm -o power
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 2
    Or rather `gcc power.c -lm -o power`. The lib has to come after the source file. – James M Jan 26 '12 at 20:44
  • This is not the proper order of the arguments. See http://stackoverflow.com/questions/45135/linker-order-gcc – Daniel Kamil Kozar Jan 26 '12 at 20:44
  • @ruakh- No change. Got the same error message. – Avinash Sonawane Jan 26 '12 at 20:45
  • @JamesMcLaughlin- It worked! But may I know why I need to add that swith (-lm)? 2) If it's all about linking the library then why it get's executed when I replace x by any constant no.? – Avinash Sonawane Jan 26 '12 at 20:48
  • @JamesMcLaughlin: Both ways work for me, but O.K.: since you, Daniel, and the OP's outcome all agree, I've updated the answer accordingly. Thanks for the -- correction, I guess? – ruakh Jan 26 '12 at 20:53
  • 1
    @AvinashSonawane: That's because GCC will "optimize away" a call to `pow(3, 2)`, replacing it with its result, so it never gets to the linker. – ruakh Jan 26 '12 at 20:54
  • Why I don't have to add that switch (-ls for example) for stdio.h? Why is it so in case of math.h? Do I have to add the switches for all the header files excluding stdio.h while linking the program? – Avinash Sonawane Jan 26 '12 at 21:11
  • @AvinashSonawane: GCC does link most commonly-used libraries by default; the math library is the most common library that's *not* linked by default. So you don't usually have to worry about it. – ruakh Jan 26 '12 at 21:41
1

The pow function is part of the math library. Therefore, the invokation of your compiler should be as follows :

gcc power.c -o power -lm

Where -lm is the switch instructing the linker to perform linking with the math library on your system.

Daniel Kamil Kozar
  • 18,476
  • 5
  • 50
  • 64
  • @Daniel-It worked! But may I know why I need to add that swith (-lm)? 2) If it's all about linking the library then why it get's executed when I replace x by any constant no.? 3) Why I don't have to add this swith in case of linking other libraries like stdio.h? – Avinash Sonawane Jan 26 '12 at 20:50
  • `gcc` always implicitly links the source with the standard C library. Obviously, you're programming in C, so you're most probably going to use the library anyway, so adding an extra -lc to your command line every time would be weird. If you replace the 'x' by a constant, the compiler - knowing what the pow function does - turns that into a constant and avoids actually calling the function at runtime. – Daniel Kamil Kozar Jan 26 '12 at 20:53
  • @Daniel- So did u mean math.h is not a part of standard C library as stdio.h? – Avinash Sonawane Jan 26 '12 at 21:06