From: Michael Edgar Date: 2011-07-09T14:16:12+09:00 Subject: Re: FFI with a C++ Library On Jul 9, 2011, at 12:40 AM, Mike Bethany wrote: > This may be more of a C++ question but here goes: > > 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. > > 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. 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]. 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: extern "C" { void foo(...) { } } This will give foo the external name "foo" instead of how it would be defined with C++ linkage. 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: #ifdef __cplusplus extern "C" { #endif and this at the bottom: #ifdef __cplusplus } #endif [0] http://en.wikipedia.org/wiki/Name_mangling Michael Edgar adgar@carboni.ca http://carboni.ca/