Must use -lm in Clang with pow () but not sqrt ()

On FreeBSD 10.1 and using Clang version 3.4.1

Now I saw other topics asking how to compile with pow () , but I did not see a thread with this question. Take for example:

 #include <math.h> #include <stdio.h> int main() { float result; result = pow(2,3); printf("%d", result); return 0; } 

Now, using pow() , I need to give clang the -lm argument.

cc -lm -o name name.c

However, replacing pow(2,3) with sqrt(5); I can compile with cc -o name name.c If they both use math.h , then why do pow() need to be linked to the library? Not side: gcc is installed for testing, and I don't need to use -lm at all.

+4
source share
1 answer

You are targeting a processor whose floating point hardware implements sqrt directly, like all IEEE-754 compatible devices. Thus, the compiler can directly generate code for sqrt and does not require a reference to the library function.

On the other hand, floating point hardware usually does not offer pow implementations, and therefore it should be implemented in a library.

+5
source

All Articles