All you should really know about Autoconf and Automake

Tagged:  •    •  

So, here is the shortest possible tutorial on the autotools.

The problem with autotools is that it is used for complicated things, and people cut-and-paste complicated things even when they ought to be simple. 99% of people just need a way to access .pc files and generate juicy Makefiles; the portability part is taken care by glib, sdl and so on.

You can use then the following basic autotools setup, which is just 9 lines. You can start from here and add more stuff (including libtool).

  • configure.ac:

AC_INIT([package], [version])
AM_INIT_AUTOMAKE([foreign subdir-objects])
AC_CONFIG_SRCDIR([configure.ac])
AC_CONFIG_HEADERS([config.h])     # not even really needed
AC_PROG_CC                        # or AC_PROG_CXX
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

  • Makefile.am:

bin_PROGRAMS = hello
hello_SOURCES = hello.c

That's enough for:

$ autoreconf -fvi
$ ./configure
$ make

On top of this, for each package you need, you add:

PKG_CHECK_MODULES([cairo], [cairo])
PKG_CHECK_MODULES([fontconfig], [fontconfig])

and

AM_CFLAGS = $(cairo_CFLAGS) $(fontconfig_CFLAGS)
LIBS += $(cairo_LIBS) $(fontconfig_LIBS)

respectively in configure.ac (after AC_PROG_CC) and Makefile.am.

Is that complicated?

Paolo, thanks so much for this. Was searching for a tutorial covering the correct layout for the configure.ac.

What files should I commit into a VCS then?

I understand most of the time it's copying from other configure.ac, even there's much documentation on autotools. You're completely right. People only needs a couple of minutes to look at how it works, and everyone can understand how it works. It's not all that complicated, and if you know how to use it (knowledge is really basic) it's way better than any other alternative out there.

I'd add the maintainer mode to your post :)

But I hate maintainer mode. :-)

Thanks for reposting this; not everyone's a redditor and this is can be extremely useful.

-lowell

(where's the name field? Weird. Anonymous it is.)

> this is can be extremely useful

Oops. You know what I mean.

In the 1st snippet, I suspect package and version are meant to be replaced with the actual package name and version. Is it the same for foreign?
Also, what does the simple case not need? Will it autodetect dependancies between .c/.h files ? I thought autoconf generates Makefile.am from Makefile.in ?

> Is it the same for foreign?

No foreign stays as is.

> Will it autodetect dependancies between .c/.h files?

Yes

> I thought autoconf generates Makefile.am from Makefile.in ?

autoconf [edit from Paolo: configure] generates Makefile from Makefile.in. automake generates Makefile.in from Makefile.am. So Makefile.am is all you need.

> In the 1st snippet, I suspect package and version are meant to be
> replaced with the actual package name and version.

Yes.

> Is it the same for foreign?

No.

> Also, what does the simple case not need?

The next step could be detecting libraries such as pthreads, with AC_SEARCH_LIBS or AC_CHECK_LIB.

> Will it autodetect dependancies between .c/.h files ?

Yes.

> I thought autoconf generates Makefile.am from Makefile.in ?

automake generates Makefile.in from Makefile.am, configure generates Makefile from Makefile.in.

autoreconf runs all four of aclocal, autoconf, autoheader and automake.

Do I need the [] for foreign, Makefile etc?

Yes, that's how arguments are delimited in m4.

Technically they're not needed if they do not contain m4 macro names or the macros do not expand to text containing commas, but it's far far safer to just wrap all arguments with brackets.

User login