Sunday, April 12, 2009

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

I recently started learning/using Google C++ Testing framework.

I compiled and installed Google testing framework as per the instruction. 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

So, to check which all shared library are failed to load, I issued following command

$ ldd a.out

and the output was

[email protected]u-desktop:~/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 are failed to load. This gave me a hint that, /usr/local/lib is not the ld library 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 typed

$ ldd a.out

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


After this I was able to run samples without any problems. :)

Temporary Solution:

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

so, the solution is to add /usr/local/lib into the enviornment variable named LD_LIBRARY_PATH, by typing

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 into the terminal

$ sudo bash -c 'echo /usr/local/lib >> /etc/ld.so.conf ' && sudo ldconfig

P.S.: I think GNU/Linux distributions by default should include /usr/local/lib in /etc/ld.so.conf or at-least the install script of the libraries should have this intelligence.

No comments: