c++ - How to export specific symbol from executables in GNU/Linux -
while loading dynamic libraries ::dlopen(), exporting symbols executables can done -rdynamic option, exports symbols of executable, results in bigger binary size.
is there way export specific function(s)?
for example, have testlib.cpp , main.cpp below:
testlib.cpp
extern void func_export(int i); extern "c" void func_test(void) { func_export(4); } main.cpp
#include <cstdio> #include <dlfcn.h> void func_export(int i) { ::fprintf(stderr, "%s: %d\n", __func__, i); } void func_not_export(int i) { ::fprintf(stderr, "%s: %d\n", __func__, i); } typedef void (*void_func)(void); int main(void) { void* handle = null; void_func func = null; handle = ::dlopen("./libtestlib.so", rtld_now | rtld_global); if (handle == null) { fprintf(stderr, "unable open lib: %s\n", ::dlerror()); return 1; } func = reinterpret_cast<void_func>(::dlsym(handle, "func_test")); if (func == null) { fprintf(stderr, "unable symbol\n"); return 1; } func(); return 0; } compile:
g++ -fpic -shared -o libtestlib.so testlib.cpp g++ -c -o main.o main.cpp i want func_export used dynamic library, hide func_not_export.
if link -rdynamic, g++ -o main -ldl -rdynamic main.o , both functions exported.
if not link -rdynamic, g++ -o main_no_rdynamic -ldl main.o , got runtime error unable open lib: ./libtestlib.so: undefined symbol: _z11func_exporti
is possible achieve requirement export specific function?
is there way export specific function(s)?
we needed functionality, , added --export-dynamic-symbol option gold linker here.
if using gold, build recent version , you'll set.
if not using gold, perhaps should -- it's faster, , has functionality need.
Comments
Post a Comment