Extern "C" in C ++"

Source: Internet
Author: User

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;

Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules. Remember, the following statements:

Extern int;

It is just a declaration of a variable. It does not define variable A and does not allocate memory space for variable. 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;

Compilation Method without the extern "C" declaration

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:

Void Foo (int x, int y );

After the function is compiled by the C compiler, its name in the symbol library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, 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 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.

We can use a single sentence to summarize the true purpose of the extern "C" statement to implement mixed programming of C ++ and C and other languages.

2. Usage of extern "C"

(1) To reference functions and variables in C language in C ++, the following processing must be performed when the C Language header file (assumed as cexample. h) is included:

Extern "C"
{
# Include "cexample. H"
}
The source code of the three files in the example project of C ++ referenced by the author 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 */
# Include "cexample. H"
Int add (int x, int y)
{
Return X + Y;
}
// C ++ implementation file, call Add: cppfile. cpp
Extern "C"
{
# Include "cexample. H"
}
Int main (INT argc, char * argv [])
{
Add (2, 3 );
Return 0;
}

(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:

// 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
/* Compilation errors: # include "cexample. H "*/
Extern int add (int x, int y );
Int main (INT argc, char * argv [])
{
Add (2, 3 );
Return 0;
}

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.