我是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;}但是當我用以下命令編譯時: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和其他三角函數遇到相同的錯誤。為什么?
3 回答

冉冉說
TA貢獻1877條經驗 獲得超1個贊
您需要使用鏈接-lm器選項鏈接
您需要編譯為
gcc test.c -o test -lm
歷史上,默認情況下,gcc(非g ++)在鏈接時不會包含數學函數。它還已從libc分離到單獨的庫libm中。要使用這些功能,你必須通知鏈接到包含鏈接庫-l鏈接選項,后跟庫名m這樣-lm。
- 3 回答
- 0 關注
- 464 瀏覽
添加回答
舉報
0/150
提交
取消