编写多文件的程序时,编译器对于每个有名字的变量和函数都有链接模式的区别,分别是外链接(external) 内链接(internal) 无链接(noneternal),具体使用如下:
A.c
#include "C.h"
extern temp_b; //外链接模式
extern temp_c;
static temp_d //内链接模式,只在A.c中有效
B.c
int temp_b
static temp_d //内链接模式,只在B.c中有效
C.h
static int temp_c
如果A.c中要使用C.h中的temp_c变量,一定要在C.h中设置为static类型,不然编译器会出现重复定义这个变量的报错。如果又不想改变这个数值,可以设置为static const类型。