Search code examples
xcodemacossymbolsdylib

How to unmangle exported symbols from C++ in dynamic libraries in Xcode on macOS


I've been trying to develop a dynamic library in C++ that can be run-time loaded in an application. I finally got it working, but it's a little ugly. I have a function that takes a pointer to a C++ class as an argument, which looks like this:

bool registerGrindPlugin( Grind::PluginManager* mgr );

But of course it's being exported as:

_Z19registerGrindPluginPN5Grind13PluginManagerE

I tried a .c file with a simple function and it exported fine as "registerGrindPlugin", but of course I can't pass a C++ class as the argument that way.

So... my question is, is there a way to unmangle or alias the exported symbol so that I don't have to use a monstrosity like Z19registerGrindPluginPN5Grind13PluginManagerE in my dlsym call?

I did see something about -alias_list as a linker option, but I haven't quite figured out how to use it in Xcode. If that is the solution, can somebody provide some more details on how to use this?


Solution

  • Usually, plugin interfaces are defined with c-naming and calling conventions. Name mangling is potentially compiler dependent (non standard).

    So the easiest solution is to define some interface function and declare that as extern "C":

    extern "C"  {
        bool registerGrindPlugin( Grind::PluginManager* mgr );
    }
    

    You may have to replace the Grid::PluginManager with a void* type and an internal cast. This has to be a simple structure, rather than a class with virtual functions.

    If you do insist on dealing with mangled names you can take a look at the source code of 'c++filt' which is a gnu utility that is also available on OS X.