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.

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.

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

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: Lorent Konenbal at 08012007

Related Articles

1. Using Shared Libraries
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 unl...

2. What's a Linux Process
What exactly is a process? In the original Unix implementations, a process was any executing program. For each program, the kernel kept track of ...

3. POSIX Interfaces
POSIX Required Types POSIX defines several typedefs defined in the header file <sys/types.h> and used for many arguments and return values. These typede...

4. Common Linux Security Holes
Now that we have looked at ways of reducing the potential impact of insecure code, we go over some of the most common programming mistakes that lead to security problem...

5. Emax vs. vi Unix text editors
Unix developers have traditionally held strong and diverse preferences, especially about editors. Many programmers' editors are available for you to try; the two ...

6. Running a Linux Program as a Daemon
Programs that are designed to run as system daemons need to be a little careful how they become daemons to get all of the details right. Here is a list of things that s...

7. Linux Console Capabilities
The Linux console, like most terminals, is modal: Its response to data depends on what mode it is in. By default, it prints on the screen the characters you send ...

8. Notional Lineage of Unix Systems
Although the major portions of Linux comprise code developed independently of traditional Unix source bases, the interfaces that Linux provides were influenced heavily ...

9. What is the GNU Debugger
Gdb is the Free Software Foundation's debugger. It is a good command-line debugger, on which several tools have been built, including Emacs' gdb mode, the graphical ...