Autoconf, Automake, and Libtool: Creating Convenience Libraries with libtool

> | ### 10.2.4 Creating Convenience Libraries The third type of library which can be built with `libtool` is the convenience library. Modern compilers are able to create partially linked objects: intermediate compilation units which comprise several compiled objects, but are neither an executable or a library. Such partially linked objects must be subsequently linked into a library or executable to be useful. Libtool convenience libraries are partially linked objects, but are emulated by `libtool` on platforms with no native implementation. If you want to try this to see what `libtool` does on your machine, put the following code in a file ``trim.c'`, in the same directory as ``hello.c'` and ``libhello.la'`, and run the example shell commands from there:
>     #include <string.h>

>     #define WHITESPACE_STR  " \f\n\r\t\v"

>     /**
>      * Remove whitespace characters from both ends of a copy of
>      *  '\0' terminated STRING and return the result.
>      **/
>     char *
>     trim (char *string)
>     {
>       char *result = 0;

>       /* Ignore NULL pointers.  */
>       if (string)
>         {
>           char *ptr = string;

>           /* Skip leading whitespace.  */
>           while (strchr (WHITESPACE_STR, *ptr))
>             ++ptr;

>           /* Make a copy of the remainder.  */
>           result = strdup (ptr);

>           /* Move to the last character of the copy.  */
>           for (ptr = result; *ptr; ++ptr)
>             /* NOWORK */;
>           --ptr;

>           /* Remove trailing whitespace.  */
>           for (--ptr; strchr (WHITESPACE_STR, *ptr); --ptr)
>               *ptr = '\0';
>        }

>       return result;
>     }
To compile the convenience library with `libtool`, you would do this:
    $ libtool gcc -c trim.c
    rm -f .libs/trim.lo
    gcc -c  -fPIC -DPIC trim.c -o .libs/trim.lo
    gcc -c trim.c -o trim.o >/dev/null 2>&1
    mv -f .libs/trim.lo trim.lo
    $ libtool gcc -o libtrim.la trim.lo
    rm -fr .libs/libtrim.la .libs/libtrim.* .libs/libtrim.*
    ar cru .libs/libtrim.al trim.lo
    ranlib .libs/libtrim.al
    creating libtrim.la
    (cd .libs && rm -f libtrim.la && ln -s ../libtrim.la libtrim.la)
Additionally, you can use a convenience library as an alias for a set of zero or more object files and some dependent libraries. If you need to link several objects against a long list of libraries, it is much more convenient to create an alias:
    $ libtool gcc -o libgraphics.la -lpng -ltiff -ljpeg -lz
    rm -fr .libs/libgraphics.la .libs/libgraphics.* .libs/libgraphics.*
    ar cru .libs/libgraphics.al
    ranlib .libs/libgraphics.al
    creating libgraphics.la
    (cd .libs && rm -f libgraphics.la && \
    ln -s ../libgraphics.la libgraphics.la)
Having done this, whenever you link against ``libgraphics.la'` with `libtool`, all of the dependent libraries will be linked too. In this case, there are no actual objects compiled into the convenience library, but you can do that too, if need be.

This document was generated by Gary V. Vaughan on February, 8 2006 using texi2html