只有当参数不是常量时,math.h中的sqrt才会导致链接器错误“未定义对sqrt的引用”
我创建了一个小程序,如下所示:
#include #include #include int main(int argc, char *argv[]) { int i; double tmp; double xx; for(i = 1; i <= 30; i++) { xx = (double) i + 0.01; tmp = sqrt(xx); printf("the square root of %0.4f is %0.4f\n", xx,tmp); sleep(1); xx = 0; } return 0; }
当我尝试使用以下命令编译它时,我收到编译器错误。
gcc -Wall calc.c -o calc
收益:
/tmp/ccavWTUB.o: In function `main': calc.c:(.text+0x4f): undefined reference to `sqrt' collect2: ld returned 1 exit status
如果我用sqrt(10.2)之类的常量替换sqrt(xx)调用中的变量,它编译得很好。 或者,如果我明确链接如下:
gcc -Wall -lm calc.c -o calc
它也可以正常工作。 有谁能告诉我是什么原因造成的? 我很长一段时间都是C程序员(我用math.h写过类似的小程序),我从来没有见过这样的东西。
我的gcc版本如下:
$ gcc --version gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $
如果你在使用sqrt(10.2)
的情况下查看编译器的输出,我敢打赌你看到实际上没有调用sqrt()
。
这是因为GCC识别出它可以专门处理的几个function。 这使它能够进行某些优化,在这种情况下是常量折叠 。 这些特殊function称为内置function。
如果它必须链接到数学库(因为你用变量调用它),你需要显式链接它。 有些操作系统/编译器会为您执行此操作,这就是您过去可能没有注意到的原因。