How & why to use extern “C” to disable name mangling in C++

When mixing C and C++ (i.e., a. calling C function from C++; and b. calling C++ function from C), the C++ name mangling causes linking problems. Technically speaking, this issue happens only when the callee functions have been already compiled into binary (most likely, a *.a library file) using the corresponding compiler.

For a better understanding the issue, we start from:

Call C functions from C++





To establish a mixed compiling environment, we compile the object files one by one:

Then we link:



We define the C function as extern "C" to make it callable from a C++ program.


Then we run the above compile/link commands:



We examine the binary to better understand why:

Firstly we look at pure.o:


Secondly, we look at taint.o using the problematic version pure.h:


As given in the outputs, the actual function names of print do not match from the caller (in C++, __Z5printv ) and the callee(in C, _print ).

If we look at the taint.o using the modified version of pure.h:

This is how the problem is solved.

Call C++ functions from C

This time, I will skip problem and principle but give the solution only.




compile & run:


Best practice

name mangling in a word

C++ introduces name mangling to support something that does not exist in C, namely, function overloading, member function etc.

So when calling C++ functions from C, if the functions fall into the categories above, wrapper functions defined as extern "C" should be used.

Self-defined C library

To enable your C library to be used directly by C++ projects, surround the function declaring code with:

We use #ifdef __cplusplus as extern "C" notation can be used only in C++.

3rd party C library

Most likely, nothing is required as the above technique has been applied already. If now, include the header files this way:

So all the functions can be declared as extern "C".



Callback class for member functions in C++

This article is derived from the post. It describes a generalized class that can represent C++ member functions as callbacks, like a function pointer in C. In the original post: these callbacks are called as “delegate”s but I will keep using the term callback.

In traditional C, function pointers are used for callbacks. However, In C++, the definition of a callback is a bit more complex because most functions (member functions) require an instance pointer to be invoked.

After fixed some typos in the demo code, I made it compilable and runnable in my ubuntu virtual machine. Next, I will explain the critical lines in the source code. N.b., in the demo code, it use a concrete types for a callback’s prototype (i.e., <void(int)>). In real-world application, however, those types can be made genetic too.

In #1 #2 and#3, the genetic parameters are passed all the way down to the method_stub, which is the working horse of this class. For now,  it is a template function that is not specialized yet. In the constructor, object_ptr points to the instance pointer and stub_ptr points to the specialized version of method_stub. With the combination of object_ptr->stub_ptr, the callback is ready to use.

Two points worth noting here are #4 and #5.

In step #4, the cast is not necessary if we move the template T to the class itself and do necessary changes. However, this makes the delegate polymorphic thus is inefficient according to the original post.

In step #5, the confusing dereference here is actually the syntax for invoking a function pointer to a member function.