Wednesday, April 16, 2014

What is ldd? C++ Linux and Shared library depedencies.

what is ldd command used for!!

Shared Library in c++ do have Dependencies on other library's. For example a library A(libtiff) which will print a picture can use a library B(libjpeg) to use different color.
You will usually compile your file with shared library like this:

% gcc -o test test.c –ltiff .             Here libtiff is a shared library.

As we know for shared libray by default, this will pick up the shared-library version of libtiff, from location /usr/lib/libtiff.so. As we discussed above A(libtiff) uses B(libjpeg), the shared-library versions of these two are also drawn in (a shared library can point to other shared libraries that it depends on).To know the dependencies of shared library on other shared library we use ldd. eg.


% ldd tifftest
libtiff.so.93 => /usr/lib/libtiff.so.93 (0x4001d000)
libc.so.6 => /lib/libc.so.6 (0x40060000)
libjpeg.so.62 => /usr/lib/libjpeg.so.62 (0x40155000)

This command is not for static library's for obvious reasons that if we want to link static library to other we need to mention about that on command line

% gcc -static -o tifftest tifftest.c -ltiff -ljpeg 
       * -static means satic mean static library will take preference before dynamic library 

Depencies in shared library is often result of poor design or unavoidable dependencies. As linker will have to search static library each time it occurs in command line. 
you can read about more about difference shared and static library here. 

No comments:

Post a Comment