Reputation: 254
given the following program:
#include <math.h>
#include <stdio.h>
int
main(void)
{
double x = sqrt(2);
printf("The square root of two is %f\n", x);
return 0;
}
and compiling with:
gcc calc.c -o calc
succeeds? why doesn't it require -lm or /usr/lib/blah/libm.so.x
inspecting the binary object with ldd produces:
linux-vdso.so.1 (0x00007fff4f5e5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007feeffd1b000)
/lib64/ld-linux-x86-64.so.2 (0x00007fef000e1000
No libm is referenced. However, if I look at the libc.so.6 library or the ld-linux-x86-64.so.2 library using nm -D, no sqrt function is in those libraries.
What's going on here? Is gcc magically including a default set of common functions or something else?
Upvotes: 2
Views: 529
Reputation: 7691
No, the gcc compiler knows that sqrt(2) is a constant value and just calculates the value at compile time.
To trigger a use of the sqrt()
library function, use code like this:
volatile double y = 2;
double x = sqrt(y);
One could also use the -ffreestanding
gcc option, but this is not recommeneded.
Upvotes: 4