日期:2014-05-16  浏览次数:20598 次

如何用g++编译包含多个文件的源码
我在学习鸟哥的linux私房菜第22章22.3.1节中的案例时 发现将函数类型改为.cpp的话,main.cpp函数不能编译,用
g++ -c main.cpp haha.cpp sin_value.cpp cos_value.cpp 
编译时,显示如下信息:
main.cpp:in function 'int main()':
main.cpp:13:error:'haha'was not declaredin this scope
main.cpp:14:error:'sin_value'was not declaredin this scope
main.cpp:15:error:'cos_value'was not declaredin this scope
请教各位,这是怎么回事呢,把这些文件的后缀全部改为.c,则能成功编译呢,.c++就不行。
各个函数内容如下:
/*main.cpp
#include <stdio.h>
#include <math.h>
#define pi 3.14159
char name[15];
float angle;
int main(void)
{
printf ("\n\nPlease input your name: ");
scanf ("%s", &name );
printf ("\nPlease enter the degree angle (ex> 90): " );
scanf ("%f", &angle );
haha( name );
sin_value( angle );
cos_value( angle );
}

/*sin_value.cpp
#include <stdio.h>
#include <math.h>
#define pi 3.14159
float angle;
void sin_value(void)
{
float value;
value = sin ( angle / 180. * pi );
printf ("\nThe Sin is: %5.2f\n",value);
}

/*cos_value.cpp
#include <stdio.h>
#include <math.h>
#define pi 3.14159
float angle;
void cos_value(void)
{
float value;
value = cos ( angle / 180. * pi );
printf ("The Cos is: %5.2f\n",value);
}

/*haha.cpp
#include <stdio.h>
int haha(char name[15])
{
printf ("\n\nHi, Dear %s, nice to meet you.", name);
}



------解决方案--------------------
这是C++语法规则上的啦,extern一下你要用到的函数。