namespace someNameSpace { extern "C" void doSomething() { someOperations(); } }
I want to run doSomething()
in both C++ and C environment.
Is someNameSpace
still encapsulating doSomething()
if I expose it to extern "C"
linkage?
Is there a good way to share functions between C++ and C while avoiding polluting global namespace on C++ side?
Edit: Because this code is primarily used in C++ mode, while the C linkage is for test use only, I guess this is a better way to do it.
namespace someNameSpace { #ifdef COMPILE_FOR_C_LINKAGE extern "C" #else extern "C++" #endif { void doSomething() { someOperations(); } } }
By declaring a function with extern "C" , it changes the linkage requirements so that the C++ compiler does not add the extra mangling information to the symbol. This pattern relies on the presence of the __cplusplus definition when using the C++ compiler. If you are using the C compiler, extern "C" is not used.
extern "C" is a linkage specification which is used to call C functions in the Cpp source files. We can call C functions, write Variables, & include headers. Function is declared in extern entity & it is defined outside. Syntax is. Type 1: extern "language" function-prototype.
Using extern "C" lets the compiler know that we want to use C naming and calling conventions. This causes the compiler to sort of entering C mode inside our C++ code. This is needed because C++ compilers mangle the names in their symbol table differently than C compilers and hence behave differently than C compilers.
The extern “C” keyword is used to make a function name in C++ have the C linkage. In this case the compiler does not mangle the function. Let us see what is the mangling in C++ first, then we can discuss about the extern “C” keyword. In C++ we can use the function overloading feature.
Your code works, but you should beware that all functions that have extern "C"
linkage share the same space of names, but that is not to be confused with the C++ notion of "namespace": Your function is really someNameSpace::doSomething
, but you cannot have any other extern "C"
function with unqualified name doSomething
in any other namespace.
See 7.5/6:
At most one function with a particular name can have C language linkage. Two declarations for a function with C language linkage with the same function name (ignoring the namespace names that qualify it) that appear in different namespace scopes refer to the same function. Two declarations for a variable with C language linkage with the same name (ignoring the namespace names that qualify it) that appear in different namespace scopes refer to the same variable. An entity with C language linkage shall not be declared with the same name as a variable in global scope, unless both declarations denote the same entity; no diagnostic is required if the declarations appear in different translation units. A variable with C language linkage shall not be declared with the same name as a function with C language linkage (ignoring the namespace names that qualify the respective names); no diagnostic is required if the declarations appear in different translation units. [Note: Only one definition for an entity with a given name with C language linkage may appear in the program (see 3.2); this implies that such an entity must not be defined in more than one namespace scope. — end note]
Your company's or project's global style arbiters should be able to advise you on a suitable naming policy for your code base.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With