1. Basic explanation
Extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function.
In addition, extern can be used to specify links.
2 problem: extern variable
An array is defined in a source file: Char A [6];
The following statement is used in another file: extern char *;
Can this be done?
Answer and analysis:
1) No. Illegal access will be reported during the program running. The reason is that the pointer to type T is not equivalent to the array of type T. Extern char * A declares a pointer variable instead of a character array. Therefore, it is different from the actual definition, resulting in invalid access during runtime. Change the Declaration to extern char a [].
2) The example analysis is as follows. If a [] = "ABCD", the external variable a = 0x61626364 (the ASCII code value of ABCD), * A is obviously meaningless.
Obviously, the space (0x61626364) that a points to is meaningless and prone to illegal memory access.
3) This reminds us that we must strictly match the declared format when using extern. In actual programming, such errors are not uncommon.
4) extern is often used in variable declaration *. the C file declares a global variable. If the global variable is to be referenced, it is placed in *. H and use extern to declare.
4 problem: extern function 2
When the function provider unilaterally modifies the function prototype, if the user does not know to continue to use the original extern statement, the compiler will not report an error during compilation. However, during the running process, a system error is often caused by missing or missing input parameters. How can this problem be solved?
Answer and analysis:
Currently, the industry does not have a perfect solution to deal with this situation. The common practice is that the provider provides external interface declarations in its own xxx_pub.h, and then the caller includes the header file, in this way, the extern step is omitted. To avoid such errors.
Baojian has a dual front. For the application of extern, different practices should be selected for different occasions.
5. Problem: extern "C"
When using C functions in the C ++ environment, the compiler often fails to find the C function definition in the OBJ module, resulting in a link failure. How can this problem be solved?
Answer and analysis:
During compilation, C ++ will combine function names and parameters to generate an intermediate function name to solve the problem of function polymorphism. c ++ does not, therefore, the corresponding function cannot be found during the link. In this case, the C function needs to use the extern "C" to specify the link. This tells the compiler to keep my name, do not generate an intermediate function name for the link for me.
The following is a standard syntax:
// On the. h File Header
# Ifdef _ cplusplus
# If _ cplusplus
Extern "C "{
# Endif
# Endif/* _ cplusplus */
...
...
// Where the. h file ends
# Ifdef _ cplusplus
# If _ cplusplus
}
# Endif
# Endif/* _ cplusplus */
3 problem: extern function 1
It is often seen that extern is placed before the function to become part of the function declaration. So, what is the role of the C language keyword extern in the function declaration?
Answer and analysis:
If the function declaration contains the keyword extern, it only implies that the function may be defined in other source files and has no other function. There is no obvious difference between the following two function declarations:
Extern int F (); and int f ();
Of course, such use is still available, that is, replacing include "*. h "to declare the function. In some complicated projects, I prefer to add the extern modifier before all the function declarations.