我要在keil中头文件声明和C文件定义头文件中的变量,可是我在头文件中声明后再到C文件中定义的时候
发布网友
发布时间:2022-04-24 12:43
我来回答
共3个回答
热心网友
时间:2023-10-13 05:04
是这样的,通常,变量的定义是放在C文件里,而不是头文件里,C文件定义,头文件引用。
比如,你在X.c定义一个变量 unsigned char a;如果你想要在Y.C文件里使用这个变量,你可以在Y.C里引用,引用方法如下:extern unsigned char a;
你也可以在一个头文件里引用,引用方法是一样的:extern unsigned char a;
在头文件引用后,凡是包含了这个头文件的C文件,都可以使用那个变量a,而不必再另外引用了。
有一点要注意,引用的时候,不可以带初始化,例如,X.c文件里这样定义的变量:
unsigned char a = 100; 定义的时候可以初始化,在头文件或者其它C文件引用的时候,不能初始化,妈extern unsigned char a = 100;这是不可以的,直接引用就行了:extern unsigned char a;
热心网友
时间:2023-10-13 05:05
#ifdef MODULE_ANALOG
volatile signed int LatestCCI = 0; // most recent value of Instantaneous CC conversion
signed long CCoffset = 0x0000; // this is SUBTRACTED to EACH sample from the CC Accum.
signed int CCIoffset = 0; // this is in mA and is ONLY SUBTRACTED to the 1-second total, NOT TO EACH SAMPLE.
unsigned char FastRCCal = 0;
unsigned int SlowRCCal = 0;
unsigned char BGCCRCal = 0;
unsigned char ThermistorSelect = 0;
unsigned char CellToBalance = 0;
unsigned int cell_current[4]; //used for impedance measurement
signed long RunningAcc = 0;
#else
extern volatile signed int LatestCCI;
extern unsigned char FastRCCal;
extern unsigned int SlowRCCal;
extern unsigned char BGCCRCal;
extern unsigned char ThermistorSelect;
extern unsigned char CellToBalance;
extern unsigned int cell_current[4];
extern signed long RunningAcc;
#endif
热心网友
时间:2023-10-13 05:05
声明有没有加 extern呢追问加了哦