user672118
user672118

Reputation:

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?

Upvotes: 8

Views: 28643

Answers (2)

Kemin Zhou
Kemin Zhou

Reputation: 6881

On Centos systems with /usr/lib and /usr/lib64, if you install 64-bit libs manually into /usr/lib then at runtime, the library may not be visible even though at build time it is visible (I used autotools and it was able to find my zopfli library from /usr/lib without any problem). When I execute the my_binary that links to /usr/lib/libzopfli.so.1 I got

libzopfli.so.1 => not found

After moving libzopfly.so.1 from /usr/lib to /usr/lib64, then everything works fine.

Upvotes: 2

Jon Lin
Jon Lin

Reputation: 143846

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.

Upvotes: 10

Related Questions