undefined reference to `sin'問題解決,undefinedreference

來源:互聯網
上載者:User

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
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.