[英]gcc link the math library by default in C on mac OS X?
From this question: Why do you have to link the math library in C? 从这个问题出发: 为什么必须在C中链接数学库?
I know that C math library (libm) is separated from C standard library (libc), and is not linked in by default. 我知道C数学库(libm)与C标准库(libc)是分开的,并且默认情况下未链接。
But when I compiled the code below using gcc filename.c
without -lm
on mac osx 10.11.1 : 但是当我在Mac osx 10.11.1上使用
gcc filename.c
而不使用-lm
编译以下代码时:
#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
: 然后我尝试了
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? 我想知道Mac上是否存在某些库结构差异?
Or it's the new feature for gcc 5.2.0? 还是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
. 而且它仍然不需要
-lm
。
And I disassemble the code with otool -vVt
: 我用
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. 似乎
sqrt
被称为。 So why things go different on mac? 那么,为什么Mac上的情况会有所不同?
Update : 更新 :
I found the conclusion in this question: C std library don't appear to be linked in object file 我在以下问题中找到结论: C std库似乎未在目标文件中链接
It says on OS X, the math library is part of libSystem: 它说在OS X上,数学库是libSystem的一部分:
$ ls -l /usr/lib/libm.dylib
lrwxr-xr-x 1 root wheel 15 3 Jun 01:39 /usr/lib/libm.dylib@ -> libSystem.dylib
There's no separate math library on OSX. 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.
虽然许多系统在单独的数学库中的标准C math.h标头中提供函数,但OSX不会这样做,但它是libSystem库的一部分,该库始终链接在其中。
In addition to that, a compiler might optimize away any such call if it can perform the computation at compile time. 除此之外,如果编译器可以在编译时执行计算,则它可以优化掉此类调用。
sqrt
is provided as a compiler built-in, so no link to the library is necessary (as it happens - doing so would still be good practice so that it compiles elsewhere). sqrt
是作为内置的编译器提供的,因此不需要链接到库(发生这种情况-这样做仍然是一个好习惯,以便可以在其他地方进行编译)。
The ISO C90 functions [long list including
sqrt
] are all recognized as built-in functions unless-fno-builtin
is specified (or-fno-builtin-function
is specified for an individual function).在ISO C90函数[一长串包括
sqrt
]除非都识别为内置函数-fno-builtin
指定(或-fno-builtin-function
为单个功能被指定)。 All of these functions have corresponding versions prefixed with__builtin_
.所有这些功能都具有以
__builtin_
为前缀的相应版本。
If you compile with -fno-builtin
I would expect a failure at the link stage. 如果使用
-fno-builtin
编译,则在链接阶段会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.