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
0
votes
3 answers

How to restrict access to static variables in C++?

I have a C-function called "count" that looks like this: void count(){ static int c = 0; printf("Counter=%i", c); c++; } Futhermore I have a vector of Cpp-objects and each object calls the "count" function. Since the counter variable is…
Miyako
  • 9
  • 3
0
votes
1 answer

error LNK2019: unresolved external symbol

I'm having trouble compiling an old MFC WIN CE embedded project in windows 7 visual studio 2010. I believe the old application was compiled in embedded Visual Studio 4 and needed mfcce211.dll. Any who, all of the source files manage to compile fine…
user4292309
-1
votes
1 answer

Calling a C file in C++ is giving errors

The reproduceable error code is: #ifdef __cplusplus extern "C" { #endif #include #include #include #include //#include //functions #define true 1 #define false 0 #ifdef _MSC_VER // define…
Himanshu
  • 21
  • 6
-1
votes
1 answer

Unresolved external class member when linking static lib from dynamic dll

MS Visual Studio 2008. This seems to be a name mangling issue, but I can't find the right search terms to come up with an answer. I have a dynamic lib that has a class in it, which is using a logging class from a static logging library. The dynamic…
JoeFish
  • 3,009
  • 1
  • 18
  • 23
1 2 3 4 5
6