You can mix C and C++ coded but there's a trick to it.
First, C++ has function overloading. That is, you can have muiltiple functions of the same name as long as the arguments are different.
Second, C++ uses a decorator, or mangler, to hash a functions and it's arguiments to a unique symbol. This symbol, which may look like MHG_FYYZ is tghe name of the function called by C++ functions.
Third, a C function is not mangled. So when you call a C++ function from C you use the C++ function name and not the mangled name. Since these two name are never the same you can never call the C++ function using it's C name.
Fourth, The trick: Tell the C++ compiler that the C++ function will be called from C and ot please turn off the name mangler. You lose function overloading in C++ for this function since all function names in C++ must be unique. That's why the mangler in the first place.
To do this you use
extern "C" in the C++ code:
- extern "C" void MyFunction(int);
in the header file and:
- extern "C" void MyFunction(int)
-
{
-
//function body
-
}
Note that
appears only in the C++ code.
Fifth, Therefore any C function called by C++ must be extern "C" in C++. Likewise, any C function calling C++ functions must be extern "C" in C++.
No changes are ever made to the C code.