Writing Free Software – Part 4: configure.ac


In this section, we’ll create a minimalist configure.ac, re-name our Makefile to Makefile.am, generate a configure script, and use this configure script to produce a Makefile. The end result won’t do much more than our manually-created Makefile. It will, however, allow us to make use of the autoconf and automake infrastructure in future lessons. So let’s get started.

Return to our workplace

$ cd ~/src/greeting

Create a configure.ac file

$ cat > configure.ac
AC_INIT([greeting],[0.0.1])
AM_INIT_AUTOMAKE([1.9 tar-ustar])
AC_PATH_PROG(CSC, gmcs, no)
if test "x$CSC" = "x" ; then
  AC_MSG_ERROR([Can't find "gmcs" in your PATH])
fi
AC_CONFIG_FILES([
Makefile
])
AC_OUTPUT
^D

Re-name your Makefile

$ mv Makefile Makefile.am

Install autoconf & automake

$ sudo apt-get install autoconf automake

Create files required by automake

$ for i in NEWS README AUTHORS ChangeLog ; do touch $i ; done

Import required m4 macros

$ aclocal

Ask automake to install missing pieces

$ automake -a

Process configure.ac and Makefile.am

$ autoreconf

Create a Makefile using configure script

$ ./configure

Test the targets we created before

$ make
gmcs -out:Greeting.exe Greeting.cs NDesk.Options.cs
$ make clean
rm Greeting.exe
$ make Greeting.exe
gmcs -out:Greeting.exe Greeting.cs NDesk.Options.cs

Verify that our code still runs

$ ./Greeting.exe -n 1
Greetings, Mercury!
$ ./Greeting.exe -1
Greetings, World!
$ ./Greeting.exe ––number 3
Greetings, Earth!

Try some of the targets that automake generates

$ touch Makefile.in && make Makefile
 cd . && /bin/sh ./config.status Makefile 
config.status: creating Makefile
make: `Makefile' is up to date.
$ make distdir
...
$ ls greeting-0.0.1
AUTHORS  ChangeLog  Makefile.am  NEWS    aclocal.m4  configure.ac  missing
COPYING  INSTALL    Makefile.in  README  configure   install-sh
$ make dist
...
$ ls greeting-0.0.1.tar.gz 
greeting-0.0.1.tar.gz

Conclusion

We have now created a simple autotools framework to do what we were doing manually. I’ll be honest: this is still pretty manual. In future lessons we’ll make some tweaks to it which make use of the extensive features of the system.

, , , , , ,

7 responses to “Writing Free Software – Part 4: configure.ac”

  1. Hi cj,

    Isn’t an `autoconf` required at any point? `autoreconf` tell me that I have to run `automake -a` to install `install-sh’ and `missing’ which where not copied the first time.

    Running `autoconf ; autoreconf ; automake -a’ in this order was the only way I found to have all this working running each tool a single time (autoconf-2.61, automake-1.10).

  2. Thanks for starting this series. Finally I get a change to really learn the basics of autotools.

  3. Hey there Romain,

    Yes, autoconf is required. See above in the “install autoconf & automake” section:

    $ sudo apt-get install autoconf automake

    $ ls -l `which automake` /etc/alternatives/automake
    lrwxrwxrwx 1 root root 22 Aug 6 22:35 /etc/alternatives/automake -> /usr/bin/automake-1.10
    lrwxrwxrwx 1 root root 26 Aug 6 22:35 /usr/bin/automake -> /etc/alternatives/automake

    $ dpkg -S `which autoconf` `which autoreconf` `which automake-1.10`
    autoconf: /usr/bin/autoconf
    autoconf: /usr/bin/autoreconf
    automake: /usr/bin/automake-1.10

    You were correct about a missing piece. I forgot to mention running aclocal to import all of the required m4 macros. It is now above.

    Thanks!

    C.J.

  4. Okay, running `aclocal` before made it work smoothly :)

    I should admit I have never been able to gather enough motivation to take the time to read the autotools documentation (to the end). Such a serie of short howtos is really a sweet thing!

    Thank you!

  5. I thank you very much as well, for the same reasons David and Roman pointed out :)

Leave a Reply