Using Shared Libraries

by Lorent Konenbal.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on linux  

You are here: Categories » Computers and technology » Linux

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  
Leave a comment or ask a question
Total comments: 0

Linux Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
History of KDE - In 1996, Matthias Ettrich posted a now famous newsgroup post that described some of the problems that he had with the Unix Desktop.    Unix popularity grows thanks to the (more...)
History of Kubuntu - When Ubuntu was first being discussed there were rumors that it would be only based on GNOME, and KDE would be left out. Jonathan Riddell, a KDE developer, posted an article on his (more...)
Installing Kubuntu - Installing Kubuntu is just like installing Ubuntu. It is pretty much a snap. Let's start with where you find it. Where to Find Kubuntu Kubuntu is available a (more...)
Root iNode Corruption Cause Data Loss in Linux - Article contains information over root I node corruption failure causes data loss. Article also contains recovery solution for all kind of data loss in linux. In Linux and other Unix-like o (more...)
LINUX r Services - rlogind and rshd are the remote login and remote shell daemon. These so-called r services use TCP ports 513 and 514, respectively. The RLOGIN protocol is described in RFC 1282 and RSH in RFC. (more...)
Short History of Ubuntu - Born in April 2004, a history of Ubuntu may seem premature. However, the last years have been full ones for Ubuntu. With its explosive growth, it is difficult even for those involv (more...)
Free Open source Software and GNU Linux - Free Software and GNUIn a series of events that have almost become legend through constant repetition, Richard M. Stallman created the concept of "free software" in 1983. Stallman grew (more...)
Choose an Ubuntu Version - The developers behind Ubuntu have worked to make the software as easy and flexible to install as possible. They understand that people will be installing Ubuntu on different types of computers (more...)
Customize Ubuntu Look and Feel - Changing the Background To change the background of your desktop right-click it and select Change Desktop Background. Inside the dialog box that appears, choose yo (more...)
Configuring a Printer in Ubuntu - In the Linux world, configuring a printer has traditionally been a challenge. For years, newcomers to Linux have been repeatedly challenged and even bludgeoned with terms, commands, and phrases (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.