Back: Creating Static Libraries with libtool
Forward: Linking an Executable
FastBack: Linking an Executable
Up: The Libtool Library
FastForward: Linking an Executable
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

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 `', 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))

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

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

      /* 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 trim.lo
rm -fr .libs/ .libs/libtrim.* .libs/libtrim.*
ar cru .libs/ trim.lo
ranlib .libs/
(cd .libs && rm -f && ln -s ../

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 -lpng -ltiff -ljpeg -lz
rm -fr .libs/ .libs/libgraphics.* .libs/libgraphics.*
ar cru .libs/
ranlib .libs/
(cd .libs && rm -f && \
ln -s ../

Having done this, whenever you link against `' 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