How to automate wrapping C library into C++

87 Views Asked by At

LVGL library looks very promising as an alternative to Qt (C++), but it's C based. Is there a way to automate wrapping it into a C++ modern interface or at least with minimum modification each new release?

In the javascript world, react has a mechanism called react-reconciler to automate wrapping libraries, so is there something similar for C to C++?

1

There are 1 best solutions below

2
On

Most of the time you can just use the the C library by including the C header file. You might have to add an extern "C" {} around the include if the header doesn't already have that itself.

But if you mean updating the interface to modern C++ standards, using std::uniq_ptr, std::shared_ptr, std::vector and so on then you are out of luck. Changing between those and C types can't be simply automated since they add meaning to the interface that simply doesn't exist within C, it can't be inferred from the header.

On the other hand the library interface isn't likely to change. At least not much. Normally future versions should only have additions, anything else requires a SONAME change and requires all software to be recompiled against the new lib at a minimum. So if you can't live with the C interface you would only have to write a wrapper once and then just do minor additions for updates.