Section 8.5. Installing Shared Libraries


8.5. Installing Shared Libraries

The ldconfig program does all the hard work of installing a shared library. You just need to put the files in place and run ldconfig. Follow these steps:


Copy the shared library to the directory in which you want to keep it.


If you want the linker to be able to find the library without giving it a -Ldirectory flag, install the library in /usr/lib, or make a sym-link in /usr/lib named that points to the shared library file. You should use a relative symlink (with /usr/lib/ pointing to ../../lib/, instead of an absolute symlink (/usr/lib/ would not point to /lib/


If you want the linker to be able to find the library without installing it on the system (or before installing it on the system), create a link in the current directory just like the system-wide one. Then use -L. to tell gcc to look in the current directory for libraries.


If the full pathname of the directory in which you installed the shared library file is not listed in /etc/, add it, one directory path per line of the file.


Run the ldconfig program, which will make another symlink in the directory in which you installed the shared library file from the soname to the file you installed. It will then make an entry in the dynamic loader cache so that the dynamic loader finds your library when you run programs linked with it, without having to search many directories in an attempt to find it.[4]

[4] If you remove /etc/, you may be able to detect the slowdown in your system. Run ldconfig to regenerate /etc/

You need to create entries in /etc/ and run ldconfig only if you are installing the libraries as system libraries if you expect that programs linked against the library will automatically work. Other ways to use shared libraries are explained in the next section.

8.5.1. Example

As an extremely simple but still instructive example, we have created a library that contains one short function. Here, in its entirety, is libhello.c:

 1: /* libhello.c */ 2: 3: #include <stdio.h> 4: 5: void print_hello(void) { 6:     printf("Hello, library.\n"); 7: } 

Of course, we need a program that makes use of libhello:

 1: /* usehello.c */ 2: 3: #include "libhello.h" 4: 5: int main (void) { 6:     print_hello(); 7:     return 0; 8: } 

The contents of libhello.h are left as an exercise for the reader.

In order to compile and use this library without installing it in the system, we take the following steps:


Use -fPIC to build an object file for a shared library:

 gcc -fPIC -Wall -g -c libhello.c 


Link libhello against the C library for best results on all systems:

 gcc -g -shared -Wl,-soname, -o \         libhello.o -lc 


Create a pointer from the soname to the library:

 ln -sf 


Create a pointer for the linker to use when linking applications against -lhello:

 ln -sf 


Use -L. to cause the linker to look in the current directory for libraries, and use -lhello to tell it what library to link against:

 gcc -Wall -g -c usehello.c -o usehello.o gcc -g -o usehello usehello.o -L. -lhello 

(This way, if you install the library on the system instead of leaving it in the current directory, your application will still link with the same command line.)


Now run usehello like this:

 LD_LIBRARY_PATH=$(pwd) ./usehello 

The LD_LIBRARY_PATH environment variable tells the system where to look for libraries (see the next section for details). Of course, you can install* in the /usr/lib directory and avoid setting the LD_LIBRARY_PATH environment variable, if you like.


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168 © 2008-2017.
    If you may any questions please contact us: