我創建一個小程序,如下所示:SQRT導致連接錯誤「未定義參照SQRT」只有當參數不是恆定
#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;
}
當我嘗試用下面的編譯此命令,我得到一個編譯器錯誤。
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(XX)與像開方(10.2)常數,它編譯就好了。或者,如果我明確地鏈接如下:
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.
$
如果指定'--std = c99',它仍然需要'-lm'嗎? – 2009-11-10 23:15:28
是的。奇怪的。我也嘗試使用-ansi來達到同樣的效果。 No-go without -lm – alesplin 2009-11-10 23:22:18
這是gcc的一個有趣功能,您需要明確鏈接數學庫 - 它首次吸引了所有人。 編譯器可以靜靜地替換sqrt(10.2),只是增加了樂趣! – 2009-11-10 23:25:20