Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to fix shared object file error

📅 2014-Jan-14 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ ldconfig, ld_library_path, shared library ⬩ 📚 Archive

Problem

One of the most common errors a programmer faces is when an executable is run and it fails to find a required shared library. The error is usually of this form:

hello-world-program: error while loading shared libraries: libFoobar.so.1: cannot open shared object file: No such file or directory

However, you might know that the shared library file libFoobar.so.1 actually exists, say in a directory named /opt/foobar/lib. But, for some reason the ld-linux dynamic loader-linker is not looking in this directory.

Solution

It is important to know which are the locations that the loader searches for a given shared library file. This is described in this post. After reading the above post, you can see that the error is caused because the shared library file is not found in the locations listed in that post. Of all those locations, the two places which are easy to modify are at the user-level (LD_LIBRARY_PATH) and at the system-level (shared library cache).

Add to LD_LIBRARY_PATH

Set this at shell for temporary use or add to the shell initialization file for permanent effect:

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/foobar/lib

If your shared library is located in the same directory as the executable, then you can add . to LD_LIBRARY_PATH.

Update system cache

Your program should now execute without any errors.

Tried with: Ubuntu 14.04