Middle
How to disable name mangling in C/C++?
sobes.tech AI
Answer from AI
// To disable name mangling in C++ for use with C code
extern "C" {
// Declarations of functions or global variables for which mangling is disabled
void my_c_function();
int my_c_variable;
}
// Example of a function declared with extern "C"
void my_c_function() {
// Function implementation
}
The extern "C" keyword in C++ indicates to the compiler that certain functions or global variables should be compiled using C language naming and calling conventions, rather than C++. This prevents name mangling, which C++ uses to support function overloading, namespaces, and object-oriented features.
In C, name mangling is not performed by default.
Using extern "C" is typical when interfacing C++ code with libraries written in C to ensure correct linking of functions.