首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

为什么gcc会出现这样的异常

2013-08-04 
为什么gcc会出现这样的错误程序如下:#include stdio.h#include math.henum coordinate_type { RECTANG

为什么gcc会出现这样的错误
程序如下:

#include <stdio.h>
#include <math.h>

enum coordinate_type { RECTANGULAR, POLAR };/*定义复数结构提*/
struct complex_struct {
enum coordinate_type t;
double a, b;
};

struct complex_struct make_from_real_img(double x, double y)
{
struct complex_struct z;
z.t = RECTANGULAR;
z.a = x;
z.b = y;
return z;
}

struct complex_struct make_from_mag_ang(double r, double A)
{
struct complex_struct z;
z.t = POLAR;
z.a = r;
z.b = A;
return z;
}


double real_part(struct complex_struct z)
{
    if(z.t == RECTANGULAR)
        return z.a * cos(z.b);
    else 
        z.a;
}
 
double img_part(struct complex_struct z)
{
    if(z.t == RECTANGULAR)
        return z.a * sin(z.b);
    else 
        z.b;
}
 
double magnitude(struct complex_struct z)
{
    if(z.t == RECTANGULAR)
        return sqrt(z.a * z.b + z.a * z.b);
    else
        return z.a;
}
 
double angle(struct complex_struct z)
{
    if(z.t == RECTANGULAR)
        return atan2(z.a, z.b);
    else
        return z.b;
}

int print_complex(struct complex_struct z)
{
   printf("real is %f\n,img is %f\n,magnitude is %f\n,angle is %f\n",real_part(z),img_part(z),magnitude(z),angle(z));

  return 0;
}
int main(void)
{
struct complex_struct a = make_from_real_img (1, 1); 
struct complex_struct b = make_from_mag_ang (1, 1); 
print_complex(a);
print_complex(b);
return 0;
}
结果是这样的
$ gcc complex.c
/tmp/cc5hlhJw.o: In function `real_part':
complex.c:(.text+0xc4): undefined reference to `cos'
/tmp/cc5hlhJw.o: In function `img_part':


complex.c:(.text+0xef): undefined reference to `sin'
/tmp/cc5hlhJw.o: In function `magnitude':
complex.c:(.text+0x12d): undefined reference to `sqrt'
/tmp/cc5hlhJw.o: In function `angle':
complex.c:(.text+0x159): undefined reference to `atan2'
collect2: ld 返回 1
实在是不理解啊 math.h调用不出来???怎么办
GCC math.h 函数
[解决办法]
编译的时候加一个数学库 -lmath
[解决办法]
这么编译就可以了
gcc -lm complex
加一个-lm,
math.h是外部链接库.不加-l找不到的.

热点排行