The Autotools are de facto standard for building C/C++ code in the Linux world. It is a very flexible build system, but because of its flexibility it has a quite steep learning curve. Here is the bare minimum example that will get you started.
The bare minimum
To build a simple hello world program with Autotools you just need 3 files. Here is the file structure:
bare-minimum-example
├── Makefile.am
├── configure.ac
└── main.c
The configure
will be created from configure.ac
.
#configure.ac
AC_INIT([hello-world], 1.0)
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
And a Makefile.am
to generate a Makefile.in
from which the final Makefile
will be created.
Contents of hello.c
:
//hello.c
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello world!\n");
return 0;
}
Building
First you need to transform your configure.ac
into a configure
script. This is done by the autoconf
. The configure
script uses a Makefile.in
to produce the final Makefile
. The Makefile.in
is usually generated by the automake
from a Makefile.am
. Are you still with me?
configure.ac -> |autoconf| -> configure -> | |
|./configure| -> Makefile
Makefile.am -> |automake| -> Makefile.in -> | |
You can use autoreconf
to run autoconf
and automake
for you.
$ autoreconf -vfi
Then you can run the produced ./configure
script and build you application.
$ ./configure
$ make
$ ./hello
Hello world!
And that’s pretty much it.
Next steps
This example is everything you need to know in order to build a simple program with Autotools . If your scenario is more complex, then with the help of the official documentation you will be able extend it.