我有以下代碼(簡化為該問題的基本知識):#include<stdio.h>#include<math.h>double f1(double x){ double res = sin(x); return 0;}/* The main function */int main(void){ return 0;}使用它進行編譯時,gcc test.c出現以下錯誤,并且我無法找出原因:/tmp/ccOF5bis.o: In function `f1':test2.c:(.text+0x13): undefined reference to `sin'collect2: ld returned 1 exit status但是,我編寫了sin從main功能內部調用的各種測試程序,這些程序運行良好。我肯定在這里做錯了什么-那是什么?
3 回答

慕尼黑5688855
TA貢獻1848條經驗 獲得超2個贊
無論如何我還是有問題-lm
gcc -Wall -lm mtest.c -o mtest.o
mtest.c: In function 'f1':
mtest.c:6:12: warning: unused variable 'res' [-Wunused-variable]
/tmp/cc925Nmf.o: In function `f1':
mtest.c:(.text+0x19): undefined reference to `sin'
collect2: ld returned 1 exit status
我最近發現,如果您首先指定-lm,它將不起作用。順序很重要:
gcc mtest.c -o mtest.o -lm
鏈接無問題
因此,您必須在之后指定庫。

繁華開滿天機
TA貢獻1816條經驗 獲得超4個贊
請注意,Mac(運行Mac OS X或macOS)上的系統庫包含數學函數。有一個庫可以滿足對內部-lm
版本的引用,但是您無需使用它-lm
來鏈接數學函數。導致數學庫與其他數學庫分離的主要問題是CPU并不總是內置浮點數。有浮點協處理器(80387等),因此有很多方法提供功能(使用軟件浮點庫或使用硬件,并提供不同的支持)。
- 3 回答
- 0 關注
- 1711 瀏覽
添加回答
舉報
0/150
提交
取消