1 basic explanation
Externs can be placed before the variable or function, to mark the variable or function definition in another file, prompting the compiler to seek definitions in other modules when the compiler encounters this variable and function.
In addition, Extern can also be used for link designation.
2 Question: Extern variable
Define an array in a source file:
Char a [6];
In another file, use the following statement to declare:
EXTERN CHAR * A;
Excuse me, can you?
Answer and analysis:
1), not, the program will tell you illegal access. The reason is that the pointer to the type T does not equal the array of type T. EXTERN CHAR * A declares is a pointer variable rather than a character array, so it is different from the actual definition to cause illegal access to runtime. The statement should be changed to Extern Char A [].
2), the example analysis is as follows, if a [] = "abcd", the external variable A = 0x61626364 (ABCD ASCII code value), * a obviously meaningless, as shown below:
Obviously A pointing space (0x61626364) does not make sense, easy to have illegal memory access.
3), this prompts us, in the format of the declaration, in actual programming, such mistakes are frequent in actual programming.
4) EXTERN often has such a role in the variable declaration, you declare a global variable in the * .c file, if you want to be referenced, put it in * .h and use Extern to declare.
3 Question: Extern function 1
It is often seen in front of the function in front of the function to become a part of the function declaration, then what role does the keyword EXTERN of the C language works in the statement of the function?
Answer and analysis:
If the function's declaration is with keyword extern, it is only implicit that this function may be defined in other source files, without other functions. That is, the following two functions have no obvious difference:
EXTERN INT F (); and INT F ();
Of course, such use is still, it is to replace include "* .h" in the program to declare the function, in some complex projects, I am more habitually to add extern modifications before all function declarations.
4 Question: Extern function 2
When the function provider unilaterally modifies the function prototype, if the user does not know the original Extern declaration, the compiler will not report an error. However, during operation, because less or more input parameters, it is often necessary to make a system error. How should this situation solved?
Answer and analysis:
At present, there is no perfect solution for this situation in this case, and the usual approach is to provide a declaration for external interfaces in our own xxx_pub.h, then call the partial file file to save this step. To avoid such errors.
The sword has a double front, the application of Extern, and the different occasions should choose different practices.
5 Question: EXTERN "C"
When using the C function in the C environment, the compiler will not find the C function definition in the OBJ module, resulting in the case where the link fails, how should this situation?
Answer and analysis:
C language In order to solve the polymorphic issue of the function, the C language will join the function name and parameters to generate a middle function name, and the C language will not, so the corresponding function is not found when the link is caused. At this time The C function needs to use Extern "C" to specify, this tells the compiler, keep my name, do not generate an intermediate function name for links. Below is a standard way:
// On the head of the .h file # ifef __cplusplus # if __cplusplusextern "c" {#ENDIF #ENDIF / * __CPLUSPLUS * / ... //.h file ended #ifdef __cplusplus #if __cplusplus} # Endif # ENDIF / * __CPLUSPLUS * /