Back: Library Versioning
Forward: A Large GNU Autotools Project
FastBack: Using GNU Libtool
Up: Using GNU Libtool
FastForward: A Large GNU Autotools Project
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

11.5 Convenience Libraries

Sometimes it is useful to group objects together in an intermediate stage of a project's compilation to provide a useful handle for that group without having to specify all of the individual objects every time. Convenience libraries are a portable way of creating such a partially linked object: Libtool will handle all of the low level details in a way appropriate to the target host. This section describes the use of convenience libraries in conjunction with Automake. The principles of convenience libraries are discussed in Creating Convenience Libraries.

The key to creating Libtool convenience libraries with Automake is to use the
`noinst_LTLIBRARIES' macro. For the Libtool libraries named in this macro, Automake will create Libtool convenience libraries which can subsequently be linked into other Libtool libraries.

In this section I will create two convenience libraries, each in their own subdirectory, and link them into a third Libtool library, which is ultimately linked into an application.

If you want to follow this example, you should create a directory structure to hold the sources by running the following shell commands:

$ mkdir convenience
$ cd convenience
$ mkdir lib
$ mkdir replace

The first convenience library is built from two source files in the `lib' subdirectory.

  1. `source.c':

    #  include <config.h>
    #if HAVE_MATH_H
    #  include <math.h>
    foo (double argument)
      printf ("cos (%g) => %g\n", argument, cos (argument));

    This file defines a single function to display the cosine of its argument on standard output, and consequently relies on an implementation of the cos function from the system libraries. Note the conditional inclusion of `config.h', which will contain a definition of `HAVE_MATH_H' if `configure' discovers a `math.h' system header (the usual location for the declaration of cos). The `HAVE_CONFIG_H' guard is by convention, so that the source can be linked by passing the preprocessor macro definitions to the compiler on the command line -- if `' does not use `AM_CONFIG_HEADER' for instance.

  2. `source.h':

    extern void foo        (double argument);

    For brevity, there is no #ifndef SOURCE_H guard. The header is not installed, so you have full control over where it is #includeed, and in any case, function declarations can be safely repeated if the header is accidentally processed more than once. In a real program, it would be better to list the function parameters in the declaration so that the compiler can do type checking. This would limit the code to working only with ANSI compilers, unless you also use a PARAMS macro to conditionally preprocess away the parameters when a K&R compiler is used. These details are beyond the scope of this convenience library example, but are described in full in 9.1.6 K&R Compilers.

You also need a `' to hold the details of how this convenience library is linked:

## -- Process this file with automake to produce

noinst_LTLIBRARIES        =
library_la_SOURCES        = source.c source.h
library_la_LIBADD        = -lm

The `noinst_LTLIBRARIES' macro names the Libtool convenience libraries to be built in this directory, `'. Although not required for compilation, `source.h' is listed in the `SOURCES' macro of `' so that correct source dependencies are generated, and so that it is added to the distribution tarball by automake's `dist' rule.

Finally, since the foo function relies on the cos function from the system math library, `-lm' is named as a required library in the `LIBADD' macro. As with all Libtool libraries, interlibrary dependencies are maintained for convenience libraries so that you need only list the libraries you are using directly when you link your application later. The libraries used by those libraries are added by Libtool.

The parent directory holds the sources for the main executable, `main.c', and for a (non-convenience) Libtool library, `error.c' & `error.h'.

Like `source.h', the functions exported from the Libtool library `' are listed in `error.h':

extern void gratuitous          (void);
extern void set_program_name    (char *path);
extern void error               (char *message);

The corresponding function definitions are in `error.c':

#include <stdio.h>

#include "source.h"

static char *program_name = NULL;

gratuitous (void)
  /* Gratuitous display of convenience library functionality!  */
  double argument = 0.0;
  foo (argument);

set_program_name (char *path)
  if (!program_name)
    program_name = basename (path);

error (char *message)
  fprintf (stderr, "%s: ERROR: %s\n", program_name, message);
  exit (1);

The gratuitous() function calls the foo() function defined in the `' convenience library in the `lib' directory, hence `source.h' is included.

The definition of error() displays an error message to standard error, along with the name of the program, program_name, which is set by calling set_program_name(). This function, in turn, extracts the basename of the program from the full path using the system function, basename(), and stores it in the library private variable, program_name.

Usually, basename() is part of the system C library, though older systems did not include it. Because of this, there is no portable header file that can be included to get a declaration, and you might see a harmless compiler warning due to the use of the function without a declaration. The alternative would be to add your own declaration in `error.c'. The problem with this approach is that different vendors will provide slightly different declarations (with or without const for instance), so compilation will fail on those architectures which do provide a declaration in the system headers that is different from the declaration you have guessed.

For the benefit of architectures which do not have an implementation of the basename() function, a fallback implementation is provided in the `replace' subdirectory. The file `basename.c' follows:

#  include <config.h>

#  include <string.h>
#  include <strings.h>

#  ifndef strrchr
#    define strrchr rindex
#  endif

basename (char *path)
  /* Search for the last directory separator in PATH.  */
  char *basename = strrchr (path, '/');
  /* If found, return the address of the following character,
     or the start of the parameter passed in.  */
  return basename ? ++basename : path;

For brevity, the implementation does not use any const declarations which would be good style for a real project, but would need to be checked at configure time in case the end user needs to compile the package with a K&R compiler.

The use of strrchr() is noteworthy. Sometimes it is declared in `string.h', otherwise it might be declared in `strings.h'. BSD based Unices, on the other hand, do not have this function at all, but provide an equivalent function, rindex(). The preprocessor code at the start of the file is designed to cope with all of these eventualities. The last block of preprocessor code assumes that if strrchr is already defined that it holds a working macro, and does not redefine it.

`' contains:

## Process this file with automake to produce

noinst_LTLIBRARIES      =
libreplace_la_SOURCES   = dummy.c
libreplace_la_LIBADD    = @LTLIBOBJS@

Once again, the `noinst_LTLIBRARIES' macro names the convenience library,
`'. By default there are no sources, since we expect to have a system definition of basename(). Additional Libtool objects which should be added to the library based on tests at configure time are handled by the `LIBADD' macro. `LTLIBOBJS' will contain `basename.lo' if the system does not provide basename, and will be empty otherwise. Illustrating another feature of convenience libraries: on many architectures, `' will contain no objects.

Back in the toplevel project directory, all of the preceding objects are combined by another `':

## Process this file with automake to produce

SUBDIRS                 = replace lib .

CPPFLAGS                = -I$(top_srcdir)/lib

include_HEADERS         = error.h

lib_LTLIBRARIES         =
liberror_la_SOURCES     = error.c
liberror_la_LDFLAGS     = -no-undefined -version-info 0:0:0
liberror_la_LIBADD      = replace/ lib/

bin_PROGRAMS            = convenience
convenience_SOURCES     = main.c
convenience_LDADD       =

The initial `SUBDIRS' macro is necessary to ensure that the libraries in the subdirectories are built before the final library and executable in this directory.

Notice that I have not listed `error.h' in `liberror_la_SOURCES' this time, since `' is an installed library, and `error.h' defines the public interface to that library. Since the `' Libtool library is installed, I have used the `-version-info' option, and I have also used `-no-undefined' so that the project will compile on architectures which require all library symbols to be defined at link time -- the reason program_name is maintained in `liberror' rather than `main.c' is so that the library does not have a runtime dependency on the executable which links it.

The key to this example is that by linking the `' and `' convenience libraries into `', all of the objects in both convenience libraries are compiled into the single installed library, `'. Additionally, all of the inter-library dependencies of the convenience libraries (`-lm', from `') are propagated to `'.

A common difficulty people experience with Automake is knowing when to use a `LIBADD' primary versus a `LDADD' primary. A useful mnemonic is: `LIBADD' is for ADDitional LIBrary objects. `LDADD' is for ADDitional linker (LD) objects.

The executable, `convenience', is built from `main.c', and requires only `'. All of the other implicit dependencies are encoded within `'. Here is `main.c':

#include <stdio.h>
#include "error.h"

main (int argc, char *argv[])
  set_program_name (argv[0]);
  gratuitous ();
  error ("This program does nothing!");

The only file that remains before you can compile the example is `':

# Process this file with autoconf to create configure.


AC_INIT([convenience], [2.0], [[email protected]])

AM_INIT_AUTOMAKE([1.9 foreign])


AC_CHECK_HEADERS([string.h strings.h], [break])


AC_CONFIG_FILES([replace/Makefile lib/Makefile Makefile])

There are checks for all of the features used by the sources in the project: `math.h' and either `string.h' or `strings.h'; the existence of strrchr (after the tests for string headers); adding `basename.o' to `LIBOBJS' if there is no system implementation; and the shell code to set `LTLIBOBJS'.

With all the files in place, you can now bootstrap the project:

$ ls -R
.:  error.c  error.h  lib  main.c  replace

lib:  source.c  source.h

replace:  basename.c
$ aclocal
$ autoheader
$ automake --add-missing --copy
automake: installing ./install-sh
automake: installing ./mkinstalldirs
automake: installing ./missing 7: required file ./ltconfig not found
$ autoconf
$ ls -R
.:   error.c     ltconfig   mkinstalldirs   config.sub    error.h  replace
aclocal.m4    configure     install-sh  main.c
config.guess  lib         missing

lib:  source.c  source.h

replace:  basename.c

With these files in place, the package can now be configured:

$ ./configure
checking how to run the C preprocessor... gcc -E
checking for math.h... yes
checking for string.h... yes
checking for strrchr... yes
checking for basename... yes
updating cache ./config.cache
creating ./config.status
creating replace/Makefile
creating lib/Makefile
creating Makefile
creating config.h

Notice that my host has an implementation of basename().

Here are the highlights of the compilation itself:

$ make
Making all in replace
make[1]: Entering directory /tmp/replace
/bin/sh ../libtool --mode=link gcc  -g -O2  -o
rm -fr .libs/ .libs/libreplace.* .libs/libreplace.*
ar cru .libs/
ranlib .libs/
(cd .libs && rm -f && ln -s ../ \
make[1]: Leaving directory /tmp/replace

Here the build descends into the `replace' subdirectory and creates `', which is empty on my host since I don't need an implementation of basename():

Making all in lib
make[1]: Entering directory /tmp/lib
/bin/sh ../libtool --mode=compile gcc -DHAVE_CONFIG_H  -I. -I. \
-g -O2 -c source.c
rm -f .libs/source.lo
gcc -DHAVE_CONFIG_H -I. -I. -g -O2 -c -fPIC -DPIC source.c \
-o .libs/source.lo
gcc -DHAVE_CONFIG_H -I. -I. -g -O2 -c source.c \
-o source.o >/dev/null 2>&1
mv -f .libs/source.lo source.lo
/bin/sh ../libtool --mode=link gcc  -g -O2  -o source.lo -lm
rm -fr .libs/ .libs/library.* .libs/library.*
ar cru .libs/ source.lo
ranlib .libs/
(cd .libs && rm -f && ln -s ../
make[1]: Leaving directory /tmp/lib

Next, the build enters the `lib' subdirectory to build `'. The `configure' preprocessor macros are passed on the command line, since no `config.h' was created by AC_CONFIG_HEADER:

Here, `main.c' is compiled (not to a Libtool object, since it is not compiled using libtool), and linked with the `' Libtool library:

gcc -DHAVE_CONFIG_H -I. -I.  -I./lib  -g -O2 -c main.c
/bin/sh ./libtool --mode=link gcc  -g -O2  -o convenience  main.o \
gcc -g -O2 -o .libs/convenience main.o ./.libs/ -lm \
-Wl,--rpath -Wl,/usr/local/lib
creating convenience
make[1]: Leaving directory /tmp/convenience

libtool calls gcc to link the convenience executable from `main.o' and the shared library component of `'. libtool also links with `-lm', the propagated inter-library dependency of the `' convenience library. Since `' and `' were convenience libraries, their objects are already present in `', so they are not listed again in the final link line -- the whole point of convenience archives.

This just shows that it all works:

$ ls
Makefile      config.h  install-sh   main.c    convenience   lib          main.o   config.log     error.c  missing
aclocal.m4    config.status  error.h       libtool      mkinstalldirs
config.cache  config.sub     error.lo      ltconfig     replace
config.guess  configure      error.o
$ libtool --mode=execute ldd convenience => /tmp/.libs/ (0x40014000) => /lib/ (0x4001c000) => /lib/ (0x40039000)
        /lib/ => /lib/ (0x40000000)
$ ./convenience
cos (0) => 1
lt-convenience: ERROR: This program does nothing!

Notice that you are running the uninstalled executable, which is in actual fact a wrapper script, See section 10.5 Executing Uninstalled Binaries. That is why you need to use libtool to run ldd on the real executable. The uninstalled executable called by the wrapper script is called lt-convenience, hence the output from basename().

Finally, you can see from the output of ldd, that convenience really isn't linked against either `' and `'.

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