编译带数学函数的程序出错--undefined reference to `sin'

2817阅读 0评论2010-09-04 xudonglee
分类:LINUX

最近一直很忙,从早忙到晚。。。。6:00-18:30。。。

今天在linux下面试了一个非常小的程序,找找感觉,没想到又犯了一个以前曾经遇到过的低级失误!:(

程序很简单,如下所示:


#include <stdio.h>
#include <math.h>
int main()
{
  const double pi = 4*atan(1);
  int n;
  double sin_num, cos_num;
  scanf("%d", &n);
  sin_num = sin(pi*n/180);
  cos_num = cos(pi*n/180);
  printf("%lf %lf\n", sin_num, cos_num);
  return 0;
}


编译过程如下:

[xudonglee@xudonglee ch1]$ vi ex1-4.c
[xudonglee@xudonglee ch1]$ cc -o ex1-4.out ex1-4.c
/tmp/ccKifXUJ.o: In function `main':
ex1-4.c:(.text+0x4d): undefined reference to `cos'

ex1-4.c:(.text+0x5b): undefined reference to `sin


刚开始还以为是我没有添加头文件<math.h>,可是又检查了一遍发现已经添加过了!

然后恍然大悟,想起来编译的时候要添加选项:

[xudonglee@xudonglee ch1]$ cc -lm -o ex1-4.out ex1-4.c



[xudonglee@xudonglee ch1]$ ./ex1-4.out
45
0.707107 0.707107


OK,搞定了!!!

记录在案,希望以后不再犯这个错误了!
上一篇:更改命令提示符
下一篇:intel english