The Interesting GCC + Clib

symptom: you will have a negative number or zero if you are using the pow function in C lib without explicit declaring it to be a function taking double type. You can’t compile the code if you include math.h – you don’t include the header, but use -lm to ask gcc to use the linked library. So here is what happened:

# cat b.c
#include <math.h>
main(){
printf("2 to the power of -0.5 is %f\n", pow(2,-0.5));
}
# gcc -o b b.c
/tmp/ccCSfD5B.o(.text+0x24): In function `main':
: undefined reference to `pow'
collect2: ld returned 1 exit status
# cat c.c
main(){
printf("2 to the power of -0.5 is %f\n", pow(2,-0.5));
}
# gcc -o c -lm c.c
# c
2 to the power of -0.5 is -0.500000
# gcc -v
gcc version 3.3.5 (Debian 1:3.3.5-13)
#

Advertisements