Installing 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 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.
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
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...)
Working with Windows from inside Ubuntu - Although the Linux platform offers an increasingly compelling platform for the desktop, there are sometimes situations when there is just no alternative application available. This is often the (more...)
Hardening the System with Bastille and Functions - Bastille is an open source program that facilitates the hardening of a Linux system. It performs many of the tasks, including downloading operating system updates and disabling services and po (more...)
Using GPG and Md5sum to Verify Signatures on Tarball Packages - Follow these steps to verify the signature of a gzipped tarball: 1. Add the public key of the person or organization that created the package. 2. Sign the public k (more...)
Red Hat Linux Errata: Fixes and Advisories - Once your Red Hat system is live, you must make sure that the most current required Red Hat errata are installed.These errata include bug fixes, corrections, and updates to Red Hat products. (more...)
Locking Down Ports Under Linux - TCP/IP networks assign a port to each service, such as HTTP, Simple Mail Transfer Protocol (SMTP), and Post Office Protocol version 3 (POP3).This port is given a number, called a port number, (more...)
Deploying GNU Privacy Guard - Although many GUI interfaces are in the planning stage for GPG, the following steps focus on using GPG with the command line.The steps assume that you already have GPG installed on your system (more...)
Manually Disabling Unnecessary Services and Ports in Linux - To harden a server, you must first disable any unnecessary services and ports.This process involves removing any unnecessary services, such as the Linux rlogin service, and locking down unnece (more...)
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 (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.