1 Basic explanations
extern can be placed before a variable or function to mark the definition of a variable or function in another file, prompting the compiler to find its definition in other modules when it encounters this variable and function.
In addition, extern can also be used for link designation.
2 Question: extern variable
An array is defined in a source file: Char a[6];
In another file, it is declared with the following statement: extern char *a;
Excuse me, is this OK?
Answer and Analysis:
1, can not, the program will tell you when the operation of illegal access. The reason is that pointers to type T are not equivalent to arrays of type T. The extern char *a declares a pointer variable instead of a character array, and therefore differs from the actual definition, resulting in illegal access at run time. The declaration should be changed to extern char a[].
2), example analysis as follows, if a[] = "ABCD", then the external variable a=0x61626364 (ABCD ASCII code value), *a obviously meaningless
Obviously a point of space (0x61626364) is meaningless and prone to illegal memory access.
3, this prompts us, in the use of extern time to strictly correspond to the declaration when the format, in actual programming, such errors are not uncommon.
4, extern used in variable declarations often have such a role, you declare a global variable in the *.C file, the global variable if you want to be referenced, put in the *.h and declared with extern.
4 Question: extern function 2
When the function provider modifies the function prototype unilaterally, the compiler will not make an error if the user does not knowingly continue with the original extern declaration. But in the running process, because less or more input parameters, often will be a system error, this situation should be how to solve.
Answer and Analysis:
Currently, the industry does not have a perfect solution for this situation, the common practice is that the provider in its own xxx_pub.h to provide a declaration of the external interface, and then the caller include the header file, thus eliminating the step of extern. To avoid this error.
The sword has two fronts, for the application of extern, different occasions should choose a different approach.
5 question: extern "C"
When using C functions in C + + environments, it is often possible for the compiler to find the C function definition in the OBJ module, which causes the link to fail, and how it should be resolved.
Answer and Analysis:
the C + + language is compiled to solve the polymorphic problems of functions, the function names and parameters will be combined to generate a middle of the functions of the name, and the C language will not, so will cause the link can not find the corresponding function, then C function needs to use extern "C" Make a link designation, which tells the compiler to keep my name, and do not give me an intermediate function name for the link.
The following is a standard notation:
//On the header of the. h file
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif/* Cplusplus */
...
...
//.h where the file ends
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif/* __cplusplus */ 3 Problem:extern function 1
often see extern placed in front of a function as part of a function declaration, so the keyword extern of C language plays a role in the declaration of a function.
Answer and Analysis:
If the declaration of a function has a keyword extern, it simply implies that the function may be defined in another source file, with no other effect. That is, there are no obvious differences between the following two function declarations:
extern int f (); and int f ();
Of course, the use of this is in the program to replace the include "*.h" to declare functions, in some complex projects, I am more accustomed to all the function declarations before adding extern decoration