Generate C wrapper from C++?

c, c++, ffi, wrapper

Solution

You can try SWIG, C code generator was last year's GSoC project. AFAIK they haven't merged it to the trunk yet, so you'd have to checkout & build the branch from SVN.

Problem

I want to generate C wrappers from C++ libraries. There are tutorials on how to do it by hand: - http://dsc.sun.com/solaris/articles/mixing.html - http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html But it is too much of a manual labor. For example, for this: ``` struct RtAudio { virtual DeviceInfo const& f() {...} class DeviceInfo { virtual void g() { ... } }; ... }; ``` I need to write: ``` struct RtAudioC { RtAudio x; }; struct DeviceInfo { RtAudio::DeviceInfo x; }; extern "C" { RtAudioC* newRtAudio() { return new RtAudioC; } void deleteRtAudio(RtAudioC *p { delete p; } /* do something with RtAudio::f() */ void g(DeviceInfo *p) { try { p->x.g(); } catch (SomeError & err) { } } } ``` Are there tools that can automate this process?

Original source