很多人对C语言中的 “文件包含”都不陌生了,文件包含处理在程序开发中会给我们的模块化程序设计带来很大的好处,通过文件包含的方法把程序中的各个功能模块联系起来是模块化程序设计中的一种非常有利的手段。
文件包含处理是指在一个源文件中,通过文件包含命令将另一个源文件的内容全部包含在此文件中。在源文件编译时,连同被包含进来的文件一同编译,生成目标目标文件。
1: //file1: main.c
2: #include
3: #include "fun.c"
4: int main()
5: {
6: int a=5,b=19;
7: c = a;
8: sun(a,b);
9: printf("c=%d\n",c);
10: return 0;
11: }
12: //end of file1
1: //file2: fun.c
2: int c=0;
3: void sun(int a, int b)
4: {
5: printf("a+b=%d\n",a+b);
6: c=0;
7: printf("c=%d\n",c);
8: }
9: //end of file2
10:
1: //main.cpp文件中
2: 931 # 2 "main.c" 2
3: 932 # 1 "fun.c" 1
4: 933 //注意这里是fun.c里边的内容
5: 934 int c=0;
6: 935 void sun(int a, int b)
7: 936 {
8: 937 printf("a+b=%d\n",a+b);
9: 938 c=0;
10: 939 printf("c=%d\n",c);
11: 940 }
12: //这里是main函数
13: 941 # 3 "main.c" 2
14: 942 int main()
15: 943 {
16: 944 int a=5,b=19;
17: 945 c = a;
18: 946 printf("c=%d\n",c);
19: 947 sun(a,b);
20: 948 printf("c=%d\n",c);
21: 949 return 0;
22: 950 }
1: //file1: main.c
2: #include
3: //#include "fun.c" //注释掉
4: extern int c; //添加这一句
5: int main()
6: {
7: int a=5,b=19;
8: c = a;
9: sun(a,b);
10: printf("c=%d\n",c);
11: return 0;
12: }
13: //end of file1
14:
15:
16: //file2: fun.c
17: int c=0;
18: void sun(int a, int b)
19: {
20: printf("a+b=%d\n",a+b);
21: c=0;
22: printf("c=%d\n",c);
23: }
24: //end of file2
1: //file1: main.c
2: #include
3: #include "fun.h" //fun.c修改为fun.h
4: //extern int c; //这行也不要了
5: int main()
6: {
7: int a=5,b=19;
8: c = a;
9: sun(a,b);
10: printf("c=%d\n",c);
11: return 0;
12: }
13: //end of file1
1:
2: //file2: fun.c
3: #include "fun.h"
4: int c=0; //变量c的定义
5: void sun(int a, int b) //函数sun()的定义
6: {
7: printf("a+b=%d\n",a+b);
8: c=0;
9: printf("c=%d\n",c);
10: }
11: //end of file2
1: //file3: fun.h
2: extern int c; //把c声明为外部可用的
3: void sun(int a, int b); //sun()函数的声明
4: //end of file3
原文:http://www.cnblogs.com/Bonker/p/3548276.html