linux - get mangled symbol name from inside C++ -
how can mangled name of function inside c++ program?
for example, suppose have header:
// 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 have client program can load plugins implement interface:
// client.h #include "interface.h" void call_plugin(so_filename) { void *lib = dlopen(so_filename, ...); // how implement 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 write get_mangled_name
function compute mangled name of foo
function , return string?
one method comes mind compile interface.o
, use nm -a interface.o | grep foo
mangled name copy-and-paste hard-coded string client.h
, feels wrong approach.
another method comes mind force interface.h
pure c interface marshal of c++ data big blob that's sent through c interface , have c++ objects reconstructed on other side. feels suboptimal.
edit
note distinct question getting mangled name demangled name. i'm wondering how @ mangled value inside of c++ itself, @ compile time, type information available compiler, given c++ identifier's name only.
you can prevent mangling declaring functions 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
... , making sure #include header file in source files make shared library, declaration affects both definition , references relevant function emitted compiler. note function signature can still use non-pod types, such std::string.
Comments
Post a Comment