The intent of extern "C" in C + + is to implement mixed programming between C + + and C and other languages.
C + + to support overloading of functions. C + + has a distinct difference in the way global functions are handled.
For the function void foo (int x, int y), the function is compiled by the C compiler in the symbol library with the name _foo. The C + + compiler produces names like _foo_int_int.
extern is a keyword that indicates the scope (visibility) of functions and global variables in the C + + language. The keyword tells the compiler. Its declared functions and variables can be used in this module or in other modules.
Usually. In the header file of the module, the functions and global variables that this module provides to other modules are Keywordextern declared. For example, if module B is to refer to the global variables and functions defined in module A, simply include the header file of module A. In this way, when the function in module A is called in Module B, in the compile phase, Module B cannot find the function. But there is no error. It finds this function in the connection phase from the target code generated by module a compilation.
The keyword corresponding to extern is static. The global variables and functions that are modified by it 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.
Variables and functions modified by extern "C" are compiled and concatenated according to the C language method.
For example, if you are in C + +:
Module a header file moduleA.h
int foo (int x, int y);
reference the function in module B:
ModuleB.cpp
#include "ModuleA.h"
Foo (2,3);
in fact, during the connection phase. The connector will look for _foo_int_int this symbol from the target file Modulea.obj generated by module A!
After adding the extern "C" declaration, the header file of module a becomes:
Module a header file moduleA.h
extern "C" int foo (int x, int y);
foo (2,3) is still called in the implementation file of Module B, and the result is:
(1) When module a compiles the target code for foo. No special treatment of its name, the use of C language way;
(2) When the connector looks for foo (2,3) call for the target code for module B. Looking for an unmodified symbol name _foo. Suppose that the function in module a declares that Foo is an extern "C" type, and that module B includes an extern int foo (int x, int y). Module B cannot find the function in module A, and vice versa.
in the header file of the C language, only the extern type can be specified for its external function. extern "C" declarations are not supported in the C language. A compile syntax error occurs when the extern "C" is included in the. c file. When referencing functions and variables in the C + + language. C + + header file needs to be added extern "C", but in C language can not directly reference the declaration of the extern "C" of the header file. The extern "C" function defined in C + + should only be declared as an extern type in the file. For example://c++ Header File A.h
extern "C" int foo (int x, int y);
C + + implementation file A.cpp
#include "A.h"
int foo (int x, int y) {return x + y;}
/ * C Implementation file B.C
This compiles an error: #include "a.h" */
extern int foo (int x, int y);
int main (int argc, char* argv[]) {foo (2, 3); return 0 ; }
extern " c" The meaning of:???? Enables mixed programming of C + + and C and other languages