Questions tagged [extern-c]

`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 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 is mandatory; if the question cannot accept the tag, it should not have the tag either. These questions could be tagged with too, which is generally not a good idea, but these questions are about the interaction with C code. The 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

79 questions
2
votes
1 answer

Extern used twice in C++

I am very curious about what happens during linking, and, during my research in this area I have stabbed upon this code #ifdef __cplusplus extern “C” { #endif extern double reciprocal (int i); #ifdef __cplusplus } #endif The code was in some…
MinuxLint
  • 111
  • 9
2
votes
1 answer

Returning unique_ptr from a function executed via dlsym

I have a function that is located in a shared object, and is loaded and executed with dlsym from the main program. (both the shared object and the main program are C++) Is it possible that this function will return std::unique_ptr ? shared object…
Guy
  • 25
  • 3
2
votes
3 answers

extern keyword in c

Possible Duplicate: Why do we need extern “C”{ #include } in C++? many a times in our code i have seen some statements like following: extern "C" { //some code }; what does this exactly mean?
Vijay
  • 65,327
  • 90
  • 227
  • 319
2
votes
5 answers

Unable to call C++ function from a C code

I'm new to mixing C & C++ code. Understood the need of extern & __cplusplus directives after reading some SO links & online reading. Not sure why am I getting the error. Did i miss something ? C++ header: cppexh.h #include #ifdef…
codeLover
  • 3,720
  • 10
  • 65
  • 121
2
votes
2 answers

Can I have a pointer on pointer on const in C++?

Basically I am wondering whether something like f(const void* a){ // This is also what `(char**) a` would do behind the scenes char** string_a_ptr {reinterpret_cast(const_cast(a))}; // ... is possible while preserving the…
maow
  • 2,712
  • 1
  • 11
  • 25
2
votes
0 answers

linking to C++ libraries to Modelica

I'm trying to utilize a C++ library in Modelica. Modelica compilers generate c from Modelica source and then invokes a c compiler to create an executable. Modelica provides a mechanism to call c functions. I've created a…
Adam
  • 67
  • 1
  • 8
2
votes
1 answer

Passing a pointer to a C++ function in a C Project

I have a huge C Project. And now i needed a C++ function to fill some variables. With declaring the function as extern "C", it was no problem, to call the function from the C Project. The Problem is, that i need to pass a pointer to the C++…
Trikolix
  • 145
  • 4
2
votes
3 answers

How Do I layout Function Prototypes mixing C with C++

I have a header that I want to include from .c and .cpp files. so I know about name mangling and extern "C" so... #ifdef __cplusplus extern "C" { int isPrime(int64_t p); } #endif but when I include this in the .c file it doesn't see the…
Grady Player
  • 14,399
  • 2
  • 48
  • 76
1
vote
1 answer

extern "C": Why does Visual C++ behave different on conflicting linkage specs for variables vs. for functions?

If a variable is declared somewhere without extern "C" (e.g. in a header file) and afterwards defined with extern "C", then the Visual C++ compiler compiles it with C++ linkage without further notice: extern IntStruct ifcStruct; extern int …
MattTT
  • 339
  • 3
  • 9
1
vote
1 answer

extern c template instantiation

I want to write a templated function and explicitly instantiate it inside extern "C" block to avoid code duplication. Here is example of what I mean: template // my templated function T f(T val){ return val; } extern "C"{ int…
Gleb
  • 33
  • 3
1
vote
0 answers

extern "C" function returning std::array

The following code compiles with gcc but does not compile with MSVC (Godbolt link) #include extern "C" std::array foo() { return std::array{1,2,3}; } Is one of the compilers is right and the other is wrong, or, is it…
Anonymous
  • 27
  • 2
1
vote
1 answer

Resolution of overloaded extern C function with default arguments

This code: #include // int abs(int); int abs(int i = 0) { return 42; } int main() { return abs(1); // Returns 42 } Returns 42. The compiler picks the overloaded C++ function. I tested this on many versions of g++/clang. Can I rely on…
braph
  • 23
  • 4
1
vote
2 answers

Is clang++ ignoring extern "C" for some deprecation warnings?

If I use clang 3.8.1 to compile: extern "C" { int foo(int x) { register int y = x; return y; } } int main() { return foo(123); } I get the warning: a.cpp:3:18: warning: 'register' storage class specifier is deprecated and incompatible with C++1z…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
vote
1 answer

extern "C" functions in compiled object code

Environment is Microsoft Visual C++ 2015 and Windows 7. Is there anything special about inline extern "C" functions defined in a header? I am consuming an SDK in which one of the headers contain such a beast. In my application I have a lone TU…
ForeverLearning
  • 6,352
  • 3
  • 27
  • 33
1
vote
1 answer

C-callable wrapper around C++ class

I am following a tutorial to create a wrapper around C++ code, so that it can be called from C#. I get an error compiling the wrapper though. Header.h class MyClass{ public: MyClass(int x, int y); double GetSum(); private: …
Tumelo
  • 301
  • 3
  • 18