I have read the usage of extern some time ago. I tried the following problems today. I defined global constants in header file 1, then, the same global variables are defined in header file 2, and then two header files are included in another CPP. A Redefinition error occurs, removing the global definition in header file 2, undeclared identifier error. Finally, the undeclared indentifier error occurs when all global variables in 2 are added with extern. After finding that header file 2 does not contain header file 1, the program is normal after modification.
This error gives us a deeper understanding of the role of extern.
In the compilation phase, the source code syntax is checked. In the connection phase, the space content is specified, for example, the position of the function in the executable program is determined. The Declaration tells the compiler that the variable or function exists, while the definition allocates space. Modern compilers generally use file-based compilation. Therefore, global variables defined in each file are transparent during compilation, that is, during compilation, the visible fields of global variables are restricted within the file. However, at the link stage, the content of each file must be "integrated". Therefore, if the global variable names defined in some files are the same, A duplicate definition error occurs at this time. Therefore, the global variable names defined in each file cannot be the same.
In the link phase, the content of each file (actually the OBJ file generated by compilation) is merged. Therefore, the global variables defined in a file are. After the link is complete, its visible scope is extended to the whole program. In this way, the global variables defined in a file can be used anywhere in the entire program. However, the undeclared identifier error may occur. This is because the visibility of the global variables defined in the file is extended to the entire program after the link is completed, while their visibility is still limited to their respective files in the compilation phase.
However, C ++ provides the global variables defined in a file. The method used in another file is called the extern keyword. Use the following: extern type variable name. In this way, you can use the global variables defined in this file in another file. Finally, do not forget to add the header file that initially defines global variables.
In addition, the usage of extern "C" may be as follows:
When using C functions in the C ++ environment, the compiler often fails to find the C function definition in the OBJ module, as a result, when the link fails, the C ++ language will combine the function name and parameters to generate an intermediate function name during compilation to solve the function polymorphism problem, but the C language will 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.