As Paul R. answered, you need a stub code. also, you'll better be sure that your C++ function does not throw exceptions (I guess that a C++ function, called from a C program and main
, which throws an uncaught exception is having some undefined behavior). BTW, you should be sure that the constructor of static
C++ data (like std::cout
) is called "conceptually" before your main
in C (so you better link your program with a C++ compiler, not a C one). See the GCC __attribute__(constructor)
In practice, at least on Linux with C++ code compiled by GCC (g++
) or by Clang/LLVM (clang++
), a C++ function has some mangled name.
You might use some ugly and non-portable trick to call a function by its mangled name. You could dare coding:
int main(int argc, char**argv) {
extern void* _Z10my_alloc_ci(int size);
void * p = _Z10my_alloc_ci(42);
return 0;
}
but I am a bit ashamed of giving such silly advice. You could even use asm labels e.g.
extern void*f(int) asm ("_Z10my_alloc_ci");
p = f(42);
However, I feel that you should not have any such approaches, and I am surprized why you need to call a C++ function which is not wrapped around extern "C"
.
Notice that in theory, C++ functions (without extern "C"
) could even have a different -and incompatible- calling convention than C functions. I don't know any implementation doing that. So to be safe you should wrap your C++ function with some C++ wrapping using extern "C"
To avoid uncaught exceptions, you might catch all of them in your C++ wrapper:
#include <cstdio>
#include <cstdlib>
extern "C" void* my_alloc_c(int size) {
extern void* my_alloc(int);
try {
return my_alloc(size);
} catch (...) {
::fprintf(::stderr, "got uncaught C++ exception for my_alloc(%d)\n",
size);
::fflush(nullptr);
::abort();
}
}
BTW, if your C++ library is large, you might perhaps try to automatize the generation of the glue code. For example, you could customize GCC using MELT (a Lispy domain specific language to extend GCC) by coding an extension in MELT which would generate the glue code when compiling the C++ header files.
You might be interested in libffi which enables you to (portably) call any (C and probably C++) function of arbitrary signature.