Back: Removing --foreign
Forward: Including Texinfo Documentation
FastBack: Including Texinfo Documentation
Up: A Large GNU Autotools Project
FastForward: Rolling Distribution Tarballs
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

12.3 Installing Header Files

One of the more difficult problems with GNU Autotools driven projects is that each of them depends on `config.h' (or its equivalent) and the project specific symbols that it defines. The purpose of this file is to be #included from all of the project source files. The preprocessor can tailor then the code in these files to the target environment.

It is often difficult and sometimes impossible to not introduce a dependency on `config.h' from one of the project's installable header files. It would be nice if you could simply install the generated `config.h', but even if you name it carefully or install it to a subdirectory to avoid filename problems, the macros it defines will clash with those from any other GNU Autotools based project which also installs its `config.h'.

For example, if Sic installed its `config.h' as `/usr/include/sic/config.h', and had `#include <sic/config.h>' in the installed `common.h', when another GNU Autotools based project came to use the Sic library it might begin like this:

#  include <config.h>

#  include <sic.h>

static const char version_number[] = VERSION;

But, `sic.h' says `#include <sic/common.h>', which in turn says `#include <sic/config.h>'. Even though the other project has the correct value for `VERSION' in its own `config.h', by the time the preprocessor reaches the `version_number' definition, it has been redefined to the value in `sic/config.h'. Imagine the mess you could get into if you were using several libraries which each installed their own `config.h' definitions. GCC issues a warning when a macro is redefined to a different value which would help you to catch this error. Some compilers do not issue a warning, and perhaps worse, other compilers will warn even if the repeated definitions have the same value, flooding you with hundreds of warnings for each source file that reads multiple `config.h' headers.

The Autoconf macro AC_OUTPUT_COMMANDS(25) provides a way to solve this problem. The idea is to generate a system specific but installable header from the results of the various tests performed by configure. There is a 1-to-1 mapping between the preprocessor code that relied on the configure results written to `config.h', and the new shell code that relies on the configure results saved in `config.cache'.

The following code is a snippet from `', in the body of the AC_OUTPUT_COMMANDS macro:

    # Add the code to include these headers only if autoconf has
    # shown them to be present.
    if test x$ac_cv_header_stdlib_h = xyes; then
      echo '#include <stdlib.h>' >> $tmpfile
    if test x$ac_cv_header_unistd_h = xyes; then
      echo '#include <unistd.h>' >> $tmpfile
    if test x$ac_cv_header_sys_wait_h = xyes; then
      echo '#include <sys/wait.h>' >> $tmpfile
    if test x$ac_cv_header_errno_h = xyes; then
      echo '#include <errno.h>' >> $tmpfile
    cat >> $tmpfile << '_EOF_'
#ifndef errno
/* Some sytems #define this! */
extern int errno;
    if test x$ac_cv_header_string_h = xyes; then
      echo '#include <string.h>' >> $tmpfile
    elif test x$ac_cv_header_strings_h = xyes; then
      echo '#include <strings.h>' >> $tmpfile
    if test x$ac_cv_header_assert_h = xyes; then
      cat >> $tmpfile << '_EOF_'

#include <assert.h>
#define SIC_ASSERT assert

        echo '#define SIC_ASSERT(expr)  ((void) 0)' >> $tmpfile

Compare this with the equivalent C pre-processor code from `sic/common.h', which it replaces:

#  include <stdlib.h>

#  include <unistd.h>

#  include <sys/wait.h>

#  include <errno.h>
#ifndef errno
/* Some systems #define this! */
extern int errno;

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

#  include <assert.h>
#  define SIC_ASSERT assert
#  define SIC_ASSERT(expr) ((void) 0)

Apart from the mechanical process of translating the preprocessor code, there is some plumbing needed to ensure that the `common.h' file generated by the new code in `' is functionally equivalent to the old code, and is generated in a correct and timely fashion.

Taking my lead from some of the Automake generated make rules to regenerate `Makefile' from `' by calling `config.status', I have added some similar rules to `sic/' to regenerate `common.h' from `'.

# Regenerate common.h with config.status whenever changes.
common.h: stamp-common
stamp-common: $(srcdir)/ $(top_builddir)/config.status
        cd $(top_builddir) \
          && CONFIG_FILES= CONFIG_HEADERS= CONFIG_OTHER=sic/common.h \
          $(SHELL) ./config.status
        echo timestamp > $@

The way that AC_OUTPUT_COMMANDS works, is to copy the contained code into config.status (see section C. Generated File Dependencies). It is actually config.status that creates the generated files -- for example, automake generated
`Makefile's are able to regenerate themselves from corresponding `'s by calling config.status if they become out of date. Unfortunately, this means that config.status doesn't have direct access to the cache values generated while configure was running (because it has finished its work by the time config.status is called). It is tempting to read in the cache file at the top of the code inside AC_OUTPUT_COMMANDS, but that only works if you know where the cache file is saved. Also the package installer can use the `--cache-file' option of configure to change the location of the file, or turn off caching entirely with `--cache-file=/dev/null'.

AC_OUTPUT_COMMANDS accepts a second argument which can be used to pass the variable settings discovered by configure into config.status. It's not pretty, and is a little error prone. In the first argument to AC_OUTPUT_COMMANDS, you must be careful to check that every single configure variable referenced is correctly set somewhere in the second argument.

A slightly stripped down example from the sic project `' looks like this:

# ----------------------------------------------------------------------
# Add code to config.status to create an installable host dependent
# configuration file.
# ----------------------------------------------------------------------
  if test -n "$CONFIG_FILES" && test -n "$CONFIG_HEADERS"; then
    # If both these vars are non-empty, then config.status wasn't run by
    # automake rules (which always set one or the other to empty).
  case "$CONFIG_OTHER" in
    dirname="sed s,^.*/,,g"

    echo creating $outfile
    cat > $tmpfile << _EOF_
/*  -*- Mode: C -*-
 * --------------------------------------------------------------------
 * DO NOT EDIT THIS FILE!  It has been automatically generated
 * from: and `echo $outfile|$dirname`.in
 * on host: `(hostname || uname -n) 2>/dev/null | sed 1q`
 * --------------------------------------------------------------------

#ifndef SIC_COMMON_H
#define SIC_COMMON_H 1

#include <stdio.h>
#include <sys/types.h>

    if test x$ac_cv_func_bzero = xno && \
       test x$ac_cv_func_memset = xyes; then
      cat >> $tmpfile << '_EOF_'
#define bzero(buf, bytes) ((void) memset (buf, 0, bytes))
    if test x$ac_cv_func_strchr = xno; then
      echo '#define strchr index' >> $tmpfile
    if test x$ac_cv_func_strrchr = xno; then
      echo '#define strrchr rindex' >> $tmpfile

    # The ugly but portable cpp stuff comes from here
    infile=$srcdir/sic/`echo $outfile | sed 's,.*/,,g;s,\..*$,,g'`
    sed '/^##.*$/d' $infile >> $tmpfile 


You will notice that the contents of `' are copied into `common.h' verbatim as it is generated. It's just an easy way of collecting together the code that belongs in `common.h', but which doesn't rely on configuration tests, without cluttering `' any more than necessary.

I should point out that, although this method has served me well for a number of years now, it is inherently fragile because it relies on undocumented internals of both Autoconf and Automake. There is a very real possibility that if you also track the latest releases of GNU Autotools, it may stop working. Future releases of GNU Autotools will address the interface problems that force us to use code like this, for the lack of a better way to do things.

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