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 编程》)

posted @ 2012-10-28 23:21  木lin木  阅读(7293)  评论(0编辑  收藏  举报