Reprinted-extern "C" usage parsing extern "C" usage Parsing

Source: Internet
Author: User
Analysis on the usage of http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.htmlextern "C" 1. IntroductionC ++ language was originally created as "a better C ", however, this does not mean that the compilation and connection methods used by global variables and functions similar to C in C ++ are the same as those in C. As a language to be compatible with C, C ++ retains the features of some procedural languages (known as "not completely oriented objects "), 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. 2. Starting from the standard header fileAn enterprise once gave the following interview questions: why are the standard header files having a structure similar to the following? # Ifndef _ incvxworksh # DEFINE _ incvxworksh # ifdef _ cplusplus extern "C" {# endif /*... */# ifdef _ cplusplus} # endif/* _ incvxworksh */The analysis is clear, 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 # ifdef _ cplusplusextern "C" {# endif # ifdef _ cplusplus} # What is the role of endif? We will discuss it one by one below. 3. In-depth decryption Extern "C"Extern "C" contains a double meaning, which can be obtained literally: first, the target is "extern", and second, the target is "C. Let's explain these two meanings in detail. The function or variable specified by extern "C" is of the extern type, and extern is a keyword in C/C ++ that indicates the range (visibility) of the function and global variable, this keyword tells the compiler that 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. 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 it 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. Variables and functions modified by extern "C" are compiled and connected in C language; the compilation method without the extern "C" declaration first looks at how C-like functions are compiled in C ++. As an object-oriented language, 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 void Foo (int x, int y). After the function is compiled by the C compiler, its name in the symbol library is _ Foo, the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, but all adopt the same mechanism, 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 local variables and 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, 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. The connection method when the extern "C" statement is not added is assumed 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 this function in Module B: // Module B implementation file moduleb. CPP # include "modulea. H "Foo (2, 3); in fact, in the connection phase, the connector will generate the target file modulea from module. search for symbols like _ foo_int_int in OBJ! After the compilation and connection method after the extern "C" declaration is added, the header file of module A is changed to: // 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 Module B's implementation file. The result is: (1) When module A compiles the foo target code, it does not perform special processing on its name and uses the C language. (2) when the connector looks for a FOO (2, 3) call for the target code of Module B, it looks for the unmodified symbol name _ Foo. If the function in module A declares that foo is of the extern "C" type, and Module B contains the extern int Foo (INT X, int y ), module B cannot find the function in module A, and vice versa. Therefore, we can summarize the true purpose of the statement "extern" C "in one sentence (the birth of any syntax feature 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 ): realize mixed programming of C ++, C and other languages. Understand the motivation for setting up extern "C" in C ++. Next we will analyze the common usage skills of extern "C.   4. extern "C" Usage(1) reference functions and variables in C language in C ++, and include the C Language header file (assumed as cexample. h), the following processing is required: extern "C" {# include "cexample. H "} in the header file of C language, the external function can only be specified as the extern type. The C language does not support the extern" C "declaration. when the c file contains extern "C", a compilation syntax error occurs. The source code of the three files included in the example project of C function referenced by the author C ++ is as follows:/* C header file: cexample. H */# ifndef c_example_h # define c_example_hextern int add (int x, int y); // Note: write it as extern "C" int add (INT, INT ); alternatively, you can # endif/* C 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 correct. If the compilation fails, replace it with extern" C "int add (INT, INT). You can use} int main (INT argc, char * Rgv []) {Add (2, 3); Return 0;} if C ++ calls a C language. DLL, when. extern "C" {} should be added when the DLL header file or interface function is declared "{}. (2) When referencing functions and variables in C ++ in C, the header file of C ++ needs to add extern "C ", however, you cannot directly reference this header file that declares extern "C" in C. You should only declare the extern "C" function defined in C ++ as the extern type. The source code of the three files contained in the example project of C ++ is as follows: // the header file cppexample of C ++. 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/* 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 have thoroughly understood what extern "C" described in section 3rd plays in the compilation and connection phases Function. Pay special attention to the sample code given in section 4th. 1. IntroductionC ++ language was originally created as "a better C ", however, this does not mean that the compilation and connection methods used by global variables and functions similar to C in C ++ are the same as those in C. As a language to be compatible with C, C ++ retains the features of some procedural languages (known as "not completely oriented objects "), 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. 2. Starting from the standard header fileAn enterprise once gave the following interview questions: why are the standard header files having a structure similar to the following? # Ifndef _ incvxworksh # DEFINE _ incvxworksh # ifdef _ cplusplus extern "C" {# endif /*... */# ifdef _ cplusplus} # endif/* _ incvxworksh */The analysis is clear, 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 # ifdef _ cplusplusextern "C" {# endif # ifdef _ cplusplus} # What is the role of endif? We will discuss it one by one below. 3. In-depth decryption Extern "C"Extern "C" contains a double meaning, which can be obtained literally: first, the target is "extern", and second, the target is "C. Let's explain these two meanings in detail. The function or variable specified by extern "C" is of the extern type, and extern is a keyword in C/C ++ that indicates the range (visibility) of the function and global variable, this keyword tells the compiler that 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. 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 it 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. Variables and functions modified by extern "C" are compiled and connected in C language; the compilation method without the extern "C" declaration first looks at how C-like functions are compiled in C ++. As an object-oriented language, 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 void Foo (int x, int y). After the function is compiled by the C compiler, its name in the symbol library is _ Foo, the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, but all adopt the same mechanism, 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 local variables and 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, 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. The connection method when the extern "C" statement is not added is assumed 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 this function in Module B: // Module B implementation file moduleb. CPP # include "modulea. H "Foo (2, 3); in fact, in the connection phase, the connector will generate the target file modulea from module. search for symbols like _ foo_int_int in OBJ! After the compilation and connection method after the extern "C" declaration is added, the header file of module A is changed to: // 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 Module B's implementation file. The result is: (1) When module A compiles the foo target code, it does not perform special processing on its name and uses the C language. (2) when the connector looks for a FOO (2, 3) call for the target code of Module B, it looks for the unmodified symbol name _ Foo. If the function in module A declares that foo is of the extern "C" type, and Module B contains the extern int Foo (INT X, int y ), module B cannot find the function in module A, and vice versa. Therefore, we can summarize the true purpose of the statement "extern" C "in one sentence (the birth of any syntax feature 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 ): realize mixed programming of C ++, C and other languages. Understand the motivation for setting up extern "C" in C ++. Next we will analyze the common usage skills of extern "C.   4. extern "C" Usage(1) reference functions and variables in C language in C ++, and include the C Language header file (assumed as cexample. h), the following processing is required: extern "C" {# include "cexample. H "} in the header file of C language, the external function can only be specified as the extern type. The C language does not support the extern" C "declaration. when the c file contains extern "C", a compilation syntax error occurs. The source code of the three files included in the example project of C function referenced by the author C ++ is as follows:/* C header file: cexample. H */# ifndef c_example_h # define c_example_hextern int add (int x, int y); // Note: write it as extern "C" int add (INT, INT ); alternatively, you can # endif/* C 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 correct. If the compilation fails, replace it with extern" C "int add (INT, INT). You can use} int main (INT argc, char * Rgv []) {Add (2, 3); Return 0;} if C ++ calls a C language. DLL, when. extern "C" {} should be added when the DLL header file or interface function is declared "{}. (2) When referencing functions and variables in C ++ in C, the header file of C ++ needs to add extern "C ", however, you cannot directly reference this header file that declares extern "C" in C. You should only declare the extern "C" function defined in C ++ as the extern type. The source code of the three files contained in the example project of C ++ is as follows: // the header file cppexample of C ++. 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/* 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 have thoroughly understood what extern "C" described in section 3rd plays in the compilation and connection phases Function. Pay special attention to the sample code given in section 4th.
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.