Next: Adding source files to a project, Up: Tutorial [Contents][Index]
Here is a minimal example showing how to create and use a build system for a simple C program. Create the following three files in the same directory:
hello.c:
#include <stdio.h> int main (void) { printf ("hello, world\n"); }
Makefile.am:
AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=hello hello_SOURCES=hello.c
configure.ac:
AC_INIT([hello], [1.0]) AM_INIT_AUTOMAKE AC_PROG_CC AC_CONFIG_FILES([Makefile]) AC_OUTPUT
You can then run ‘autoreconf --install’ from within the directory
containing these files to create the build system. autoreconf
is the command for building and updating an Automake build system. It runs the
programs that are needed to create the Makefile.in template and
configure script.
You may now use the build system to build the hello
program,
by running ‘./configure’ (to create Makefile) followed by
‘make’, which follows the instructions in Makefile to build
hello
from its sources.
The Makefile.am and the configure.ac given here are explained below:
The Makefile.am given here consists of a series of variable definitions, each of which consists of the variable name, followed by an equals sign, followed by the variable’s value.
AUTOMAKE_OPTIONS=foreign
This line prevents
automake
requiring compliance with some aspects of the
GNU Coding Standards, such as the existence of a README file.
bin_PROGRAMS=hello
This line lists the programs in the project
that the build system should know how to build. The name of the
bin_PROGRAMS
variable demonstrates a frequent pattern for
Automake variables: the bin_
prefix on the variable indicates
that the programs listed in this variable are to be installed in a
directory for binary executable programs on the user’s system (known as
the bindir
), and the ‘PROGRAMS’ part tells Automake the type
of objects that are listed in the variable, in this case executable
programs to be built.
hello_SOURCES=hello.c
Finally, ‘hello_SOURCES’ contains the source files for the
hello
program. The name of this variable is based on the name
of the program given in ‘bin_PROGRAMS’, namely ‘hello’.
This file is a sequence of uses of macros. Macros in configure.ac that take arguments are followed by a comma-separated list of arguments within parentheses. The arguments are surrounded by square brackets.
AC_INIT([hello], [1.0]) AM_INIT_AUTOMAKE
The AC_INIT
macro usually appears at the beginning of
configure.ac, before any other macros. It is required by
Autoconf. The first argument given here is the name of the package, and
the second argument a version number for the package.
AM_INIT_AUTOMAKE
is required when Automake is used.
(The macros beginning ‘AC_’ come
from the Autoconf package, and macros beginning ‘AM_’ come from the
Automake package.)
AC_PROG_CC
AC_PROG_CC
checks for the presence of
a C compiler on the system where the configure script is run. If it
fails, the configure script will abort, printing an error message.
AC_CONFIG_FILES([Makefile]) AC_OUTPUT
AC_CONFIG_FILES
lists the files for the configure script to
create, here Makefile based on the input file Makefile.in.
AC_OUTPUT
outputs these files.
Next: Adding source files to a project, Up: Tutorial [Contents][Index]