我是C語言的新手,我有以下代碼:#include <stdio.h>#include <math.h>int main(void){ double x = 0.5; double result = sqrt(x); printf("The square root of %lf is %lf\n", x, result); return 0;}但是當(dāng)我用以下命令編譯時:gcc test.c -o test我收到這樣的錯誤:/tmp/cc58XvyX.o: In function `main':test.c:(.text+0x2f): undefined reference to `sqrt'collect2: ld returned 1 exit status為什么會這樣?是sqrt()不是在math.h頭文件?我cosh和其他三角函數(shù)遇到相同的錯誤。為什么?
3 回答

拉莫斯之舞
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個贊
您需要使用鏈接-lm器選項(xiàng)鏈接
您需要編譯為
gcc test.c -o test -lm
歷史上,默認(rèn)情況下,gcc(非g ++)在鏈接時不會包含數(shù)學(xué)函數(shù)。它還已從libc分離到單獨(dú)的庫libm中。要使用這些功能,你必須通知鏈接到包含鏈接庫-l鏈接選項(xiàng),后跟庫名m這樣-lm。
- 3 回答
- 0 關(guān)注
- 953 瀏覽
添加回答
舉報(bào)
0/150
提交
取消