c++ - How to use shared libraries in a static library without forcing end-users to link against these? -
let's i'm working on static library foo.a
, makes use of function in bar.so
.
how build library in such way uses foo.a
in project doesn't have explicitly link against bar.so
?
what can in libfoo.a
make call dlopen
dynamically link libbar.so
. then, use dlsym
locate function want use.
typedef void (*barfunctiontype) (const char *); functiontype barfunction; void initialize_foo_lib () { void *h = dlopen("libbar.so", rtld_lazy); barfunctiontype barfunction = (barfunctiontype) dlsym(h, "barfunction"); // note scary looking cast function pointer. //... rest of code can call barfunction }
libbar.so
should c library, or function should extern "c"
if c++ library, find function name properly. otherwise need mangle name find function dlsym
. can use objdump -t
on library check if names in library mangled.
realize there still dependency on libbar.so
, baked static library. user of libfoo.a
not have add -lbar
link line when link in -lfoo
.
Comments
Post a Comment