简体   繁体   English

仅当参数不是常量时,来自math.h的sqrt才会导致链接器错误“对sqrt的未定义引用”

[英]sqrt from math.h causes linker error “undefined reference to sqrt” only when the argument is not a constant

I created a small program, as follows: 我创建了一个小程序,如下所示:

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

  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;
 }

When I try to compile this with the following command, I get a compiler error. 当我尝试使用以下命令对此进行编译时,出现编译器错误。

gcc -Wall calc.c -o calc

returns: 返回:

/tmp/ccavWTUB.o: In function `main':
calc.c:(.text+0x4f): undefined reference to `sqrt'
collect2: ld returned 1 exit status

If I replace the variable in the call to sqrt(xx) with a constant like sqrt(10.2), it compiles just fine. 如果我将sqrt(xx)调用中的变量替换为sqrt(10.2)之类的常量,则编译就可以了。 Or, if I explicitly link like the following: 或者,如果我明确链接如下:

gcc -Wall -lm calc.c -o calc

It also works just fine. 它也可以正常工作。 Can anyone tell me what's causing this? 谁能告诉我是什么原因造成的? I've been a C programmer for a long time (and I've written similar small programs using math.h) and I have never seen anything like this. 我很长时间以来一直是C程序员(并且我已经使用math.h编写了类似的小程序),但从未见过这样的事情。

My version of gcc follows: 我的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.
$

If you look at the output of the compiler in the case where you used sqrt(10.2) , I'll bet you see that a call to sqrt() isn't actually made. 如果您在使用sqrt(10.2)的情况下查看编译器的输出,我敢打赌,您会发现实际上并未调用sqrt()

This happens because GCC recognizes several functions that it can treat specially. 发生这种情况是因为GCC可以识别几种可以特殊处理的功能。 This gives it the ability to do certain optimizations, in this case Constant folding . 这样,它就可以进行某些优化,在本例中为“ 常量折叠” Such special functions are called Built-ins . 这样的特殊功能称为内置功能。

In the case where it must link to the math library (because you're calling it with a variable), you need to link it explicitly. 如果必须将其链接到数学库(因为要使用变量对其进行调用),则需要显式链接。 Some operating systems/compilers do it for you, which is why you might not have noticed in the past. 一些操作系统/编译器会为您执行此操作,这就是为什么您过去可能没有注意到的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM