1.Introduction C ++ The original intention of creating a language is "A Better C" But this does not mean C ++ Similar C The compilation and connection methods used by global variables and functions of the language are as follows: C The language is the same. As a desire and C Compatible language, C ++ Retains the characteristics of some procedural languages (known " Ground Object not completely " ), So it can define global variables and functions that do not belong to any class. However, C ++ After all, it is an object-orientedProgramDesign Language To support function overloading, C ++ Processing of global functions and C There are obvious differences.
2.Starting from the standard header file An enterprise once gave the following interview questions: Interview Questions Why do standard header files have a structure similar to the following? # Ifndef _ incvxworksh # DEFINE _ incvxworksh # Ifdef _ cplusplus Extern "C "{ # Endif /*...*/ # Ifdef _ cplusplus } # Endif # Endif/* _ incvxworksh */ Analysis Apparently, the compilation macro in the header file "# Ifndef _ incvxworksh , # DEFINE _ incvxworksh , # Endif" To prevent the header file from being referenced repeatedly. So # Ifdef _ cplusplus Extern "C "{ # Endif # Ifdef _ cplusplus } # Endif What is the role? We will discuss it one by one below.
3.In-depth decryptionExtern "C" Extern "C" It contains a double meaning, which can be obtained literally: first, the goal of its modification is "Extern" Second, the target to be modified is "C" . Let's explain these two meanings in detail. Quilt Extern "C" The specified function or variable is Extern Type; Extern Yes C/C ++ Keyword indicating the range (visibility) of the function and global variable in the language. This keyword tells the compiler that the declared function and variable can be used in this module or other modules. Remember, the following statements: Extern int; It is just a declaration of a variable. It is not a definition of a variable. A , Not A Allocate memory space. Variable A All modules can be defined only once as a global variable. Otherwise, a connection error occurs. Generally,In the module header file, the functions and global variables referenced by this module to other modules are provided with keywords. Extern Statement . For example, if the module B To reference this module A The global variables and functions defined in A . In this way, the module B Call Module A In the compilation phase, the module B This function cannot be found, but no error is reported.Connection phaseSlave Module A Goals generated by compilationCodeFind this function. And Extern The corresponding keyword is Static The global variables and functions modified by it can only be used in this module. Therefore, a function or variable can only be used by this module and cannot be Extern "C" . Quilt Extern "C" The modified variables and functions follow C Language compilation and connection; Not added Extern "C" Statement Compilation Method First, let's take a look C ++ For C How to compile the function. As an object-oriented language, C ++ Supports function overloading, while procedural languages C Is not supported. Function is C ++ The name and C Different languages. For example, assume that the prototype of a function is: Void Foo (int x, int y ); This function is C The name of the compiled Symbol Library is _ Foo , And C ++ The compiler will generate an image _ Foo_int_int Name (different compilers may generate different names, but they all adopt the same mechanism. "Mangled name" ). _ Foo_int_int Such a name contains the function name, number of function parameters, and type information, C ++ This mechanism is used to implement function overloading. For example C ++ Medium, Function Void Foo (int x, int y) And Void Foo (int x, float y) The symbols generated by the compilation are different, and the latter is _ Foo_int_float . Similarly, C ++ In addition to local variables, the variables also support class member variables and global variables. The class member variables of the program written by the user may have the same name as the global variables. "." . 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. Not added Extern "C" Connection method when declaring Assume that C ++ Module A The header file is as follows: // Module A Header file Modulea. h # Ifndef module_a_h # Define module_a_h Int Foo (int x, int y ); # Endif In the module B Reference this function: // Module B Implementation File Moduleb. cpp # Include "modulea. H" Foo (2, 3 ); In fact, in the connection phase, the connector will A Generated target file Modulea. OBJ Searching _ Foo_int_int Such a symbol! Add Extern "C" Post-declaration compilation and Connection Methods Add Extern "C" After the declaration, the module A The header file is changed: // Module A Header file Modulea. h # Ifndef module_a_h # Define module_a_h Extern "C" int Foo (int x, int y ); # Endif In the module B Is still called in the implementation file Foo (2, 3) The result is: ( 1 )Module A Compile and generate Foo When the target code is used, no special processing is performed on its name. C Language method; ( 2 )The connector is in the module B Target Code Search Foo (2, 3) When calling, you are looking for unmodified symbolic names. _ Foo . If A Function declared Foo Is Extern "C" Type, while the module B Which Contains Extern int Foo (int x, int y) , Then the module B Module not found A And vice versa. Therefore, we can summarize it in one sentence. Extern "C" The true purpose of this statement (the birth of any syntax in any language is not random and comes from the needs of the real world. When thinking about a problem, we should not just focus on how the language is made, but also ask why it is doing so and what the motivation is, so that we can better understand many problems ): Implementation C ++ And C And other languages. Understand C ++ Medium Extern "C" The reason for setting up is analyzed in detail below Extern "C" Common tips.
4. extern "C"Usage ( 1 ) In C ++ References C Functions and variables in the language, including C Language header file (assuming Cexample. h ), The following processing is required: Extern "C" { # Include "cexample. H" } In C In the header file of the language, the external function can only be specified Extern Type, C Language not supported Extern "C" Statement, in . C The file contains Extern "C" The compilation syntax is incorrect. . Written by the author C ++ Reference C Function example project contains three filesSource codeAs follows: /* C Language header file: Cexample. H */ # Ifndef c_example_h # Define c_example_h Extern int add (int x, int y );// Note: It can be written as extern "C" int add (INT, INT ); # Endif /* C Language implementation file: Cexample. C */ # Include "cexample. H" Int add (int x, int y) { Return X + Y; } // C ++ Implementation file, call Add : Cppfile. cpp Extern "C" { # Include "cexample. H"// Note: this is not correct. If it cannot be compiled in this way, replace it with extern "C" int add (INT, INT). You can use } Int main (INT argc, char * argv []) { Add (2, 3 ); Return 0; } If C ++ Call C Language . Dll When . Dll When the header file or interface function is declared, add Extern "C "{ } . ( 2 ) In C References C ++ For functions and variables in the language, C ++ Header file needs to be added Extern "C" , But in C The declaration cannot be referenced directly in the language. Extern "C" This header file should only C In the file C ++ Defined in Extern "C" Function declaration is Extern Type. Written by the author C Reference C ++ The source code of the three files contained in the function example project is as follows: // C ++ Header file Cppexample. h # Ifndef cpp_example_h # Define cpp_example_h Extern "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 /* This will cause compilation errors: # Include "cexample. H "*/ Extern int add (int x, int y ); Int main (INT argc, char * argv []) { Add (2, 3 ); Return 0; } If you understand 3 As described in Extern "C" In the compilation and connection phases, you can truly understand C ++ Reference C Functions and C Reference C ++ Function usage. For 4 The sample code provided in this section. Pay special attention to the details.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.