为什么只有在主函数外部使用时数学库才需要链接?

5

使用gcc test.c,第一份代码样例可以编译,而第二份则不能。为什么?

它们都需要显式链接数学库才能运行(即gcc test.c -lm)。

第一份样例:

#include <stdio.h>
#include <math.h>

int main () {
   printf("%lf\n", sqrt(4.0) );
   return 0;
}

第二个样例:

#include <math.h>
#include <stdio.h>

double sqrt2(double a) { return sqrt(a); }

int main() {
  printf("%lf\n", sqrt(4.0));
  printf("%lf\n", sqrt2(4.0));
  return 0;
}

第二个示例程序出现连接错误:
/tmp/ccuYdso7.o: In function `sqrt2':
test.c:(.text+0x13): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

gcc -v:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-mul
Thread model: posix
gcc version 8.1.0 (GCC)

在主函数中使用sqrt时不会出现链接错误。这是为什么呢?我还检查了clang,但没有-lm无法编译(链接错误)。

我猜在第一个例子中它被内联了,所以没有什么需要链接的。 - Chad
4
请检查反汇编代码。我强烈感觉 sqrt(4.0) 被优化掉了。 - Eugene Sh.
2个回答

9

gcc是一款特别聪明的编译器。

它会将sqrt(4.0)优化为编译时可求值的常量表达式。这是因为sqrt的定义由标准定义,并且其返回值仅取决于其输入。(需要进一步说明的是,在IEEE754下,sqrt必须返回最接近最终结果的double。这进一步支持了优化假设)

在第二种情况下,存在其他翻译单元可以使用的函数使得这种优化失效。


3

由于对sqrt的参数在编译时已知,并且其行为是标准化的,因此可以在编译时计算sqrt(4.0)


1
如果您不介意的话,我在函数标准化方面添加了一个短语。 - Bathsheba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接