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
2101
votes
18 answers

What is the effect of extern "C" in C++?

What exactly does putting extern "C" into C++ code do? For example: extern "C" { void foo(); }
Litherum
  • 22,564
  • 3
  • 23
  • 27
392
votes
4 answers

Combining C++ and C - how does #ifdef __cplusplus work?

I'm working on a project that has a lot of legacy C code. We've started writing in C++, with the intent to eventually convert the legacy code, as well. I'm a little confused about how the C and C++ interact. I understand that by wrapping the C…
dublev
  • 4,257
  • 4
  • 18
  • 12
150
votes
11 answers

Why do we need extern "C"{ #include } in C++?

Why do we need to use: extern "C" { #include } Specifically: When should we use it? What is happening at the compiler/linker level that requires us to use it? How in terms of compilation/linking does this solve the problems which…
Landon
  • 15,166
  • 12
  • 37
  • 30
140
votes
9 answers

Why can't C functions be name-mangled?

I had an interview recently and one question asked was what is the use of extern "C" in C++ code. I replied that it is to use C functions in C++ code as C doesn't use name-mangling. I was asked why C doesn't use name-mangling and to be honest I…
Engineer999
  • 3,683
  • 6
  • 33
  • 71
112
votes
6 answers

How to call C++ function from C?

I know this. Calling C function from C++: If my application was in C++ and I had to call functions from a library written in C. Then I would have used //main.cpp extern "C" void C_library_function(int x, int…
claws
  • 52,236
  • 58
  • 146
  • 195
110
votes
4 answers

Call a C function from C++ code

I have a C function that I would like to call from C++. I couldn't use "extern "C" void foo()" kind of approach because the C function failed to be compiled using g++. But it compiles fine using gcc. Any ideas how to call the function from C++?
Dangila
  • 1,434
  • 3
  • 14
  • 25
41
votes
6 answers

Is extern "C" only required on the function declaration?

I wrote a C++ function that I need to call from a C program. To make it callable from C, I specified extern "C" on the function declaration. I then compiled the C++ code, but the compiler (Dignus Systems/C++) generated a mangled name for the…
bporter
  • 3,572
  • 4
  • 24
  • 23
40
votes
7 answers

What does mean for a name or type to have a certain language linkage?

According to (c) ANSI ISO/IEC 14882:2003, page 127: Linkage specifications nest. When linkage specifications nest, the innermost one determines the language. A linkage specification does not establish a scope. A linkage-specification shall occur…
artyom.stv
  • 2,097
  • 1
  • 24
  • 42
37
votes
7 answers

When to use extern "C" in simple words?

Maybe I'm not understanding the differences between C and C++, but when and why do we need to use extern "C" { ? Apparently its a "linkage convention". I read about it briefly and noticed that all the .h header files included with MSVS surround…
Russel
  • 399
  • 1
  • 3
  • 3
33
votes
4 answers

Can C++ functions marked as Extern "C" throw?

I've got C++ functions that I want to declare using extern "C" even though they are only called in C++ code. Yes, I know this is strange but it's something I would like to do for consistency since we have mixed C and C++ declarations. I just want…
Will Brode
  • 1,026
  • 2
  • 10
  • 27
32
votes
9 answers

How does an extern "C" declaration work?

I'm taking a programming languages course and we're talking about the extern "C" declaration. How does this declaration work at a deeper level other than "it interfaces C and C++"? How does this affect the bindings that take place in the program as…
samoz
  • 56,849
  • 55
  • 141
  • 195
25
votes
2 answers

When to use extern "C" in C++?

Possible Duplicate: Why do we need extern “C”{ #include } in C++? I have often seen programs coded like: extern "C" bool doSomeWork() { // return true; } Why do we use an extern "C" block? Can we replace this with something in C++?…
pankajt
  • 7,642
  • 12
  • 39
  • 60
25
votes
10 answers

Is it possible to subclass a C struct in C++ and use pointers to the struct in C code?

Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct foo* f){ return f.k; } C++ code: class bar : public foo { int my_bar() { return ret_foo( (foo)this ); } }; There's an…
Edu Felipe
  • 10,197
  • 13
  • 44
  • 41
22
votes
5 answers

static vs extern "C"/"C++"

What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because…
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
18
votes
2 answers

Is there any reason to use extern "C" on headers without methods?

I frequently come across C header files that contain extern "C" guards, but don't contain any actual functions. For example: /* b_ptrdiff.h - base type ptrdiff_t definition header */ #ifndef __INCb_ptrdiff_th #define __INCb_ptrdiff_th #ifdef…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
1
2 3 4 5 6