C++ linux: dlopen can't find .so library -


reworded question (although it's been solved already):

i've been having trouble using dlopen(3) load shared object library on linux. library part of system of libraries built me loaded @ runtime central executable. of organized single workspace in code::blocks, each project given own folder within directory called source, shipped program. build directory of executable 2 directories backward own source code exectuable , source folder in same directory, libraries build same directory executable, naturally pass name of library i'm trying open shown:

int main(int argc, char** argv) {     void* hlibrary = dlopen("liblibrary.so", rtld_now | rtld_global);     if(hlibrary == null) {         fprintf(stderr, "%s\n", dlerror());         return 1;     }      return 0; } 

this working @ 1 point when build directory same source code, until changed directories of source code around arrangement described above. problem @ point dlerror() returns "cannot open liblibrary.so: no such file or directory," though file exists , in same directory executable. tried passing in "/liblibrary.so" instead, because according man page on dlopen(3), adding / indicates relative directory. returned same error.

the solution "./" needed - "." represents working directory of executable - , working directory needed changed in code::blocks executable built. following works perfectly:

void* hlibrary = dlopen("./liblibrary.so", rtld_now | rtld_global); 

this doesn't show full solution, following equivalent of i'm doing:

void* hlibrary = dlopen("./../../liblibrary.so", rtld_now | rtld_global); 

hopefully explains situation little better.

read dlopen(3) man page (e.g. typing man dlopen in terminal on machine):

if filename contains slash ("/"), interpreted (relative or absolute) pathname. otherwise, dynamic linker searches library follows (see ld.so(8) further details):

   o   (elf only) if executable file calling program        contains dt_rpath tag, , not contain dt_runpath tag,        directories listed in dt_rpath tag searched.     o   if, @ time program started, environment        variable ld_library_path defined contain colon-separated        list of directories, these searched.  (as security        measure variable ignored set-user-id , set-group-id        programs.)     o   (elf only) if executable file calling program        contains dt_runpath tag, directories listed in        tag searched.     o   cache file /etc/ld.so.cache (maintained ldconfig(8))        checked see whether contains entry filename.     o   directories /lib , /usr/lib searched (in order). 

so need call dlopen("./liblibraryname.so", rtld_now) -not dlopen("liblibraryname.so", rtld_now) wants plugin in $ld_library_path on in /usr/lib/ etc .... - or add . ld_library_path (which don't recommend security reasons).

as jhonnash answered should use , display result of dlerror when dlopen (or dlsym) fails:

  void* dlh = dlopen("./liblibraryname.so", rtld_now);   if (!dlh)      { fprintf(stderr, "dlopen failed: %s\n", dlerror());        exit(exit_failure); }; 

you might want read books advanced linux programming knowledge linux system programming in general.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -