Building Shared Libraries

written by: Lorent Konenbal; article published: year 2007, month 07;



In: Categories » Computers and technology » Linux » Building Shared Libraries

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.

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. How to use PuTTY Passphrase Agents
STEP1: Use Pageant to store your private keys in memory To make public key authentication more convenient, the developers of PuTTY created Pageant. Pageant is a program included with PuTTY that will keep your decrypted private keys in memory so you only have to enter your passphrase once rather than every time you authenticate to a server using public key authentication. While this will make your day-to-day use more convenient, please keep in mind that it also poses a slight risk, since other applications (inc...

2. Interactively transfer files from the command line with PSFTP
One method to transfer files from the Windows command line is to use PSFTP. PSFTP creates an interactive SFTP file transfer session where you can use many of the commands available within a normal FTP session. Since PSFTP uses the SFTP protocol, which is only available with servers running protocol SSHv2, you may not be able to run it on every server. PSFTP is run from the command line and provides numerous options. To see the options available run PSFTP with the –h option: ...

3. Using Plink to initiate an SSH session from the command line or a script
Using PuTTY from the command line will create an SSH interactive session. This may not be what we want if for example we need to remain at the Windows command line or we want to issue an SSH command from within a script. In order to satisfy these types of needs, PuTTY provides a tool called Plink. Plink is a command line tool that will allow you to log in to a remote machine using SSH and either create an SSH session or execute a command, all from the command line and without opening another window. Plink comes with many comma...

4. How to Generate a Key Pair Using OpenSSH
Problem: How can a key-pair be created in OpenSSH?STEP1: Generating your public/private key-pairThe ssh-keygen command is utilized to generate your public and private keys. OpenSSH provides authentication methods via a choice of three public key "cryptosystems": RSA1, RSA, and DSA. RSA1 works with SSHv1 while RSA and DSA are for SSHv2. RSA and DSA use different techniques for authenticating and have different capabilities, but for purposes of this guide, either will suffice.To create a key-pair, r...

5. Transfer files from the command line with PSCP
A second method to transfer files from a Windows command line prompt is to use PSCP. Unlike PSFTP, PSCP is not interactive and is designed to transfer files "in one shot" and then exit, much like OpenSSH's scp command. PSCP also allows you to specify wildcards within filenames (PSFTP does not). Additionally, PSCP will work with any SSH server as it is not dependent on SSHv2 being present. Note  PSCP will blindly copy files to the remote server, overwriting any files with the same name, without prompting for veri...

6. Create an SSH session from the command line using PuTTY
There are multiple ways to create an SSH session from the command line using PuTTY. The first way involves using the PuTTY program itself. PuTTY comes with a number of options that can be used to invoke the graphical PuTTY terminal from the command line. A description of these options is available within the PuTTY help file. To run PuTTY from the command line: Note  ...

7. Install SSH Windows Clients to Access Remote Machines Securely
Problem: Many times administrators will find themselves on a Windows machine with no way to access a remote server securely since Microsoft does not yet package an SSH client. There are a number of excellent tools available that provide SSH client connectivity from a Windows platform. A list of these tools is available at http://www.openSSH.com/windows.html. ...

8. How to use OpenSSH Passphrase Agents
Problem: Using public key authentication makes logging in to a server with SSH more secure, but less convenient due to having to type in a longer and more complex passphrase. STEP1: Use ssh-agent and ssh-add to store your private keys in memory To make public key authentication more convenient to use, the OpenSSH developers created the ssh-agent and ssh-add programs. These programs are designed to keep your private keys decrypted in memory for your current session. With ssh-agent, you will not ne...

9. Buffer Overflow
A buffer overflow occurs when a program or process tries to store more data in a temporary data storage area than it was intended to hold. Since buffers are created to contain a finite amount of data, the extra information can overflow into adjacent buffers, corrupting or overwriting the valid data held in them. Buffer overflows are a fertile source of bugs and malicious attacks. They occur when a program attempts to write data past the end of a buffer. A buffer is a contiguous allocated chunk of memory, such as an array ...

10. LINUX r Services
rlogind and rshd are the remote login and remote shell daemon. These so-called r services use TCP ports 513 and 514, respectively. The RLOGIN protocol is described in RFC 1282 and RSH in RFC. The r services were developed at Berkeley to provide seamless ("Look, Ma—no password") authentication between trusted hosts and/or users. Authentication between client and server is based on the client IP address, TCP port, and client username. The client IP address and username must match an entry in either the system-wi...