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

前端 未结 2 1479
予麋鹿
予麋鹿 2020-12-09 22:17

Reworded Question (although it\'s been solved already):

I\'ve been having trouble using dlopen(3) to load a shared object library on linux. The library is part of a

相关标签:
2条回答
  • 2020-12-09 22:29

    Read the dlopen(3) man page (e.g. by typing man dlopen in a terminal on your machine):

    If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details):

       o   (ELF only) If the executable file for the calling program
           contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
           then the directories listed in the DT_RPATH tag are searched.
    
       o   If, at the time that the program was started, the environment
           variable LD_LIBRARY_PATH was defined to contain a colon-separated
           list of directories, then these are searched.  (As a security
           measure this variable is ignored for set-user-ID and set-group-ID
           programs.)
    
       o   (ELF only) If the executable file for the calling program
           contains a DT_RUNPATH tag, then the directories listed in that
           tag are searched.
    
       o   The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is
           checked to see whether it contains an entry for filename.
    
       o   The directories /lib and /usr/lib are searched (in that order).
    

    So you need to call dlopen("./libLibraryName.so", RTLD_NOW) -not just dlopen("libLibraryName.so", RTLD_NOW) which wants your plugin to be in your $LD_LIBRARY_PATH on in /usr/lib/ etc .... - or add . to your LD_LIBRARY_PATH (which I don't recommend for security reasons).

    As Jhonnash answered you should use and display the 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 to read some books like Advanced Linux Programming to get some knowledge about Linux system programming in general.

    0 讨论(0)
  • 2020-12-09 22:34

    About the dlopen is defined; Dynamic library dlopen error can be checked. Undefined symbol error by this link.

    0 讨论(0)
提交回复
热议问题