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:

1.

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

2.

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 libname.so that points to the shared library file. You should use a relative symlink (with /usr/lib/libc.so pointing to ../../lib/libc.so.5.3.12), instead of an absolute symlink (/usr/lib/libc.so would not point to /lib/libc.so.5.3.12).

3.

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 libname.so 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.

4.

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

5.

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/ld.so.cache, you may be able to detect the slowdown in your system. Run ldconfig to regenerate /etc/ld.so.cache.

You need to create entries in /etc/ld.so.conf 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:

1.

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

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


2.

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

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


3.

Create a pointer from the soname to the library:

 ln -sf libhello.so.0.0 libhello.so.0 


4.

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

 ln -sf libhello.so.0 libhello.so 


5.

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.)

6.

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 libhello.so.* in the /usr/lib directory and avoid setting the LD_LIBRARY_PATH environment variable, if you like.


       
    top
     


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net