Linux(ubuntu) undefined reference to `sin'問題解決


轉自:http://blog.csdn.net/llqkk/article/details/2447029

今天在編譯程序時碰到該問題:
whetstone.c:(.text+0x56c): undefined reference to `sin'
whetstone.c:(.text+0x585): undefined reference to `cos'
whetstone.c:(.text+0x5cf): undefined reference to `atan'
檢查頭文件math.h已經包含,原來雖然程序中已經包含math.h,但在鏈接時要鏈接到數學庫,加上-lm編譯選項即可,如下:
將 gcc whetstone.c -o whetstone   改為 gcc whetstone.c -lm -o whetstone即可。

 

PS : 我的系統是centos,同樣的問題也解決了。。

使用 math.h 中聲明的庫函數還有一點特殊之處,gcc 命令行必須加-lm 選項,因為數學函
數位於 libm.so 庫文件中(這些庫文件通常位於/lib 目錄下),-lm 選項告訴編譯器,
我們程序中用到的數學函數要到這個庫文件里找。本書用到的大部分庫函數(例如
printf)位於 libc.so 庫文件中,使用 libc.so 中的庫函數在編譯時不需要加-lc 選項,
當然加了也不算錯,因為這個選項是 gcc 的默認選項。
(以上選自《linux C 編程》)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM