从文件math.h开方导致链接错误"未定义的引用开方"只有当参数不是一个常数 [英] sqrt from math.h causes linker error "undefined reference to sqrt" only when the argument is not a constant

查看:211
本文介绍了从文件math.h开方导致链接错误"未定义的引用开方"只有当参数不是一个常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小程序,如下:

 的#include<&math.h中GT;
  #包括LT&;&stdio.h中GT;
  #包括LT&;&unistd.h中GT;  INT主(INT ARGC,CHAR *的argv []){
    INT I;
    双TMP;
    双XX;    对于(i = 1; I< = 30;我++){
      XX =(双)1 + 0.01;      TMP =开方(XX);
      的printf(%0.4f的平方根%0.4f \\ n,XX,TMP);
      睡眠(1);
      XX = 0;
    }    返回0;
 }

当我尝试用下面的命令来编译,我得到一个编译错误。

 的gcc -Wall -o calc.c钙

返回:

  /tmp/ccavWTUB.o:在函数'主':
calc.c :(文字+ 0x4f):未定义引用'开方'
collect2:劳工处返回1退出状态

如果我更换变量调用SQRT(XX)像开方常数(10.2),它编译就好了。或者,如果我明确链接如下所示:

 的gcc -Wall -lm calc.c -o钙

也工作得很好。谁能告诉我是什么引起的?我已经很长一段时间C程序员(和我写math.h中使用类似的小程序),我从来没有见过这样的事。

我的gcc版本如下:

  $ GCC --version
海湾合作委员会(Ubuntu的4.3.3-5ubuntu4)4.3.3
版权所有(C)2008自由软件基金会
这是自由软件;参见复印条件的来源。有否
保证;甚至不是针对特定目的的适销。
$


解决方案

如果你看一下编译器在您使用开方时的输出(10.2),我敢打赌,你看到到的sqrt()的调用实际上没有做。

这是因为GCC识别多种功能,它可以特殊对待。这给它做某些优化,在这种情况下常量折叠中的能力。这种特殊的功能被称为内建

在那里必须链接到数学库(因为你是一个变量调用它)的情况下,你需要将它显式链接。某些操作系统/编译器为你做,这就是为什么你可能没有在过去的注意。

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. 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.

My version of gcc follows:

$ 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.

This happens because GCC recognizes several functions that it can treat specially. 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.

这篇关于从文件math.h开方导致链接错误&QUOT;未定义的引用开方&QUOT;只有当参数不是一个常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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