LINUX TIPS AND TRICKS --- September 08, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Shared Libraries
by Danny Kalev

A collection of object files, or modules, produced from compiling one or 
more source files comprises a library. When you link a program, the 
linker searches for the necessary code in the library and copies it into 
the executable. For example, when a program contains a printf() call, 
the linker locates the function's code in the appropriate library and 
copies it into the executable. This is called "static linking" -- all 
symbols (e.g., function names) are resolved and inserted into the 
executable at link time. The code in the libraries can be pretty large; 
problematic since many programs use printf() -- or other standard 
functions -- and static linking produces bloated programs and wastes 
disk space. This bloating results in longer startup time and extensive 
paging. Shared libraries solve these problems. 

When you link a program against a shared library, the linker doesn't 
yank code from it; it merely places a reference in the executable to the 
specific dynamic library. When you run the program, a special system 
utility called the dynamic linker, or loader, locates the dynamic 
library referred to from the program and loads it into memory. Only a 
single copy of the library resides in the system's memory and all the 
programs linked against that library share the same copy. When another 
process (program) launches, the loader merely maps the address of the 
previously loaded library into the process's address space. 

The three main advantages of a shared library are:

    * It reduces the program's size
    * Changes made to the shared library (e.g., an upgrade, bug fixes) 
      don't require that the programs be re-linked; the next time you   
      run them, they will automatically pick the new library version.
    * A shared library saves disk space.

That said, shared libraries can also cause difficulties. Several 
programs depending on the same library that was modified or moved to a 
different directory, may result in some programs not running. This is 
known as "DLL hell" in Windows, although Linux suffers from a similar 
phenomenon. Additionally, dynamic linking incurs extra runtime overhead.

Shared libraries in Linux have an extension of the form .so.x, where x 
is the version number. To see your shared C library, type the following 
command:
  
  ls /lib/libc.so*

To list all the shared libraries that a particular program uses, type:

  ldd program

Where 'program' is the name of the executable file.

Next week we will learn how to diagnose and solve two common problems 
with shared libraries.


Resources

How much RAM is enough? 
Determining the right amount of RAM for your Unix computer requires a 
little detective work
http://www.sunworld.com/sunworldonline/swol-05-1996/swol-05-perf.html

Which is better, static or dynamic linking? 
At compile time, developers can choose static or dynamic linking.
Which offers better performance?
http://www.sunworld.com/sunworldonline/swol-02-1996/swol-02-perf.html

Shared memory uncovered 
Discover how shared memory is implemented in Solaris, the boundaries of 
the shared memory tunable parameters, what "intimate shared memory" is, 
and how these things play into implementation
http://www.sunworld.com/sunworldonline/swol-09-1997/swol-09-insidesolaris_p.html

************************************************************************

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.
 
*********************************************************************

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
dan_chupka@itworld.com
* For recruitment advertising information, write Jamie Swartz, Eastern
Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
Western Regional Sales Manager at: paul_duthie@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager,
Newsletters at: jodie_naze@itworld.com

*********************************************************************

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com
