2

(I want to make this question as generic as possible, so I will omit the name of the library that gave me the problem in the title.)

I have a project, and for that project I need a library xxx. So I download that library, compiled with the default commands (./configure && make && make install && make clean) - the installation procedure was documented - and tried if the example given in the documentation worked to see whether everything was correctly set up. I copied and pasted the code in a .c file and compiled it with the following command:

gcc -o program program.c -lxxx

And gcc reports no errors. However, as soon as I run the program, the following error occurs:

 ./program: error while loading shared libraries: lib<xxx>.so: cannot open shared object file: No such file or directory

The first solution that came up to my mind was to use the gcc option -static. It worked fine and the program run correctly. But I was disappointed because that means that the library is "embedded" in the program (sorry for the wrong terminology here: I'm just a student).

So, I looked for a solution in Stack Overflow: that's what I found: error while loading shared libraries: libnsd.so: cannot open shared object file: No such file or directory

The library is different, but using the command

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

worked fine. In fact the libxxx.so is in /usr/local/lib (found just by using ls /usr/local/lib | grep xxx). The other, permanent, solution the thread suggests is to write the directory name in the /etc/ld.so.conf file:

sudo echo "/usr/local/lib" >> /etc/ld.so.conf
sudo ldconfig

And that's the tricky point: ld.so.conf file contains the following line

include /etc/ld.so.conf.d/*.conf

and inside the directory /etc/ld.so.conf.d there is a file called libc.conf that contains the line

/usr/local/lib

I'm a bit confused about that include; if it works just as #include in C I should have /usr/local/lib in my /etc/ld.so.conf file. It appears it is not so.

4
  • did you run rehash after installing the library? Commented Nov 10, 2017 at 21:56
  • The issue is not specifically related to C. Commented Nov 11, 2017 at 8:06
  • man ldconfig Commented Nov 12, 2017 at 12:30
  • @Herb Wolfe, no... I run into rehash for the first time now.. ldconfig solved the problem, but I'm going to look for that too. Thanks! :-) Commented Nov 15, 2017 at 7:14

1 Answer 1

3

/etc/ld.so.conf is only read by ldconfig, which uses it to populate the dynamic loader cache /etc/ld.so.cache. If you put a library into /usr/local/lib and /usr/local/lib is included in the search path via that mechanism, it will only become usable (without setting LD_LIBRARY_PATH) if you run ldconfig first.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, sorry for the late reply and thanks a lot for your answer to my thread. I did not know about the ld.so.cache... I will make some nore researches about it. Anyway, running ldconfig has solved the problem. So, every time I install a dynamic load library in /usr/local/lib I need to refresh the cache file, don't I?
Yes, run ldconfig after system-wide library installation is recommended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.