Basic explanation
Extern can be placed in a variable orFunctionTo indicate variables orFunctionIn other files, prompting the compiler to encounter this variable andFunctionFind its definition in other modules.
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,ProgramThe Operation will tell you illegal access. The reason is thatPointerIt is not equivalent to an array of type T. Extern char * A declaresPointerVariable instead of 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.
3 problem: extern function 1
it is often seen that extern is placed in before the function , it becomes part of the function declaration. Then, what is the role of the keyword extern of C language in the declaration of function ?
answer and analysis:
If the function Declaration contains the keyword extern, it just implies that this function may be defined in other source files and has no other effect. There is no obvious difference between the following two Functions Declaration:
extern int F (); and int F ();
of course, this is useful, that is, replacing include "*. h "to declare Functions . In some complex projects, I prefer to add the extern modifier before all Functions declaration.
4 problem: externFunction2
WhenFunctionUnilaterally modified by the providerFunctionIf the user does not know how to use the original extern statement during the prototype, 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, there is no perfect solution in the industry to deal with this situation. The common practice is that the provider provides externalInterfaceAnd then the caller includes the header file to save the extern step. 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"
InC ++CFunctionThe compiler often fails to find C in the OBJ module.FunctionDefinition, which leads to a link failure. How can this problem be solved?
answer and analysis:
C ++ to solve the function polymorphism problem during compilation, the function and parameters are combined to generate an intermediate function name, but C language does not, therefore, the corresponding function cannot be found during the link. In this case, C function requires the use of extern" C "for Chain This tells the compiler to keep my name. Do not generate the function name for the link.
The following is a standard syntax:
// In. hfile header
# ifdef _ cplusplus
# If _ cplusplus
extern "C" {
# endif /* _ cplusplus */
...
...
//. h. End of the file
# ifdef _ cplusplus
# If _ cplusplus
}< br> # endif
# endif/* _ cplusplus * /