ale*_*lin 6 c compiler-errors compilation
我创建了一个小程序,如下所示:
#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;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下命令编译它时,我收到编译器错误.
gcc -Wall calc.c -o calc
Run Code Online (Sandbox Code Playgroud)
收益:
/tmp/ccavWTUB.o: In function `main':
calc.c:(.text+0x4f): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
如果我用sqrt(10.2)之类的常量替换对sqrt(xx)的调用中的变量,它编译得很好.或者,如果我明确链接如下:
gcc -Wall -lm calc.c -o calc
Run Code Online (Sandbox Code Playgroud)
它也可以正常工作.有谁能告诉我是什么原因造成的?我很长一段时间都是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.
$
Run Code Online (Sandbox Code Playgroud)
Car*_*rum 17
如果你在你使用的情况下查看编译器的输出sqrt(10.2),我敢打赌你看到sqrt()实际上没有调用.
这是因为GCC识别出它可以专门处理的几个功能.这使它能够进行某些优化,在这种情况下是常量折叠.这些特殊功能称为内置功能.
如果它必须链接到数学库(因为你用变量调用它),你需要显式链接它.有些操作系统/编译器会为您执行此操作,这就是您过去可能没有注意到的原因.
| 归档时间: |
|
| 查看次数: |
8557 次 |
| 最近记录: |