Extern & quot; C

Source: Internet
Author: User

Introduction: extern "C" contains double meanings, which can be obtained literally: first, the target is "extern". Second, the target to be modified is "C. Let's explain these two meanings in detail. Meaning (1) the function or variable restricted by extern is of the extern type: a. extern variable declaration. For example, if the file a. c needs to reference the int v variable in B. c, you can declare extern int v in a. c, and then you can reference the variable v. Note that the link property of the referenced variable v must be external, that is,. c. the declaration of extern int v in c also depends on whether the variable v itself can be referenced. This involves another C language topic-variable scope. Variables that can be referenced by other modules with the extern modifier are generally global variables. It is also important that extern int v can be placed in. anywhere in c, for example, you can. in c, the function fun declares the extern int v at the beginning of the definition, and then you can reference the variable v, except that v can only be referenced in the function fun scope, this is still a problem of variable scope. Many people are concerned about this. It seems that the extern Declaration can only be used in the file scope. Www.2cto.com B. extern modifier function declaration. In essence, variables and functions are no different. The function name is a pointer to the beginning of the binary block of the function. If the file. c. Reference B. c functions, such as B. the original type of c is int fun (int mu. declare extern int fun (int mu) in c, and then you can use fun to do anything. Just like the declaration of variables, extern int fun (int mu) can be placed anywhere in a. c, not necessarily within the scope of a. c's file. The most common method for referencing functions in other modules is to include the header files of these function declarations. What is the difference between using extern and containing header files to reference functions? The reference method of extern is much simpler than that of header files! The use of extern is straightforward. to reference a function, use extern to declare a function. This is probably a manifestation of the KISS Principle! An obvious advantage of doing so is that it will accelerate the compilation (preprocessing) process of the program and save time. This difference is obvious during compilation of large C Programs. (2) the variables and functions modified by extern "C" are compiled and connected in C language. The compilation method is not added when the extern "C" declaration is used. First, let's take a look 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 voidfoo (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 mode when the instance is declared as "extern" C "is not added. In C ++, the header file of module A is as follows: // module A header file moduleA. h # ifndef MODULE_A_H # define MODULE_A_H int foo (int x, int y); # endif reference this function in Module B: // Module B implementation file moduleB. cpp # I nclude "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_H extern "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. Usage of extern "C" (1) references functions and variables in C language in C ++, and contains the C Language header file (assumed as cExample. h), the following processing is required: extern "C" {# I nclude "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_H extern int add (int x, int y); # endif/* C language implementation file: cExample. c */# I nclude "cExample. h "int add (int x, int y) {return x + y;} // c ++ implementation file, call add: cppFile. cpp extern "C" {# I nclude "cExample. h "} int main (int argc, char * argv []) {add (2, 3); return 0 ;}( note that if GCC is used for compiling, use the gcc-c option to generate cExample. o, then use g ++-o c PpFile cppFile. cpp cExample. o can generate the expected result of c ++ calling the c function. Otherwise, use g ++-o cppFile. cpp cExample. the c compiler reports an error. When cppFile. the following statement extern "C" {# I nclude "cExample is not used in the cpp file. h "} Instead of # I nclude" cExample. h "extern" C "int add (int x, int y); g ++-o cppFile. cpp cExample. in c compilation, the add function is interpreted as a symbol like _ foo_int_int in c ++.) If C ++ calls a. DLL file written in C language, when it includes the header file of. DLL or declares an interface function, add extern "C "{}. (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_H extern "C" int add (int x, int y); # endif // C ++ implementation file cppExample. cpp # I nclude "cppExample. h "int add (int x, int y) {www.2cto.com return x + y;}/* C implementation file cFile. c/* compilation errors: # I nclude "cppExample. h "*/extern int add (int x, int y); int main (int argc, char * argv []) {add (2, 3); return 0 ;}

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.