Building Shared Libraries

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


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

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

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.

Disclaimer

1) E-articles 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 infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article 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.

link to this article