Extern usage
By Crifan Li
Date:
First, we should know the what is the meaning of the keyword: “extern” :
It means, this variable or function can be used in else-where !
Not means the variable or function is defined else-where.
So, “extern” should ONLY be seen in the original defined place, and its header file, NOT the place where we use the extern variable or function!!!!!
1. in the C file, define the global variable ,
let’s say :
in item_audio_equalizer.c, we define a global variable:
const eq_struct eqList[EQ_MAX_NR] =
{
0, "
1, "Class", -1, -1, -1, -5, -6,
2, "Jazz", 6, -2, -3, -1, 4,
3, "Pop", -3, 4, 4, -2, -1,
4, "Rock", 9, -3, -2, 7, 7,
5, "Cust", -1, -1, -1, -1, -1
};
This means, we defined a (global) variable here.
2. in the C header file, exclaim it
let’s say:
in item_audio_equalizer.h, we exclaim it:
extern const eq_struct eqList[EQ_MAX_NR];
this means, this variable can be used in other place by other C file
just same as the functions exclaimed in this header file !
3. in other C file, If you want to use that extern variable, you just need use it Directly !!!
do not need to add something like “extern const eq_struct eqList[EQ_MAX_NR];”
or “const eq_struct eqList[EQ_MAX_NR];”
Conclusion:
ONLY the functions or variables exclaimed in the header file of the original C file,
Can be used by other place!!!
The functions and variables, realized in C file but not exclaimed in header file, is means the private functions and variables ,only can be used by itself’s C file, can not be used in other place!
转载请注明:在路上 » Extern usage