Get garbled character name from inside C ++

How can I get a distorted function name from a C ++ program?

For example, suppose I have a heading:

// interface.h
#ifndef INTERFACE_H
#define INTERFACE_H
typedef void (*FooFunc)(int x, int y, double z, ...more complicated stuff...);
void foo(int x, int y, double z, ...more complicated stuff...);
#endif

and I have a client program that can load plugins that implement the interface:

// client.h
#include "interface.h"
void call_plugin(so_filename)
{
    void *lib = dlopen(so_filename, ...);
    // HOW DO I IMPLEMENT THE NEXT LINE?
    static const char *mangled_name = get_mangled_name(foo);
    FooFunc func = (FooFunc)dlsym(lib, mangled_name);
    func(x, y, z, ...);
    dlclose(lib);
}

How to write a function get_mangled_nameto calculate the distorted function name fooand return it as a string?

One method that comes to mind is to compile interface.oand use nm -A interface.o | grep footo get the changed name, and then copy and paste it as a string with hard code in client.h, but this seems like the wrong approach.

, , interface.h C-, ++ -, C, ++, , .

Edit

, . , ++ , , ++.

+4
1

mangling, extern "C":

// interface.h
#ifndef INTERFACE_H
#define INTERFACE_H
typedef void (*FooFunc)(int x, int y, double z, ...more complicated stuff...);
extern "C" void foo(int x, int y, double z, ...more complicated stuff...);
#endif

... , # , , , , . , - -POD-, std::string.

+1

All Articles