C语言库函数的调用问题 ,在linux下编译

来源:互联网 发布:c语言计算器程序代码 编辑:程序博客网 时间:2024/05/17 01:06

  1 #include <stdio.h>  2 #include <math.h>  3 #define PI 3.14159  4 #define OR 180  5   6 int main (void)  7 {  8         double a = 30; //角度  9         double b = a * PI / OR;//得出角度 10  11  12         printf ("%lf", sin (b)); 13         return 0; 14 }~                 

学习C 语言已经有好一段日子了,因为没用用集成环境所以有时候编译起来比较麻烦。

我是在linux下用GCC 编译的。

以上是我的源代码我用传统方法编译链接

gcc -o sin sin.c

结果出现了以下错误

/tmp/ccpJqI9K.o: In function `main':sin.c:(.text+0x46): undefined reference to `sin'collect2: error: ld returned 1 exit status

后来google得到需要我们手动链接

编译代码如下

gcc -o sin sin.c -lm
编译链接成功,运行后结果正确

0.500000 

[lan@localhost ~]$ 

这些链接问题以后等深入了可能会更多的遇到,所以写一篇博客。

参考资料

http://hi.baidu.com/zhou347742/item/7606b335ed8e449ab90c03e5

除夕夜祝大家新年快乐!!

0 0
原创粉丝点击