How to Use of extern C in C++

C++ is a powerful programming language that offers advanced features, but sometimes interoperability with C code is necessary. This is where the extern "C"
directive comes into play. It allows C++ code to call C functions and ensures that the C functions are linked correctly.
This tutorial will guide you through the use of extern "C"
in C++, explain name mangling, and provide practical examples. Whether you are a beginner or an experienced programmer, understanding how to use extern "C"
will enhance your programming skills and broaden your project capabilities.
Understanding extern "C"
The extern "C"
directive is essential when you want to include C code in your C++ programs. By default, C++ uses name mangling to allow function overloading, which changes the name of functions in the compiled code. This can create issues when linking C code, as C does not support name mangling. By using extern "C"
, you inform the C++ compiler that the functions within the block should use C linkage.
Here’s a simple example that demonstrates how to use extern "C"
:
// C code in a header file (example.h)
#ifdef __cplusplus
extern "C" {
#endif
void cFunction();
#ifdef __cplusplus
}
#endif
In this example, the extern "C"
directive is wrapped in preprocessor directives to ensure compatibility with both C and C++ compilers. This way, when you include this header in a C++ file, the function cFunction
will be linked correctly.
The extern "C"
block allows C++ to recognize cFunction
as a C function, avoiding name mangling issues. This is crucial when linking with C libraries or when you want to call C functions from C++ code.
Name Mangling in C++
Name mangling is a technique used by C++ compilers to encode additional information into the names of functions. This allows for function overloading, where multiple functions can have the same name but different parameters. However, this can create complications when trying to link C code with C++.
For example, if you have the following C++ function:
void sampleFunction(int a) {}
The compiler might generate a mangled name like sampleFunction_int
for it. If you try to call this function from C code, it will not recognize the mangled name, leading to linker errors. This is where extern "C"
becomes invaluable.
Using extern "C"
prevents name mangling, allowing the C++ compiler to use the function name as is. Here’s how you can define a C function in a C++ file:
extern "C" void sampleFunction(int a) {
// Function implementation
}
By declaring the function with extern "C"
, you ensure that it can be called from C code without any issues. This approach is particularly useful when working with legacy code or third-party libraries written in C.
Practical Example of Using extern “C”
Let’s consider a practical example where we need to use a C function in a C++ program. Assume we have a simple C library that provides mathematical functions.
First, create a C file named mathLib.c
:
#include <stdio.h>
void printSquare(int num) {
printf("Square: %d\n", num * num);
}
Next, create a header file mathLib.h
:
#ifdef __cplusplus
extern "C" {
#endif
void printSquare(int num);
#ifdef __cplusplus
}
#endif
Now, we can use this C library in a C++ file:
#include "mathLib.h"
int main() {
printSquare(5);
return 0;
}
Output:
Square: 25
In this example, the printSquare
function is defined in C and is successfully called from a C++ program. The use of extern "C"
in the header file ensures that the C++ compiler treats the function correctly, preventing name mangling and linking issues.
Conclusion
Understanding how to use extern "C"
in C++ is crucial for developers who work with both C and C++ code. It allows for seamless interoperability between the two languages while avoiding complications that arise from name mangling. By following the examples and explanations provided in this tutorial, you can confidently integrate C functions into your C++ projects. Whether you are maintaining legacy code or using third-party libraries, mastering extern "C"
will enhance your programming toolkit.
FAQ
-
What is extern “C” used for in C++?
extern “C” is used to specify that functions should use C linkage, preventing name mangling and allowing interoperability between C and C++. -
How does name mangling affect function calls?
Name mangling encodes additional information into function names, which can lead to linker errors when calling C functions from C++ if not handled with extern “C”. -
Can I use extern “C” with C++ classes?
Yes, you can use extern “C” with C++ classes, but it typically applies to functions rather than class methods, as C does not support classes. -
Is extern “C” necessary for all C functions in C++?
It is necessary when linking C functions in C++ to avoid name mangling issues, especially when using C libraries or legacy code. -
How do I include C headers in C++?
You can include C headers in C++ by wrapping the function declarations with extern “C” to ensure proper linkage.