【求助】gcc编译c文件,不能调用sin,cos,exp等函数~~

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
lostvind
帖子: 6
注册时间: 2011-03-29 22:57

【求助】gcc编译c文件,不能调用sin,cos,exp等函数~~

#1

帖子 lostvind » 2011-04-02 20:14

我用gedit写了一个fft.c的文件,然后用下面代码编译

代码: 全选

gcc -Wall fft.c -o fft.out
然后出现如下错误:
fft.c:78: warning: return type of ‘main’ is not ‘int’
fft.c: In function ‘main’:
fft.c:96: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘complex’
/tmp/ccP5bnmV.o: In function `complex_exp':
fft.c:(.text+0xf): undefined reference to `exp'
fft.c:(.text+0x2c): undefined reference to `cos'
fft.c:(.text+0x54): undefined reference to `sin'

collect2: ld returned 1 exit status
加粗的三行是最关键的,在子函数里我调用了sin cos和exp三个函数,我在c文件最开始包含了math.h文件和stdio.h文件
有谁知道是怎么回事吗~~

附c文件:

代码: 全选

#include<stdio.h>
#include<math.h>
#define M_PI 3.14159265358979323846
…………
typedef struct {
	float r,i;
}complex;

typedef struct {
	double r,i;
}doublecomplex;

void complex_exp(complex *r,complex *z)
{
        double expx;
	expx = exp((double)z->r);
	r->r = (float)expx * cos((double)z->i);
	r->i = (float)expx * sin((double)z->i);
}

void fftc(complex *x,long *n,long *isign)
{
       ……………………
}

void main()
{
       ...............................
       fftc(.........)
       ..................................
}
wutong
帖子: 345
注册时间: 2007-10-22 16:54

Re: 【求助】gcc编译c文件,不能调用sin,cos,exp等函数~~

#2

帖子 wutong » 2011-04-02 20:22

gcc -lm -o ffc.out ffc.c

加了 -lm 就是让gcc找到包含文件:math.h
lostvind
帖子: 6
注册时间: 2011-03-29 22:57

Re: 【求助】gcc编译c文件,不能调用sin,cos,exp等函数~~

#3

帖子 lostvind » 2011-04-02 20:27

wutong 写了:gcc -lm -o ffc.out ffc.c

加了 -lm 就是让gcc找到包含文件:math.h
可以用了!太感谢了!!

不过现在数据输出还是不对,估计是我程序写的有问题~
:em06
回复