In C++, the extern "C" syntax is used to specify that a section of code (functions, variables, etc.) should be treated using C linkage and name-mangling rules, even if the rest of the code is compiled as C++ code with its more complex name-mangling and linkage rules.
C++ uses a technique called "name mangling" to encode additional information about functions and symbols into their names to handle function overloading, namespaces, and other features. This name-mangling makes C++ functions incompatible with C functions when it comes to linking.
Using extern "C" is particularly useful in situations where you want to call C++ functions from C code, or when you're dealing with libraries that are written in C++ but need to be used from C code.
Here's an example:
Suppose you have a C++ function defined like this:
// mylib.cpp
#include <iostream>
extern "C" void foo() {
std::cout << "Hello from foo!" << std::endl;
}
By enclosing the foo function with extern "C", you are telling the C++ compiler to use C linkage and name-mangling rules for this function. This means that the function's name will not undergo C++ name-mangling, and it will be exposed in a way that is compatible with C.
Now, if you want to call this C++ function from a C program, you would create a header file to declare the function:
// mylib.h
#ifdef __cplusplus
extern "C" {
#endif
void foo();
#ifdef __cplusplus
}
#endif
The #ifdef __cplusplus checks ensure that the extern "C" syntax is only used when compiling in a C++ context.
Finally, in your C program, you can include the header and call the C++ function:
// main.c
#include "mylib.h"
int main() {
foo();
return 0;
}
In this example, the extern "C" syntax ensures that the C++ function foo is callable from the C code without any name-mangling issues.
Remember that the use of extern "C" is mainly relevant when interacting between C++ and C, and it's not necessary for pure C++ code.
C++ uses a technique called "name mangling" to encode additional information about functions and symbols into their names to handle function overloading, namespaces, and other features. This name-mangling makes C++ functions incompatible with C functions when it comes to linking.
Using extern "C" is particularly useful in situations where you want to call C++ functions from C code, or when you're dealing with libraries that are written in C++ but need to be used from C code.
Here's an example:
Suppose you have a C++ function defined like this:
// mylib.cpp
#include <iostream>
extern "C" void foo() {
std::cout << "Hello from foo!" << std::endl;
}
By enclosing the foo function with extern "C", you are telling the C++ compiler to use C linkage and name-mangling rules for this function. This means that the function's name will not undergo C++ name-mangling, and it will be exposed in a way that is compatible with C.
Now, if you want to call this C++ function from a C program, you would create a header file to declare the function:
// mylib.h
#ifdef __cplusplus
extern "C" {
#endif
void foo();
#ifdef __cplusplus
}
#endif
The #ifdef __cplusplus checks ensure that the extern "C" syntax is only used when compiling in a C++ context.
Finally, in your C program, you can include the header and call the C++ function:
// main.c
#include "mylib.h"
int main() {
foo();
return 0;
}
In this example, the extern "C" syntax ensures that the C++ function foo is callable from the C code without any name-mangling issues.
Remember that the use of extern "C" is mainly relevant when interacting between C++ and C, and it's not necessary for pure C++ code.
Comments
Post a Comment