From this question: Why do you have to link the math library in C?
I know that C math library (libm) is separated from C standard library (libc), and is not linked in by default.
But when I compiled the code below using gcc filename.c
without -lm
on mac osx 10.11.1
:
#include <math.h>
#include <stdio.h>
int
main (void)
{
double x = sqrt (2.0);
printf ("The square root of 2.0 is %f\n", x);
return 0;
}
There's no link error and the output executable file works correctly.
Then I tried otool -L output
:
output:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)
/opt/local/lib/libgcc/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
I wonder to know is there some library structure differences on mac?
Or it's the new feature for gcc 5.2.0?
Thanks a lot!
Update:
I changed the code with:
double in = 0;
scanf("%lf", &in);
double x = sqrt(in);
and it still doesn't need -lm
.
And I disassemble the code with otool -vVt
:
(__TEXT,__text) section
_main:
0000000100000eed pushq %rbp
0000000100000eee movq %rsp, %rbp
0000000100000ef1 subq $0x10, %rsp
0000000100000ef5 pxor %xmm0, %xmm0
0000000100000ef9 movsd %xmm0, -0x10(%rbp)
0000000100000efe leaq -0x10(%rbp), %rax
0000000100000f02 movq %rax, %rsi
0000000100000f05 leaq 0x82(%rip), %rdi ## literal pool for: "%lf"
0000000100000f0c movl $0x0, %eax
0000000100000f11 callq 0x100000f54 ## symbol stub for: _scanf
0000000100000f16 movq -0x10(%rbp), %rax
0000000100000f1a movd %rax, %xmm0
0000000100000f1f callq 0x100000f5a ## symbol stub for: _sqrt
0000000100000f24 movd %xmm0, %rax
0000000100000f29 movq %rax, -0x8(%rbp)
0000000100000f2d movq -0x8(%rbp), %rax
0000000100000f31 movd %rax, %xmm0
0000000100000f36 leaq 0x55(%rip), %rdi ## literal pool for: "The square root of 2.0 is %f\n"
0000000100000f3d movl $0x1, %eax
0000000100000f42 callq 0x100000f4e ## symbol stub for: _printf
0000000100000f47 movl $0x0, %eax
0000000100000f4c leave
0000000100000f4d retq
It seems sqrt
is called. So why things go different on mac?
Update:
I found the conclusion in this question: C std library don't appear to be linked in object file
It says on OS X, the math library is part of libSystem:
$ ls -l /usr/lib/libm.dylib
lrwxr-xr-x 1 root wheel 15 3 Jun 01:39 /usr/lib/libm.dylib@ -> libSystem.dylib
To link the math library into your program you include -lm on the gcc command line (as in the example above). To list symbols in . so . a .o or an executable files, you can use nm or objdump -t to list the contents of the symbol table.
Short answer: If you want to use functions from the math library in C, it's not enough to put #include<math. h> at the top of your source code. In addition, you must add the - lm flag to the gcc compiler command in order to use math functions in your C code.
The -l option tells GCC to link in the specified library.
Link your program with the `libm. a' library, e.g. by specifying `-lm' on the link command line.
There's no separate math library on OSX. While a lot of systems ship functions in the standard C math.h header in a separate math library, OSX does not do that, it's part of the libSystem library, which is always linked in.
In addition to that, a compiler might optimize away any such call if it can perform the computation at compile time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With