PDA

View Full Version : undefined reference to `pow '



bunny1
February 28th, 2012, 11:42 AM
Can anyone please tell me why the following code gives errors ? Iv tried changing the code but got nowhere. Im on Xubuntu with GCC.

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

int main()
{
int i=2;

printf( "%f\n", pow(i,2) );/* This fails if 2 is any higher ? */
return 0;
}




The error is..........

In function `main':
test1.c: (.text+0x23): undefined reference to `pow'
collect2: ld returned 1 exit status

Hetepeperfan
February 28th, 2012, 11:57 AM
Did you link to the math library?

compile as:


gcc test.c -o your_program_name -lmthis should help. the -lm switch tell gcc it should link to the math library this is where the function is defined. without the -lm switch gcc doesn't know how to put in the pow() function in you program.

kind regards,

Maarten

bunny1
February 28th, 2012, 01:12 PM
Did you link to the math library?

compile as:


gcc test.c -o your_program_name -lmthis should help. the -lm switch tell gcc it should link to the math library this is where the function is defined. without the -lm switch gcc doesn't know how to put in the pow() function in you program.

kind regards,

Maarten

Thank you so much. That work like a dream.

cgroza
February 28th, 2012, 10:32 PM
Thank you so much. That work like a dream.
Then mark the thread as [SOLVED]. :D