From: Jason Roelofs Date: 2011-07-11T21:32:39+09:00 Subject: Re: FFI with a C++ Library --Apple-Mail-2--779612418 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On Jul 9, 2011, at 1:16 AM, Michael Edgar wrote: > On Jul 9, 2011, at 12:40 AM, Mike Bethany wrote: >> This may be more of a C++ question but here goes: >>=20 >> I'm trying to use Ruby FFI with a CPP library but when I compile my = CPP >> library and look at the method names there's always a __Z5, __Z4, or = __Z3 in >> front of the method name. I see this when I run nm against the dynlib = file. >>=20 >> Oddly when I compile a C file it works fine but I can use a C file = because >> I'm using some CPP libraries in my library. >=20 > This is due to what's called "name mangling" - it's how C++ > differentiates different functions with the same name based on > namespace, overloads, etc. You can learn more on > Wikipedia [0]. >=20 > To resolve this, you need to specify the functions being > called with C linkage. Wrap the necessary function as > follows in your .cpp/.cc file: >=20 > extern "C" { > void foo(...) { > } > } >=20 > This will give foo the external name "foo" instead of how > it would be defined with C++ linkage. >=20 > If you have C headers and see them being used from > C++ and wish to keep the unmangled names, you can > add this to the top of your C header: >=20 > #ifdef __cplusplus > extern "C" { > #endif >=20 > and this at the bottom: >=20 > #ifdef __cplusplus > } > #endif >=20 >=20 > [0] http://en.wikipedia.org/wiki/Name_mangling >=20 > Michael Edgar > adgar@carboni.ca > http://carboni.ca/ Another solution is to use the Rice library (http://rice.rubyforge.org/) = to build C++ bindings. That said, depending on the size of the C++ code = you're working with, it's probably simpler to go with your own code = inside an extern C { } as Michael explained above and from that you can = use FFI. Jason= --Apple-Mail-2--779612418--