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

Why do you need "extern C" for C++ callbacks to C functions?

I find such examples in Boost code. namespace boost { namespace { extern "C" void *thread_proxy(void *f) { .... } } // anonymous void thread::thread_start(...) { ... …
Artyom
  • 31,019
  • 21
  • 127
  • 215
18
votes
3 answers

Are extern "C" functions a separate type?

From the C++11 draft, 7.5 (para. 1): Two function types with different language linkages are distinct types even if they are otherwise identical. So I can do overload based on language linkages: extern "C" typedef void (*c_function)(); typedef…
rodrigo
  • 94,151
  • 12
  • 143
  • 190
14
votes
3 answers

Does extern "C" have any effect in C?

I just got some C code that uses extern "C" to declare external functions like this: extern "C" void func(); Is this valid C? I'm getting an error at this line, but I'm not sure if it's because of this or something else.
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
10
votes
2 answers

Could we use extern "C" in C file without #ifdef __cplusplus?

Why shouldn’t extern "C" be specified for a function that needs to be defined as a C function? What effect would that have on the compiler when compiling the file as a C source? If there is no effect on the C compiler, can’t we just define a…
Alexander
  • 450
  • 5
  • 16
10
votes
4 answers

extern and extern "C" for variables

I'm writing a C++ shared library for a C program to use. However, I have a question about extern and extern "C". Consider the following code My header file is like this: #ifdef __cplusplus extern "C" int global; extern "C" int…
Thenewstockton
  • 443
  • 6
  • 18
8
votes
4 answers

how does extern "C" allow C++ code in a C file?

In order to use C++ code in a C file, I read that we can just do extern "C" { (where the c++ code goes here)}, but when I try printing something out using cout, I keep getting an error because it does not recognize the library . I think I am just…
8
votes
3 answers

difference between extern "C" and simply extern

I have seen C/C++ code using extern "C" declared in function signatures and also while including a C header into a CPP file. but some functions just declare extern before their signature(without the "C"). QN1: are these both ways of defining…
Mohamed Iqzas
  • 976
  • 1
  • 14
  • 19
8
votes
1 answer

C++'s extern-"C" functionality to languages other than C

As it is known, declaring extern "C" to C++ function makes its name have C linkage, enabling C code to link. My question is - are there other programming languages we can make C++ function names have linkage to, something like extern "Lisp" or…
Reflection
  • 1,936
  • 3
  • 21
  • 39
7
votes
4 answers

How does extern "C" work in C++?

I see some code in C++ using extern "C" at the beginning of the file like this: #ifdef __cplusplus extern "C" {} #endif What does this mean? How does it work?
user707549
7
votes
2 answers

Is it safe to "play" with parameter constness in extern "C" declarations?

Suppose I'm using some C library which has a function: int foo(char* str); and I know for a fact that foo() does not modify the memory pointed to by str. It's just poorly written and doesn't bother to declare str being constant. Now, in my C++…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
7
votes
1 answer

"C linkage function cannot return C++ class" in Visual Studio 2019

I have the following function: class Foo; template struct PyArray1D{ std::size_t size; T *array; }; extern "C" PyArray1D SimulatePhotonEvents() { Foo * foo = new Foo(); return {1, foo} } However this does not…
Frank
  • 2,446
  • 7
  • 33
  • 67
7
votes
2 answers

What kinds of C++ functions can be placed in a C function pointer?

I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What…
7
votes
3 answers

extern C return class object

I want to have a plugin, with a simpler name to resolve in other C++ code. class B { }; extern "C" B foo(); // to avoid name mangling in order to be loaded by dlsym And in the other part of the program (which is also in C++ and shares the same…
csslayer
  • 317
  • 2
  • 9
5
votes
6 answers

When to use extern "C"?

I know how to use extern "C" but what are the conditions when you have to use it? extern "C" tells the C++ compiler not to perform any name-mangling on the code within the braces. This allows you to call C functions from within C++. For…
user103214
  • 3,478
  • 6
  • 26
  • 37
5
votes
3 answers

extern "C" - before or after library header includes?

I'm writing a C library, which may potentially be useful to people writing C++. It has a header which looks like this: #ifndef FOO_H_ #define FOO_H_ #include #include #include #ifdef __cplusplus extern "C"…
einpoklum
  • 118,144
  • 57
  • 340
  • 684