Section 8.4. Building Shared Libraries

   


8.4. 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.[3]

    [3] 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.


       
    top
     


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net