How to fix shared object file error

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

  • Open the /etc/ld.so.conf as sudo and add a new line with the library directory. In this case, we add /opt/foobar/lib.

  • Rerun ldconfig to rebuild the cache:

$ sudo ldconfig
  • Check if the shared library cache now includes the shared libraries from the new directory:
$ ldconfig -p

Your program should now execute without any errors 🙂

Tried with: Ubuntu 14.04

Advertisements

10 thoughts on “How to fix shared object file error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s