`extern "C"` is used to indicate that C++ functions and variables should have C linkage instead of C++ linkage, allowing C and C++ modules to interact with each other. The extern-c tag should only be used on C++ questions where the subject of discussion is the behaviour of declarations with 'extern "C"'.
The extern-c tag should be used on C++ questions where the subject of discussion is the behaviour of code with extern "C"
applied to function declarations.
It should only be used on C++ questions — the tag c++ is mandatory; if the question cannot accept the c++ tag, it should not have the extern-c tag either. These questions could be tagged with c too, which is generally not a good idea, but these questions are about the interaction with C code. The c tag is not required.
extern "C"
is used to indicate that C++ functions and variables should have C linkage instead of C++ linkage, allowing C and C++ modules to interact with each other; specifically, it applies C linkage to function types, function names, and variable names.
Function types with C linkage represents calling convention, and causes the compiler to use C calling conventions instead of C++ ones (if applicable); this is independent of function names with C linkage. This also allows function pointers to specify whether they point to C or C++ functions.
typedef void CppFunc(); // void() C++ function type. extern "C" typedef void CFunc(); // void() C function type. CppFunc* cppCppFuncPtr; // Pointer with C++ linkage, to function with C++ linkage. extern "C" CppFunc* cCppFuncPtr; // Pointer with C linkage, to function with C++ linkage. CFunc* cppCFuncPtr; // Pointer with C++ linkage, to function with C linkage. extern "C" CFunc* cCFuncPtr; // Pointer with C linkage, to function with C linkage.
Function and variable names with C linkage represent name mangling; this is independent of function types with C linkage. C names are subject to minimal or no name mangling, depending on the compiler; the most well-known example of this is MSVC adding calling convention information to C function names, and a leading underscore to C variable names.
Declaring functions as extern "C"
allows C++ modules to define functions that can be called from C modules, and C++ modules to connect to functions defined in C modules. Similarly, declaring global or namespace variables as extern "C"
allows C++ modules to define variables that can be used in C modules, and C++ modules to connect to variables defined in C modules. [Class member declarations always have C++ linkage, even if the declarations appear inside an extern "C"
block.]
extern "C"
causes the compiler to treat extern "C"
functions and variables as if they were all in the same namespace when compiled, regardless of their actual namespace; two extern "C"
function declarations with the same unqualified name, or two extern "C"
variables with the same name, always refer to the same entity, regardless of whether they're in the same namespace or not. Similarly, an extern "C"
function cannot have the same name as an extern "C"
variable, regardless of whether they're in the same namespace or not. In effect, it applies the C compiling algorithm to the declaration instead of the C++ algorithm, generating a symbol identical to if the function or variable was in a C module.
extern "C"
functions are allowed to contain C++ code within them, and will execute properly even if called from C code.
For more information on linkage and extern "C"
, see the cppreference page