两个.c文件之间使用:
test1.c和test2.c两个.c文件。要在test2.c中使用test1.c中定义的变量和函数。
例:
test1.c文件:
int caldata = 0x55;
unsigned char tridata= 0xaa;
void calfun ( int i );
void testfun ()
{
caldata = tridata;
calfun ( caldata );
}
void calfun ( int i )
{
int j;
j = i;
}
在test2.c中要引用test1.c中的变量tridata和函数calfun.
注意下面红色字体部分:
extern unsigned char tridata;
extern void calfun ( int i );
unsigned char x;
int y;
void main ()
{
x = tridata;
calfun ( y );
}
总结:只要在要引用的.c文件中,用extern加上被引用的变量/函数定义,即可将其它.c文件中的变量/函数引用过来。
但要注意,被引用的变量,在被引用文件中应是全局变量。