gcc默认在Mac OS X上的C语言中链接数学库吗? [英] gcc link the math library by default in C on mac OS X?

查看:125
本文介绍了gcc默认在Mac OS X上的C语言中链接数学库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个问题开始:为什么必须链接C语言中的数学库?

我知道C数学库(libm)与C标准库(libc)是分开的,并且默认情况下未链接.

I know that C math library (libm) is separated from C standard library (libc), and is not linked in by default.

但是当我在Mac OS X 10.11.1上使用不带-lmgcc filename.c编译以下代码时 :

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.

然后我尝试了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)

我想知道Mac上是否存在某些库结构差异?

I wonder to know is there some library structure differences on mac?

还是gcc 5.2.0的新功能?

Or it's the new feature for gcc 5.2.0?

非常感谢!

更新:

我通过以下方式更改了代码

I changed the code with:

    double in = 0;
    scanf("%lf", &in);
    double x = sqrt(in);

,它仍然不需要-lm.

然后我用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

似乎已调用sqrt.那么为什么Mac上的情况会有所不同?

It seems sqrt is called. So why things go different on mac?

更新:

我在以下问题中找到了结论: C std库似乎未在目标文件中链接

I found the conclusion in this question: C std library don't appear to be linked in object file

它表示在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

推荐答案

OSX上没有单独的数学库.虽然许多系统在单独的数学库中的标准C math.h标头中提供函数,但OSX却不这样做,它是libSystem库的一部分,该库始终链接在其中.

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.

这篇关于gcc默认在Mac OS X上的C语言中链接数学库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆