Search code examples
gccshared-librariesld

Why is my shared library not found?


I'm trying to compile an example program that links to the shared library produced by Sundown. I'm compiling the program like so.

$ gcc -o sd sundown.c -L. -lsundown

Yet, when I run it I get the following error.

./sd: error while loading shared libraries: libsundown.so: cannot open shared object file: No such file or directory

The output of ls is.

$ ls
libsundown.so  libsundown.so.1  sundown.c  sd

Why is the shared library not found by ld?


Solution

  • Short solution:

    add . (or whatever it is from your -L flag) to your LD_LIBRARY_PATH. When you run sd, it'll look for libraries in the standard places and the LD_LIBRARY_PATH. Note that since you've added ., this will only work if you run sd from the same directory libsundown.so is in.

    I plan on distributing the compiled binary. How can I do so that the library can be distributed without forcing people to edit their LD_LIBRARY_PATH?

    You should install libsundown.so in one of the standard places, like /usr/lib or /usr/local/lib. You can do that with an installer or a make file, or something as simple as a INSTALL or README that tells the user to stick the libraries there and ensure the permissions are set to something sensible.