Is there any way where I can call C++ code from C code?
class a
{
someFunction();
};
How to call someFunction()
from a C code? In other words, I am asking how to avoid name mangling here.
C is faster than C++ C++ allows you to write abstractions that compile-down to equivalent C. This means that with some care, a C++ program will be at least as fast as a C one. The advantage C++ gives over C is that it enables us to also build reusable abstractions with templates, OOP and functional composition.
extern "C" specifies that the function is defined elsewhere and uses the C-language calling convention. The extern "C" modifier may also be applied to multiple function declarations in a block. In a template declaration, extern specifies that the template has already been instantiated elsewhere.
Yes - C++ can use C libraries.
(I don't remember all the damned cast operators for C++ off-hand and am too lazy to look it up, so replace (Foo*) with a more specific cast if you like in the following code.)
// My header file for the C API.
extern "C"
{
void bar(void* fooInstance);
}
// My source file for the C API.
void bar(void* fooInstance)
{
Foo* inst = (Foo*) fooInstance;
inst->bar();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With