linux c作用域 全局变量使用

来源:互联网 发布:机房网络拓扑结构图 编辑:程序博客网 时间:2024/06/08 00:41

linux下将多个文件编译成一个文件

gcc file1 -c -o file1.o

gcc file2 -c -o file2.o

gcc file1.o file.o -o exec

文件中的全局变量使用:在一个文件中声明一个变量a,再其对应的.h文件中声明extern int a;当别的文件包含此头文件后就可以使用同一个a全局变量了。如下图代码

test1.c:

#include<stdio.h>
#include"test2.h"
int temp;
int main()
{
   temp = 100;
   shuchu();
   return 0;
}
test1.h:

extern int temp;

test2.c:

#include<stdio.h>
#include"test1.h"
int shuchu()
{
    printf("temp = %d", temp);
    return 0;
}
 test2.h:

int shuchu();


用开始的方法编译进行测试可以输出100。

0 0
原创粉丝点击