Using Shared Libraries

written by: Lorent Konenbal; article published: year 2007, month 08;


In: Root » Computers and technology » Linux » Using Shared Libraries

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

The easiest way to use a shared library is to ignore the fact that it is a shared library. The C compiler automatically uses shared libraries instead of static ones unless it is explicitly told to link with static libraries. However, there are three other ways to use shared libraries. One, explicitly loading and unloading them from within a program while the program runs, is called dynamic loading.

Using Noninstalled Libraries

When you run a program, the dynamic loader usually looks in a cache (/etc/ld.so.cache, created by ldconfig) of libraries that are in directories mentioned in /etc/ld.so.conf to find libraries that the program needs. However, if the LD_LIBRARY_PATH environment variable is set, it first dynamically scans the directories mentioned in LD_LIBRARY_PATH (which has the same format as the PATH environment variable) and loads all the directories it finds in the path, before it looks in its cache.

This means that if you want to use an altered version of the C library when running one specific program, you can put that library in a directory somewhere and run the program with the appropriate LD_LIBRARY_PATH to access that library. As an example, a few versions of the Netscape browser that were linked against the 5.2.18 version of the C library would die with a segmentation fault when run with the standard 5.3.12 C library because of a more stringent enforcement of malloc() policies. Many people put a copy of the 5.2.18 C library in a separate directory, such as /usr/local/netscape/lib/, move the Netscape binary there, and replace /usr/local/bin/netscape with a shell script that looks something like this:

#!/bin/sh  
export LD_LIBRARY_PATH=/usr/local/netscape/lib:$LD_LIBRARY_PATH  
exec /usr/local/netscape/lib/netscape $* 

Preloading Libraries

Sometimes, rather than replacing an entire shared library, you wish to replace only a few functions. Because the dynamic loader searches for functions starting with the first loaded library and proceeds through the stack of libraries in order, it would be convenient to be able to tack an alternative library on top of the stack to replace only the functions you need.

An example is zlibc. This library replaces file functions in the C library with functions that deal with compressed files. When a file is opened, zlibc looks for both the requested file and a gzipped version of the file. If the requested file exists, zlibc mimics the C library functions exactly, but if it does not exist, and a gzipped version exists instead, it transparently uncompresses the gzipped file without the application knowing. There are limitations, which are described in the library's documentation, but it can trade off speed for a considerable amount of space.

There are two ways to preload a library. To affect only certain programs, you can set an environment variable for the cases you wish to affect:

LD_PRELOAD=/lib/libsomething.o 
exec /bin/someprogram $* 

However, as with zlibc, you might want to preload a library for every program on the system. The easiest way to do that is to add a line to the /etc/ld.so.preload file specifying which library to preload. In the case of zlibc, it would look something like this:

/lib/uncompress.o  

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article