It seems to me like a no-brainer, but I cannot find any information against or for it.
From the point of view of demangling etc, I don’t suppose this to be a big problem, but I can’t figure out, how I can write a little tiny C program which calls a function from a little tiny C++ library.
I am on linux right now, trying with static binding.
This must be something many people are running into or many books cover, but I feel like a blind gump sitting in front of this problem. Interestingly, there is no such question on SO either.
I do not even know IF this can work, far lesser HOW this has to be done.
Typically, you need to force the C++ compiler to build the library with C linkage for the exported functions.
You can do that by doing the following to your header:
Normally, the linker will do C++ name-mangling to the functions, so that the C linker won’t be able to find them.
extern "C"forces it not to do that. You need to wrap it in the#ifdef, becauseextern "C"isn’t valid in C.UPDATE
As Charles Salvia says in a comment below, you need to ensure that no exceptions make their way out through your interface, because C has no way of handling them (at least not a platform-independent way).