Sunday, April 12, 2009

How To: Fix shared library load problem in GNU/Linux

I recently started using Google C++ Testing framework.

I compiled and installed Google testing framework in Linux as per the instructions. But, still I wasn't able to run the sample applications compiled by me.

Whenever I run them, I used to get following error:
error while loading shared libraries: libgtest.so.0: cannot open shared object file: No such file or directory

But I had installed Google test framework properly. The install script had properly placed the libgtest.so.0 into /usr/local/lib.

To check which all shared libraries failed to load, I issued following command:
ldd a.out

And the output was:
[email protected]:~/google_test/samples$ ldd a.out
        libgtest.so.0 => not found
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00002b2897d75000)
        libm.so.6 => /lib/libm.so.6 (0x00002b2898080000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00002b2898301000)
        libc.so.6 => /lib/libc.so.6 (0x00002b2898510000)
        /lib64/ld-linux-x86-64.so.2 (0x00002b2897b57000)


Which showed that only libraries placed in /usr/local/lib have failed to load. This gave me a hint that /usr/local/lib is not in the LD_LIBRARY_PATH search path.

To confirm this I entered following command which lists all the libraries that ld can load.
ldconfig -p

This confirmed that /usr/local/lib is indeed missing from ld library search path!

So to include /usr/local/lib into the ld library search path I typed following into the terminal.
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

after this I again ran ldd command
ldd a.out

this time the ld was able to properly find the library gtest.so.0 placed in /usr/local/lib

[email protected]:~/google_test/samples$ ldd a.out
        libgtest.so.0 => /usr/local/lib/libgtest.so.0 (0x00002ae54f520000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00002ae54f772000)
        libm.so.6 => /lib/libm.so.6 (0x00002ae54fa7d000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00002ae54fcfe000)
        libc.so.6 => /lib/libc.so.6 (0x00002ae54ff0d000)
        /lib64/ld-linux-x86-64.so.2 (0x00002ae54f302000)


Now I can run samples without any problems. :)

Temporary Solution: Temporary solution is to add /usr/local/lib into the environmental variable named LD_LIBRARY_PATH

You can do this by typing
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

Permanent solution: Permanent solution is to modify the file /etc/ld.so.conf and add the path /usr/local/lib into it.

You can do it manually by typing
sudo gedit /etc/ld.so.conf

or

type following in the terminal
sudo bash -c 'echo /usr/local/lib >> /etc/ld.so.conf ' && sudo ldconfig

Technorati tags: , .

2 comments:

Anonymous said...

Thank you for this concise, brief, and straight-forward tutorial about fixing missing linked libraries! It was very helpful!

Dinesh said...

Thank You... I was missing the same ld library from my patht too...