From: Jason Roelofs Date: 2008-12-12T01:52:29+09:00 Subject: Re: Q: FFI and C++? On Thu, Dec 11, 2008 at 11:28 AM, Jeremy Henty wrote: > On 2008-12-11, Jason Roelofs wrote: > >> Because of the complexity that C++ adds, I doubt that any feasible >> FFI library can be made that works reliably against C++ libraries, > > I was afraid of that. > >> Rice - ... >> Rb++ - ... > > I looked at those but I thought they were targeted at generating MRI > bindings. The point of using FFI is to eliminate MRI-specific C code. > I just need to wrap C++ with C to get functions that FFI can call, eg. > > extern "C" { > > void *thing_new() { return new Thing(); } > void thing_delete(void *vt) { delete (Thing *) vt; } > void *thing_bar(void *vt) { return ((Thing *) vt)->bar(); } > > // etc. etc. etc. > } > > I already know this works, now I'm trying to save effort, particularly > with parsing C++ and autogenerating wrapper code. Maybe I could use > rbgccxml and add my own XML -> wrapper code backend. It looks as > though rbgccxml is precisely the bit of rbplusplus that I would want > to reuse. > > Thanks for your help, > > Jeremy Henty > > I'd ask that if you already need to write and compile a wrapper to make a library work with FFI, then why bother with FFI in the first place? Rice / Rb++ will get you a lot farther in less time than trying to hack your way into using the FFI library, besides the fact that your sample wrapper code will wreck complete havok with your C++ <-> Ruby type mapping, requiring a lot more code to handle converting types to and fro as needed. Rice gives you all this automatically. In short, Rice isn't about generating MRI bindings for you but more about exposing an OO wrapper around the MRI bindings, so you don't have to get into the nitty-gritty details of Ruby's C API, and then Rb++ sits on top of that to auto-generate as much Rice code as possible so you only have to work in Ruby. Jason