Question

Possible Duplicate:
pow() isn’t defined

void octal2decimal(char *octal, char *decimal) {
int oct = atoi(octal);
int dec = 0;
int p = 0;

while (oct!=0) {

    dec = dec + (oct%10) * pow(8,p++); //since oct is base 8 
    oct/=10;
}
*decimal = (char)dec;
*/
}

For the following code above I get this error I dont know why I include math.h but still i get this errors

/tmp/cck07bLe.o: In function 'octal2decimal':
data-converter.c:(.text+0x35f): undefined reference to 'pow'
collect2: ld returned 1 exit status

What does it mean? and how can I fix it?

Was it helpful?

Solution

pow is defined in the math library (libm), which isn't linked in by default. Thus, you should add -lm to your compilation command to get the math library. For example, if you are using gcc:

gcc data-converter.c -o data-converter -lm

By the way, pow is not the right way to compute an integral power of two. You should instead use a shift:

dec += (oct%10) << (3*p++);
oct /= 10;

Shifting left by 3*p is equivalent to multiplying by 8p, but avoids floating point numbers.

OTHER TIPS

Math library is not part of the standard C library which is included by default. Link with the math library:

gcc file.c -o file -lm
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top