Posts
Search
Contact
Cookies
About
RSS

Linking C code with a C++ library

Added 23 Feb 2020, 1:30 p.m. edited 18 Jun 2023, 1:12 a.m.

Its fairly common to see libraries that are written in C++ but only offer a C API (some also offer C & C++ API's). As someone who much prefers to code in C, I've sometimes needed to link my C application with a library that's written in C++ (for example ODE).

My initial solution was to simply link my object code with g++. This works just fine, and I simply left it at that and continued coding, assuming it was just some kind of function mangling or some other issue that a "vanilla" C linker couldn't cope with...

However the truth of the matter is somewhat simpler... turns out the only thing that's missing (assuming the library doesn't use other dependencies) is the standard C++ library.

You can still link with gcc by simply adding -lstdc++ to your LDFLAGS in your Makefile

Short and simple, but worth remembering!