Converting to autoconf and automake

autoconf and automake are two tools made by GNU to automate the building of relatively complicated programs. They are also made to make distribution of programs easier for different systems. So when ever the source code of your program becomes larger than several files (maybe 4 or 5 non header source files) or you want to give your program to others to compile and use, it is very convenient (nearly vital!) to switch to these tools instead of using the simple make tool.

Like all the other GNU software, both these two tools have fantastic manuals written by the authors of the programs, no one else knows them better. You can get the manuals in various formats from the webpages of these free tools. Therefore, this webpage is not meant to replace those excellent manuals. If you haven't read them, please stop right here and go and read them. For me, it is just a shared note to keep the steps that were relevent for my work in one place. The automake manual has a very simple and nice to understand example of how to use these two tools.

  1. Make the Makefile.ams according to automake manual in all the directories. As in the automake manual example, for a start, a simple Makefile.am with one line in the top directory is enough (assuming your source files are in the src directory):
         SUBDIRS = src
    And then to make another one in the src directory with all the needed libraries and compiler flags, for example:
         AM_CFLAGS = -Wall -O3
         AM_LDFLAGS = -lpthread -lm

         bin_PROGRAMS = progname
         progname_SOURCES = main.c other1.c other1.h other2.c other2.h
    Ofcourse, replace progname with your program's name and add as many other.c and .h source files as you like. All their dependencies will be found my automake, so don't worry about that.
    If they don't fit in one line, put a `\` at the end of the line and go to the next.
  2. Run autoscan in the top directory and open configure.scan in a text editor to make the following changes:
    • Fix the package name, version and bug information. Also add all the other fields of AC_INIT: the package file name and URL.
    • Add the AM_INIT_AUTOMAKE([-Wall -Werror foreign]) after AC_INIT.
    • If you are linking to libraries or need non-standard headers, then autoscan will use AC_CHECK_LIB or AC_CHECK_HEADERS to check for their presence. But this manner will not cause ./configure to stop if the library doesn't exist. In order to make it stop add these two arguments to these functions: [], [AC_MSG_ERROR([wcslib not installed.])]. So for example if autoscan gives you this:
           AC_CHECK_LIB([pthread], [main])
      Then change it to:
           AC_CHECK_LIB([pthread], [main], [], [AC_MSG_ERROR([No Go.])])
      You can change the No Go to any message you like, that might be suited to the particular library or header.
    • If you want to add some variables to `config.h` (for example the number of threads available to the system), then you can add something like this:
           AC_MSG_CHECKING([for number of available threads])
           AC_DEFINE_UNQUOTED([NOISECHISEL_NUM_THREADS], [`nproc`],
                [Number of threads available to system.])
           AC_MSG_RESULT([$(nproc)])
      nproc is the command that will print the number of available theads to a program. You can replace it with any other command you like to check for other things.
    • By default, the CFLAGS or -O2 -g will be used as CFLAGS, even when you specify AM_CFLAGS in your Makefile.am, these two will still appear. So if you don't want debugging information or another kind of optimization, you can remove this default by adding this line before AC_PROG_CC:
           ${CFLAGS=""}
  3. Rename configure.scan to configure.ac:
         mv configure.scan configure.ac
  4. Run autoreconf:
    • If it is the first time you are using these tools on your source code, run it with the --install. This will add all the other necessary files for autoconf and automake to work.
    • Later on, when you modify configure.ac or Makefile.ams, you will need to update all the fils that your first autoreconf run has made. To update them, simply run it without any options.
    • In case you want to see what autoreconf does, run it (in either case) also call the -v option.

You can now configure and make your system with the two commands ./configure and make and the user of your program can install it with make install in the standard methods.

One other very useful option that automake provides is make distcheck, by running it, a script will be run to put your code in a nice and standard compressed (.tar.gz) file. Before doing so, it will also do some checks to see if it works in some different conditions and will warn you if it doesn't.

A suggestion
In case the reason you are converting to automake and autoconf is to make your program easier for others to use, you might also benefit a lot from GNU libc's Argp interface. It will parse the arguments your users pass on the command line and produce standard --help, --usage and --version outputs.

 

This page was created on October 1st, 2014.

Tohoku UniversityAstronomical Institute, 6-3 Aramaki, Aoba-ku, Sendai, Japan, 980-8578
    akhlaghi :a:t: astr.tohoku.ac.jp, Office: Physics A; 838, +81-80-3335-9474