Monday, January 11, 2016

How to solve shared Libraries problem in Linux

Sometimes when you try to compile a program in Linux you face the problem of the shared libraries. It happens when C Linker is not able to locate them in the File System. Shared libraries are loaded when a program starts. they include a ".so" as the file extension and are similar to dynamic-link library or DLL under Windows. There is a convention to name  them in linux:

lib + name of library + .so + version number

for example: libXrender.so.1

The shared libraries are usually placed in the /usr/lib (/usr/lib/x86_64-linux-gnu/ for x86 machine in Ubuntu 14.04) or /usr/local/lib.
If you can not compile a program because some shared library are not present on your system, you have to first make sure which library is missing. you can use "ldd" program to prints shared library dependencies.

$ldd ./libsqlncli-11.0.so.1720.0
  libcrypto.so.6 => not found
  libodbc.so.1 => /usr/local/lib/libodbc.so.1
  libssl.so.6 => not found

The above example tells us that the shared library itself depends on two other libraries which are: libcrypto and libssl.

To resolve this issues we have to make sure which path the linker should search to find the necessary libraries. To do this, we have to simply edit file /etc/ld.so.conf. To do so just put this line in the file :

include /etc/ld.so.conf.d/*.conf

it make sure to add all the config files on the directory. We could add the necessary paths in the file directly but this way is more convenient.

For the last step just make a new file and add the path of the library. for example:

# vim /etc/ld.so.conf.d/my_odbc.conf

and add this line

/usr/local/lib/

After these steps you have to run the following command as superuser:

#ldconfig

ldconfig configure dynamic linker run-time bindings. It creates the necessary links found in the file /etc/ld.so.conf.

when compiling the program we could also pass the path by using -L option to announce the shared library ( or static) to the linker. the shared library (or static) should be announced to the linker. for example:

gcc –o program program.o –L/usr/lib –lfoo

tells the compiler to look into /usr/lib for libraries and -lfoo options tells the compiler to use a library called libfoo.so.

No comments: