1. IntroductionThe C + + language was originally created as "A better C", but this does not mean that C + + global variables and functions in C + + are compiled and connected in exactly the same way as C. As a language compatible with C, C + + retains some of the features of the procedural language (known as "not completely object-oriented"), so it can define global variables and functions that are not part of any class. However, after all, C + + is an object-oriented programming language, in order to support the overloading of functions, C + + to the global function of the processing method and C are obviously different.
2. Speaking from the standard header fileAn enterprise once gave the following interview question: Why do standard header files have a structure similar to the following? #ifndef __incvxworksh #define __incvxworksh #ifdef __cplusplus extern "C" {#endif/*...*/#ifdef __CPLU Splus} #endif #endif/* __INCVXWORKSH * * Analysis Obviously, the compiler macro in the header file "#ifndef __incvxworksh, #define __incvxworksh, #endif" Used to prevent the header file from being repeatedly referenced. So what is the role of #ifdef __cplusplusextern "C" {#endif #ifdef __cplusplus} #endif? We will be in the following one by one lanes.
3. Deep disclosure of extern "C" The extern "C" contains a double meaning, which is literally: first, the target being modified by it is "extern", and secondly, the target it modifies is "C". Let's take a detailed reading of this twofold meaning. The function or variable defined by extern "C" is an extern type; extern is a keyword that indicates the scope (visibility) of functions and global variables in the C/D + + language, which tells the compiler that its declared functions and variables can be used in this module or in other modules. Remember, the following statement: extern int A; is simply a declaration of a variable that does not define variable A and does not allocate memory space for a. Variable A can only be defined once in all modules as a global variable, or a connection error occurs. Typically, the function and global variables that this module provides to other modules in the header file of the module are declared with the keyword extern. For example, if module B is to reference the global variables and functions defined in module A, only the header file of module A can be included. In this way, when a function in module A is called in Module B, in the compile phase, Module B cannot find the function, but it does not error; it will find this function in the target code generated from module a during the connection phase. The keyword that corresponds to extern is static, and the global variables and functions it modifies can only be used in this module. Therefore, a function or variable may not be modified by extern "C" only if it is used by this module. The variables and functions modified by extern "C" are compiled and concatenated according to the C language, and the way to compile without extern "C" is to first look at how C + + is compiled for a function that is similar to c.. As an object-oriented language, C + + supports function overloading, whereas programming language C is not supported. Functions are compiled in C + + with different names in the symbol library than in the C language. For example, suppose a function is prototyped as: void foo (int x, int y); The function is compiled by the C compiler in the symbol library with the name _foo, while the C + + compiler produces names like _foo_int_int (different compilers may generate different names, but all use the same mechanism, and the resulting new name is called "Mangled Name"). _foo_int_int such a name includes the function name, function parameter number and type information, C + + is this mechanism to implement function overloading. For example, in C + +, the function void foo (int x, int y) is not the same as the symbol generated by the compilation of void foo (int x, float y), which is _foo_int_float. Similarly, variables in C + + support a class in addition to local variablesmember variables and global variables. The class member variable of the program that the user writes may have the same name as the global variable, and we use the "." to differentiate. In essence, the compiler, when compiling, is similar to the processing of the function and takes a unique name for the variable in the class, which differs from the name of the global variable named in the user program. Connection method without extern "C" declaration assuming that in C + +, the header file of module A is as follows:// module a header file Modulea.h#ifndef module_a_h#define module_a_hint foo (int x, int y ); #endif reference the function in module B:// Module B implements the file Moduleb.cpp#include "ModuleA.h" foo (2,3); In fact, during the connection phase, the connector looks for symbols like _foo_int_int from the target file Modulea.obj generated by module A! After adding extern "C" declaration after the compilation and connection method plus extern "C" declaration, the header file of module a becomes:// module a header file Modulea.h#ifndef module_a_h#define Module_a_hextern "c "int foo (int x, int y), #endif still calls Foo (2,3) in the implementation file of Module B, and the result is: (1) When module a compiles the target code of Foo, it does not have special handling of its name, the C language is used, and (2) the connector is in module B The target code looks for the Foo (2,3) call, looking for the unmodified symbol name _foo. If the function in module a declares Foo to be an extern "C" type, and Module B contains an extern int foo (int x, int y)  , then module B cannot find the function in module A, and vice versa. Therefore, it is possible to summarize the true purpose of the extern "C" statement in one sentence (the birth of any grammatical feature in any language is not arbitrary and is driven by demand from the real world.) When we're thinking about a problem, we can't just stay in the language, ask why it's doing it, what the motivation is, so we can understand a lot more in depth: Implementing a mixed programming of C + + and C and other languages. Understand the C + + in the establishment of the extern "C" motivation, we are below to specifically analyze the extern "C" the usual use of skills.
4.extern "C" Idioms (1) in C + + references to functions and variables in C, in the C language header file (assuming cExample.h), the following processing is required: extern "C" {#include "cExample.h"} and in the C language header file, extern "C" declarations are not supported for external functions except for extern types, and compile syntax errors occur when the. c file contains extern "C". I write C + + reference C Function Example project contains the source code of the three files are as follows:/* C language header file: cExample.h */#ifndef c_example_h#define c_example_hextern int Add (int x,int y); //Note: written as extern "C" int add (int, int); You can also #endif/* the C language implementation file: CEXAMPLE.C */#include "cExample.h" int Add (int x, int y) {return x + y;} C + + implementation file, call Add:cppFile.cppextern "C" {#include "cExample.h" // Note: This is not appropriate, if this compile pass, instead, replace with extern "C" int add (int, int); Can be passed}int main (int argc, char* argv[]) {Add (2,3); return 0;} If C + + calls a. dll written in C, the extern "C" {} should be added when the header file for the. dll is included or the interface function is declared. (2) When referencing functions and variables in the C + + language, the header file of C + + must be added extern "C", but the header file that declares extern "C" cannot be directly referenced in C, only the extern "C" defined in C + + should be The function is declared as an extern type. The author of the C C + + Function Example project contains the source code for the three files as follows://c++ header file cppexample.h#ifndef cpp_example_h#define cpp_example_hextern "C" int Add (int x, int y); #endif//c++ implementation file cppexample.cpp#include "cppExample.h" int Add (int x, int y) {return x + y;} /* C Implementation file cFile.c/* Compile error: #include "cExample.h" */extern int Add (int x, int y), int main (int argc, char* argv []) {Add (2, 3); return 0;} An in-depth understanding of the role of the extern "C" in the compile and join phases described in section 3rd will give you a real understanding of the idioms described in this section from C + + to reference C functions and C. For the example code given in section 4th, you need to pay special attention to each detail.
Why is there a requirement for extern "C"?