For C ++ as a language to be compatible with C, C ++ retains some procedural languages and features, names such as foo_int_int include the function name, number of function parameters, and type information. C ++ relies on this mechanism to implement function overloading.
Therefore, it can define global variables and functions that do not belong to any class. However, C ++ is an object-oriented programming language after all. To support function overloading, C ++ treats global functions differently from C. Starting from the standard header file, an enterprise once gave the following interview questions: why are there similar structures in the standard header files?
- #ifndef __INCvxWorksh
- #define __INCvxWorksh
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*...*/
- #ifdef __cplusplus
- }
- #endif
- #endif /* __INCvxWorksh */
Obviously, the compilation macro "# ifndef _ INCvxWorksh, # define _ INCvxWorksh, and # endif" in the header file is used to prevent the header file from being repeatedly referenced. So what is the role? We will discuss it one by one below.
3. the deep encryption extern "C" extern "C" contains double meanings, which can be obtained literally: first, the target to be modified is "extern". Second, the target to be modified is "C. Let's explain these two meanings in detail. The function or variable specified by extern "C" is of the extern type;
- #ifdef __cplusplus
- extern "C" {
- #endif
- #ifdef __cplusplus
- }
- #endif
Extern is the keyword that indicates the range visibility of functions and global variables in C/C ++. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules. Remember, the following statement: extern int a; is just a declaration of a variable. It is not defining variable a and does not allocate memory space for. Variable a can only be defined once as a global variable in all modules. Otherwise, a connection error occurs.
- Describes the problems and skills of the C ++ Language
- Exploring various concise and flexible features of C ++
- How to Use the Visual C ++ subset to search for a topic
- How to better learn C ++ -- and a summary of C ++ learning methods
- Advanced Programming instructions on C ++ learning methods
In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. For example, if Module B wants to reference the global variables and functions defined in module A, it only needs to include the header file of module. In this way, when Module B calls A function in module A, although Module B cannot find the function in the compilation phase, no error is reported; it will find this function from the target Code Compiled by module A in the connection phase.
The keyword corresponding to extern is static. The global variables and functions modified by extern can only be used in this module. Therefore, a function or variable can only be used by this module and cannot be modified by extern "C. As an object-oriented language, modified by extern "C" supports function overloading, while Procedural Language C does not. The name of the function in the symbol library after being compiled by C ++ is different from that in the C language. For example, assume that the prototype of a function is:
- // Module A header file moduleA. h
- # Ifndef MODULE_A_H
- # Define MODULE_A_H
- Int foo (int x, int y );
- # Endif
After the function is compiled by the C compiler, its name in the symbol library is _ foo, while the C ++ compiler may generate different compilers with different names, such as _ foo_int_int, but all adopt the same mechanism, and the new name is called "mangled name ").
A name such as foo_int_int contains the function name, number of function parameters, and type information. C ++ relies on this mechanism to implement function overloading. For example, in C ++, the void foo (int x, int y) and void foo (int x, float y) functions generate different symbols, the latter is _ foo_int_float.
Similarly, variables in C ++ support both class member variables and global variables in addition to local variables. The class member variables of the program written by the user may have the same name as the global variables, which are distinguished. In essence, the compiler uses a unique name for the variables in the class when compiling, similar to the function processing. This name is different from the global variable name with the same name in the user program.