We develop some project in plain C (C99). But, we have one library as source codes (math library) in C++. We need this library so I would like to ask, what is the most elegant way to integrate this source codes?
Ratio between sizes of C and C++ is 20:1 so moving to C++ is not the option. Should we use static library? DLL? (It’s all on Windows).
EDIT: Based on discussion in the comment, I should point out that separating things into a C-compatible
struct duckand a derivedclass Duckis probably unnecessary. You can probably safely shovel the implementation intostruct duckand eliminateclass Duck, thus obviatingreal(…). But I don’t know C++ well enough (in particular, the way it interacts with the C universe) to offer a definitive answer on this.There is no reason you can’t simply link all your C and C++ code together into a single binary.
Interfacing to the C++ code requires that you wrap the C++ API in a C API. You can do this by declaring a bunch of functions inside
extern "C" { ... }when compiling the C++ code, and without the extern declaration when compiling the C client code. E.g.:You can define the duck struct in your C++ source, and even inherit the real
Duckclass from it: