Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to fix shared object file error

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


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: cannot open shared object file: No such file or directory

However, you might know that the shared library file 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.


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).


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

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧