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
1
vote
1 answer

specifying argument default value in function definition causes error C2143: syntax error : missing ')' before '='

I have following declaration: DLL EntityHandle scenemanager_create_entity (SceneManagerHandle handle, const char* name, const char* mesh_name, const char* group_name = 0); where last agrument has default value group_name = 0. When I…
happy_marmoset
  • 2,137
  • 3
  • 20
  • 25
1
vote
3 answers

Is it possible for a C method not be able to be included in a C++ project?

I know I can include C methods inside a C++ project using the extern "C" thing. But lets us now suppose that I'm thinking in creating a C++ project that would use quite a lot of C methods coming from both libraries made by me as well as libraries…
Momergil
  • 2,213
  • 5
  • 29
  • 59
1
vote
2 answers

Unresolved externals and extern "C"?

As new as I am to C++, I don´t fully understand this linking and stuff. And I think this is about extern "C" linking. extern "C" { int loadbmp(char *filename, unsigned char **buf, int *w, int *h, int pf, int bottomup); const char…
Zerowalker
  • 761
  • 2
  • 8
  • 27
0
votes
1 answer

Including extern c header results in broken preprocessor output

I have a source code structured as follows foo.h (header from c library) #pragma once struct foo {...}; bar.h #pragma once extern "C" { #include "foo.h" } class Bar { foo val; ... }; bar.cpp // some Bar methods…
Roman
  • 1,396
  • 4
  • 15
  • 39
0
votes
0 answers

How can I check whether a function is extern-C?

In C++, can I check whether a certain function has been declared "extern C" or not? I don't mind checks restricted to the current translation unit. I'd like to check this at compile-time, but if this is somehow possible only at run-time, that's…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
0
votes
1 answer

How common is it to mix C with C++?

I wanted to know how widely used is the mixing of C++ and C. I mean as in using of C libraries/functions and call it in C++ program like how it is done here Mix C with C++. How extensive is its use in real world? Is it rarely used, avoided mostly or…
0
votes
0 answers

c++ LoadLibrary and GetProcAddress from extern DLL

I´ve got an extern DLL to work with. I try to load the DLL with LoadLibrary and get the function by calling GetProcAddress but everytime I debug the programm I got an exception, which I can ignore by windows but in the end my main method throws the…
Felix Arnold
  • 839
  • 7
  • 35
0
votes
0 answers

Segmentation fault when compiling with mingw g++ 7.2, declaring an ifstream and using extern "C"

I'm was writing some interfacing code to call c++ routines from fortran which is why I dumped the methods into an extern "C" block. When compiling with g++.exe (x86_64-win32-seh-rev1, Built by MinGW-W64 project) 7.2.0 under Windows 10 I was getting…
Peter L.
  • 1
  • 1
  • 2
0
votes
2 answers

How do I hand over a pointer to a non-static member method to an extern "C" function?

I've got a class named tmc, which now contains (among other things, which are not relevant here) a private constructor, a static factory method, and an instance method named ReadLoop (void*): extern "C" { #include } class tmc…
Neppomuk
  • 1,102
  • 1
  • 11
  • 24
0
votes
2 answers

Passing a pointer to a function as an argument to a function

Just wondering if anyone can give me some advice regarding where I'm going wrong here. My program works OK if I run it as is, but as soon as I swap the commented line with the one below it, I get errors. My goal is to be able to use the commented…
0
votes
0 answers

Calling C++ code from C using inbuilt c++ classes and templates

So I want to use string class and multiset template of C++. My original code interfaces from Python to C using ctypes and now I am trying to interface C to C++. (If there is any direct interfacing between Python & C++, I would welcome the suggestion…
user11332264
0
votes
1 answer

Check whether function called through function-pointer has a return statement

We have a plugin system that calls functions in dlls (user-generated plugins) by dlopening/LoadLibrarying the dll/so/dylib and then dlsyming/GetProcAddressing the function, and then storing that result in a function pointer. Unfortunately, due to…
Philipp
  • 957
  • 1
  • 6
  • 20
0
votes
1 answer

VS2013 C++ Compiler Mangling name defined with extern "C"

I'm trying to build a WIN32 console app that uses the current 2.12.28 ftd2xx.lib static library from FTDI. I'm using VS2013 and native unmanaged C++. My call looks like this. #include "../ftd2xx.h" . . . DWORD port_count = 0; FT_STATUS…
JonN
  • 2,498
  • 5
  • 33
  • 49
0
votes
2 answers

Should you use extern "C" in a file that only has defines?

Is it pointless to have: #ifdef __cplusplus extern "C" { #endif // code... #ifdef __cplusplus } #endif Where "code..." is just a bunch of defines and typedefs (no includes, etc.)?
flmng0
  • 387
  • 1
  • 2
  • 14
0
votes
0 answers

Can't push another viewcontroller when passing data from unity3d to iOS

I have a bridge between unity 3d and iOS to pass data. I can sent data successfully from unity 3d to my iOS using extern "C", then i can call a objective c method from a method of extern "c". But from the objective c method, if i want to push…
Nuibb
  • 1,800
  • 2
  • 22
  • 36