Wrapping a C library in C++

Why, When, How

Lots of libraries offer interfaces in C mainly to be universally callable from any programming language. This is absolutely fantastic! It lets any language with a FFI (Foreign Function Interface) to hook into and use a large number of C libraries. But the downsides? The code is verbose, memory needs to be manually managed and it’s really easy to forget the crucial bits.

C++, being extremely close to C, can easily call into a C library if needed. And the vice-verse is also true if extern "C" is used (instruct the compiler to not mangle function names). So, should you even go through the trouble of wrapping it in C++? If so, what’s a good way to do it? This post explores these questions and offers an insight into the hell called cross-language interoperability.

[Read More]