undefined reference to `sin'問題解決,undefinedreference
作者:zhanhailiang 日期:2014-10-25
使用gcc編譯如下代碼時報“undefined reference to `sin'”:
#include <stdio.h>#include <math.h>#include <stdlib.h> main () { double a = sin(1); exit (0);}
[root@~/wade/codeReview/learningc/9]# gcc -o timetest timetest.c /tmp/ccIzIHF7.o: In function `func':timetest.c:(.text+0x3f): undefined reference to `sin'collect2: ld returned 1 exit status
一般是因為缺少數學庫導致,只需要在編譯時間手動添加gcc libm.so庫即可,如下:
[root@~/wade/codeReview/learningc/9]# gcc timetest.c -lm -o timetest[root@~/wade/codeReview/learningc/9]# ./timetest
其中,通過-l指定相應庫的路徑,預設為系統庫路徑,如/lib和/usr/lib64(64位)(這個預設系統庫路徑值待筆者確認後再修改),linux下的庫統一命名規範都是lib***.so,故-lm表示gcc預設到系統庫路徑下去查到libm.so庫,如下:
[root@/usr/lib64]# find /usr/lib64/ |grep "libm.so"/usr/lib64/libm.so[root@/usr/lib64]# find /lib |grep "libm.so"
200分:linux 下 undefined reference to `cos'的問題?
編譯的時候帶上參數 -lm
如:
gcc -o myproc myproc.cpp -L/usr/lib -lm
Ubuntu下使用gcc編譯c檔案,不可以調用sin cos exp等函數~
編譯時間加上 -lm
不行再試這個:
gcc -Wall fft.c /usr/lib/libm.a -o fft.out