Open Source

using GNU Autoconf and Automake

When you have a project that needs to be compiled under all the Linux, BSD, Unix, and all the other junk OS outside here you’ll soon end up with a tons of block of source code that need conditional compiling, i.e. #ifdef – #endif

Writing a Makefile by hand that automatically detect your OS and the configuration of wich compiler and libraries are installed and located is an impossible task nowadays.

Using some GNU tool you can automatically generate scripts that those this job for you: those tools are autoconf and automake.

How they work, quick summary:

  1. you’ll write a set of rules in a file called
  2. automake will translate this in
  3. you’ll write a set of rules in a file called
  4. autoconf will translate this in configure
  5. you need to generate a template using autoheader
  6. a developer will just run the script ./configure
  7. configure will translate any into the finale Makefile
  8. the developer will just run make now

So you just need to learn how to:

  1. the syntax to create a file for every directory that needs a final Makefile
  2. the syntax to create a that will generate later all the final Makefiles

A skeleton configuration

A simple for a small project:

bin_PROGRAMS = hello
hello_CFLAGS= -Wall -Os
hello_SOURCES = test-ansi.c
hello_LDADD = ../lib/libbadpenguin.a
hello_LDFLAGS = -static
hello_HEADERS = hello.h

The key variable here is “bin_PROGRAMS” that specify with program this Makefile will generate. At each program name you can add an additional “_PARAM”. The most important is ofc the “_SOURCES” that will handle compilation of source code and dependencie handling withing the Makefile when generated.

A simple for a small project:

AC_CHECK_LIB(z,zlibVersion,,AC_MSG_ERROR([Cannot find libz]))
AC_CHECK_HEADERS([alloca.h langinfo.h libintl.h])

Withing this file the lines starting with dnl are indicating a comment.

The important lines here are:

  • AC_INIT that points to our main source file that is used to initialize autoconf
  • AM_INIT_AUTOMAKE that specify a project name and a version that is used to initialize usage of automake withing autoconf
  • AM_CONFIG_HEADER that specify config.h as the header where to write all the scanned/parsed output
  • AC_PROG_* that check the presence of the compiler and other tools
  • AC_CHECK_* that check the presence of libraries, headers and functions within the system and will automatically gererate #define withing config.h that you need to include withing your source files
  • AC_OUTPUT specify the location of all the Makefile to generate

We’re missing that will be generated below by autoheader.

If you don’t want to learn all by yourself all the checks that you can use withing you can use the tool autoscan that will try to generate automatically a file for you scanning your entire project source files and headers. You can find the output of this tool in configure.scan file and compare it against your

When you’ve finished to create your templates you can try to compile everything:

automake "“add-missing "“foreign