Building Shared Libraries

by Lorent Konenbal.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on linux  

You are here: Categories » Computers and technology » Linux

Once you have grasped the concept of sonames, the rest is easy. Just follow a few simple rules.

  • Build your sources with gcc's -fPIC flag. This generates position-independent code that can be linked and loaded at any address.

    The difference between -fPIC and -fpic relates to how the position-independent code is generated. On some architectures, only relatively small shared libraries can be built with -fpic while on others they do exactly the same thing. Unless you have a very good reason to use -fpic, just use -fPIC and things will work properly on every architecture.

  • Do not use the -fomit-frame-pointer compiler option. The libraries will still work, but debuggers will be useless. When you need a user to provide you with a traceback because of a bug in your code (or a savvy user wants a traceback to do his or her own debugging), it will not work.

  • When linking the library, use gcc rather than ld. The C compiler knows how to call the loader in order to link properly, and there is no guarantee that the interface to ld will remain constant.

  • When linking the library, do not forget to provide the soname. You use a special compiler option: -Wl passes options on to ld, with commas replaced with spaces. Use

    gcc -shared -Wl,-soname,soname -o libname filelist liblist  

    to build your library, where soname is the soname; libname is the name of the library, including the whole version number, such as libc.so.5.3.12; filelist is the list of object files that you want to put in the library; and liblist is the list of other libraries that provide symbols that will be accessed by this library. The last item is easy to overlook, because the library will still work without it on the system on which it was created, but it may not work in all other situations, such as when multiple libraries are available. For nearly every library, the C library should be included in that list, so explicitly place -lc at the end of this list.

    To create the file libfoo.so.1.0.1, with a soname of libfoo.so.1, from the object files foo.o and bar.o, use this invocation:

    gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0.1 foo.o bar.o \         
           -lc  
  • Do not strip the library unless you are in a particularly space-hungry environment. Shared libraries that have been stripped will still work, but they have the same general disadvantages as libraries built from object files compiled with -fomit-frame-pointer.

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
Choose an Ubuntu Version - The developers behind Ubuntu have worked to make the software as easy and flexible to install as possible. They understand that people will be installing Ubuntu on different types of computers (more...)
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...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.