Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dlopen failed: cannot open shared object file: No such file or directory

Tags:

linux

The problem is I use dlopen to load a library (the .so is written by me, it's not a system library), but I got the error shown in the title.

  1. I have included dlfcn.h
  2. in compiler, I used the -ldl command
  3. What I want to load is just the source code folder, I tried to add -L., but it did not work.
like image 958
user1534282 Avatar asked Oct 12 '12 01:10

user1534282


People also ask

Could not load library Cannot open shared object file no such file or directory?

cannot open shared object file: No such file or directory The reason behind this error is that the libraries of the program have been installed in a place where dynamic linker cannot find it.

How do I open a shared library file?

If you want to open a shared-library file, you would open it like any other binary file -- with a hex-editor (also called a binary-editor). There are several hex-editors in the standard repositories such as GHex (https://packages.ubuntu.com/xenial/ghex) or Bless (https://packages.ubuntu.com/xenial/bless).

What library is Dlopen in?

dlopen() The function dlopen() loads the dynamic shared object (shared library) file named by the null-terminated string filename and returns an opaque "handle" for the loaded object. This handle is employed with other functions in the dlopen API, such as dlsym(3), dladdr(3), dlinfo(3), and dlclose().


1 Answers

If the library you want to dlopen is not in the standard search path you have a number of options:

  1. Specify the full path to the file in dlopen

    dlopen("/full/path/to/libfile.so");

  2. Add the path to the library via LD_LIBRARY_PATH

    LD_LIBRARY_PATH=/path/to/library/ ./executable

  3. use the ld -rpath option to add a library path to the application.

    g++ -link stuff- -Wl,-rpath=/path/to/library/

Note that options 1 & 3 hardcode the library path into your application. -rpath does have an option to specify a relative path, i.e.

-Wl,-rpath=$ORIGIN/../lib/

Will embed a relative path into the application.

like image 128
mythagel Avatar answered Oct 05 '22 01:10

mythagel