linux c作用域 全局变量使用
2016-09-13 19:39
190 查看
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。
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。
相关文章推荐
- effective javascript(五)——变量作用域(尽量少用全局变量,尽量使用局部变量)
- 需要注意变量作用域的使用
- 对变量和常量的分析,常量+{$J+}=可定义作用域的全局变量
- Application Session Cookies等全局变量的使用区分
- 关于DSP中全局变量与局部变量的使用
- 关于DSP中全局变量与局部变量的使用
- 使用内存映象实现进程间全局变量
- 在ASP.net中如何定义和使用全局变量
- 关于DSP中全局变量与局部变量的使用
- PB使用WININET的FTP方式自动更新(一、API和全局变量)
- 关于DSP中全局变量与局部变量的使用
- 使用内存映象实现进程间全局变量
- VC中怎么使用全局变量?
- 定义和使用全局变量
- 关于DSP中全局变量与局部变量的使用
- [转]DLL中使用全局变量
- 需要注意变量作用域的使用
- C# 语言中怎么使用“全局变量”?
- C#全局变量的使用
- 关于DSP中全局变量与局部变量的使用