#Invoking C++ code within a C file
I don't know how useful this is in practice, but today I learned that you can invoke C++ code within a C library if needed.
The basic process is to forward-declare C++ structs, then declare the function(s) to be invoked from the C library within an extern "C"
block. The file containing these declarations will need to be included in the C source file and in the C++ header and source files.
The C file can then be compiled normally with gcc
and the C++ source with g++
. Finally, the executable will need to be linked
with g++
so that C++ operators and keywords will be defined.
This trivial example seems silly, but one can imagine a scenario where a C-library does useful work while taking a function pointer
as an argument. The function declared in the extern "C"
block here might be accepted as such an argument, and as long as the final