dlopen a dynamic library from a static library linux C++ -


i've linux application links against static library (.a) , library uses dlopen function load dynamic libraries (.so)

if compile static library dynamic , link application, dlopen work expected, if use described above won't.

can't static library uses dlopen function load shared libraries?

thanks.

there should no problem you're trying do:

app.c:

#include "staticlib.h" #include "stdio.h" int main() {   printf("and magic number is: %d\n",dosomethingdynamicish()); return 0; } 

staticlib.h:

#ifndef __staticlib_h__ #define __staticlib_h__  int dosomethingdynamicish();  #endif 

staticlib.c:

#include "staticlib.h" #include "dlfcn.h" #include "stdio.h" int dosomethingdynamicish() {   void* handle = dlopen("./libdynlib.so",rtld_now);   if(!handle)   {     printf("could not dlopen: %s\n",dlerror());     return 0;   }    typedef int(*dynamicfnc)();   dynamicfnc func = (dynamicfnc)dlsym(handle,"getmeanumber");   const char* err = dlerror();   if(err)   {     printf("could not dlsym: %s\n",err);     return 0;   }   return func(); } 

dynlib.c:

int getmeanumber() {   return 1337; } 

and build:

gcc -c -o staticlib.o staticlib.c ar rcs libstaticlib.a staticlib.o gcc -o app app.c libstaticlib.a -ldl gcc -shared -o libdynlib.so dynlib.c 

first line builds lib
second line packs static lib
third builds test app, linking in newly created static, plus linux dynamic linking library(libdl)
fourth line builds soon-to-be-dynamically-loaded shared lib.

output:

./app , magic number is: 1337 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -