Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Auto Tools Tutorial

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8
At a glance
Powered by AI
The automake and autoconf tools can be used to manage C++ projects under Unix by generating standardized Makefiles and configure scripts. They aim to simplify and standardize the build process.

Automake and autoconf are used to simplify the process of generating Makefiles and configure scripts that conform to GNU standards. Automake generates Makefiles from Makefile.am files, while autoconf generates a configure script from configure.ac.

Automake and autoconf use files like aclocal.m4, configure.ac, Makefile.am and configure. Automake looks for Makefile.am files and generates Makefiles, while autoconf looks for configure.ac and generates the configure script.

Using Automake and Autoconf with C++

1 de 7
Home
Projects
http://www.openismus.com/documents/linux/automake/a...
Documents
About
Back to Documents
Using Automake and Autoconf with C++
Contents
Introduction
make and con gure
automake and autoconf
Subdirectories
Example Files
Recommended Reading
Introduction
The automake and autoconf tools can be used to manage C++
projects under Unix. They should save a lot of time compared to
writing Make les and con gure scripts manually, while ensuring
that your project is structured according to GNU standards.
However, it is di cult for beginners to get started. Hopefully, this
tutorial will provide enough information for C++ programmers who
are new to Unix to create their rst C++ projects, while gaining a
super cial understanding of what the tools are doing.
If you nd any problem with this page, we welcome feedback or
patches against the example code, which can be downloaded from
the Gitorious project page. Either send a merge request or email
murrayc@openismus.com.
make and con gure
The make tool can be used to manage multi- le projects. make
07/12/12 13:29

Using Automake and Autoconf with C++


2 de 7
http://www.openismus.com/documents/linux/automake/a...
uses the Make le le in your project folder, which lists the various
compiling and linking steps, targets, and dependencies. make is
well explained in C-Scene: Multi-File Projects and the GNU
Make Utility.
A con gure script can be used to aid cross-platform compiling. A
suitable con gure script should interpret a Make le.in le and
then create a platform-speci c Make le le. It will do this after
performing several tests to determine the characteristics of the
platform.
This allows a user to type ./con gure and then make to compile a
project on her system.
automake and autoconf
Obviously most well-written Make les and con gure scripts will look
very similar. In fact, GNU provide guidelines about what should be in
these les. Therefore, GNU created automake and autoconf to
simplify the process and ensure that the Make le and con gure
script conform to GNU standards.
Here is a brief explanation of how these tools are used. You can see
examples of the les used by these tools in the Example Files
section.
Note: These tools use the M4 programming language. aclocal adds
aclocal.m4 to your project directory, which de nes all M4 macros
which are used by the con gure script.
autoconf
autoconf looks for a le called con gure.ac (or con gure.in for
backward compatibility). It then runs the M4 macro processor to
create the con gure script.
Whenever you add a macro call to con gure.ac, aclocal should be
executed as well as autoconf, because aclocal scans con gure.ac
to nd which macros it should provide. However, nowadays one can
simply run the autoreconf utility, which takes care of those details
automatically.
07/12/12 13:29

Using Automake and Autoconf with C++


3 de 7
http://www.openismus.com/documents/linux/automake/a...
Lines which every con gure.ac should have
Every con gure.ac should have lines like the following:
AC_INIT([Hello], [0.1], [bug-report@hello.example.com], [hello], [http://hello.e
xample.com/])
AC_PREREQ([2.59])
AM_INIT_AUTOMAKE([1.10 no-define])
AC_CONFIG_HEADERS([config.h])
AC_PROG_CXX
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
The AC_INIT macro initializes autoconf with information about your
project, including the project name, version number, bug-reporting
address, tarball name and the project homepage.
The AM_INIT_AUTOMAKE line adds several standard checks and
initializes automake.
AC_PROG_CXX checks for a C++ compiler. If your project uses C,
you can check for a C compiler with AC_PROG_CC.
AC_CONFIG_FILES lists the les to be generated by con gure. By
default, each le is generated from a template le of the same
name but with an .in extension appended.
AC_OUTPUT nishes con gure processing, and generates the
output les.
Using a Con g Header
The AC_CONFIG_HEADERS([con g.h]) line indicates that you will
be using a con g.h le. autoconf will then need a con g.h.in le,
which it processes to create the con g.h le. The generated header
is included by your source code to pick up the results of the various
con guration tests in the form of C preprocessor macro de nitions.
The template le con g.h.in can also be generated automatically
by the autoheader tool. autoreconf will invoke autoheader when
AC_CONFIG_HEADERS is used in con gure.ac. If multiple
con guration headers are used, autoheader will always only
generate the template for the rst one in the list.
automake
automake looks for a le called Make le.am. It then creates a
07/12/12 13:29

Using Automake and Autoconf with C++


4 de 7
http://www.openismus.com/documents/linux/automake/a...
Make le.in, based on the macros which it
by the con gure script (see above).
GNU-style projects, or not

nds. This is later used

Because automake tries to make a GNU-style project by default, it


will add a COPYING le and complain if some other necessary
informative text les are missing. For the moment, you can create
blank les with the following command, and ll them in later:
touch NEWS README AUTHORS ChangeLog
If you do not want these GNU-style les, then you could add the
'foreign' argument to your AM_INIT_AUTOMAKE call in con gure.ac
instead:
AM_INIT_AUTOMAKE([1.10 no-define foreign])
Telling automake about your source les
Use lines like the following to name your program and list its source
les:
bin_PROGRAMS = hello
hello_SOURCES = hello.h hello.cc main.cc
Note that the name of the variable hello_SOURCES is pre xed with
the name of the target de ned by the assignment to
bin_PROGRAMS. This is a common practice with autoconf and
automake. Similarly, the pre x of the bin_PROGRAMS variable also
has a meaning: It refers to the prede ned variable bindir, which
de nes the installation directory for user-visible executable les.
Thus, the rst assignment instructs automake to generate build
rules for an executable target le called "hello", which will be
installed into the directory named by bindir on execution of make
install. The second assignment instructs Automake to generate rules
for building the "hello" target from the three C++ source les listed.
The Whole Process
Assuming that you have written appropriate Make le.am and
con gure.ac les (there are examples below), you should be able to
build your project by entering the following commands:
07/12/12 13:29

Using Automake and Autoconf with C++


5 de 7
http://www.openismus.com/documents/linux/automake/a...
'touch NEWS README AUTHORS ChangeLog'
'autoreconf --force --install' - runs aclocal, autoconf,
autoheader and automake in the right order to create
con g.h.in, Make le.in, con gure and a number of auxiliary
les
'./con gure' - creates Make le from Make le.in and con g.h
from con g.h.in
'make'
Just repeat the last three steps to completely recon gure the
project. Most projects kept in version control have an autogen.sh
script that runs everything up to the con gure step. This is done to
provide a standard means for generating the build les from
scratch, as generated les should never be put under version
control. Release tarballs ship with pre-generated con gure and
Make le.in les, so that the installer does not need to have
autoconf or automake installed.
Subdirectories
Project les should, of course, be organised in subdirectories.
Ideally, all of the source les should be in a directory called 'src' in
the project directory, with all of the other les (such as Make les,
con gure scripts, and READMEs) separate at the top. Projects which
have several levels of directories are called 'deep' projects. The
necessary steps are listed here, but you should look at the Example
Files section to see them in context.
Example Files
Here are some example con gure.ac and Make le.am les. They
are su cient to manage a C++ project which only uses the C++
Standard Library.
The example uses non-recursive Make le rules, which makes
things simpler and allows faster builds. Many existing projects use
recursive make, with a Make le.am in each subdirectory. That
generates a small non-installed library in each directory, which must
be linked together (in the right order) into a binary. This is one of the
main problems with recursive make.
07/12/12 13:29

Using Automake and Autoconf with C++


6 de 7
http://www.openismus.com/documents/linux/automake/a...
See the automake and autoconf manuals in the recommended
reading section for information on the macros and variable names
used in these les.
These examples are for a 'deep' project with the following structure:
helloworld_cc
autogen.sh
configure.ac
Makefile.am
src
helloworld.h
helloworld.cc
main.cc
foo les
foo.h
foo.cc
con gure.ac
AC_INIT([Helloworld C++], [0.5], [bug-report@hello.example.com],
[helloworld_cc], [http://hello.example.com/])
AC_PREREQ([2.59])
AM_INIT_AUTOMAKE([1.10 -Wall no-define])
AC_CONFIG_HEADERS([config.h])
AC_PROG_CXX
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
Make le.am
AUTOMAKE_OPTIONS = subdir-objects
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}
bin_PROGRAMS = hello
hello_SOURCES = src/hello.h src/hello.cc src/main.cc \
src/foo/foo.h src/foo/foo.cc
dist_noinst_SCRIPTS = autogen.sh
You may download the helloworld_cc-0.5.tar.gz tar archive of the
simple example project, or browse through the source at Gitorious.
Recommended Reading
Building C/C++ libraries with automake and autoconf
Using C/C++ libraries with automake and autoconf
C-Scene: Multi-File Projects and the GNU Make Utility
Autotools Mythbuster
GNU autoconf manual
07/12/12 13:29

Using Automake and Autoconf with C++


7 de 7
http://www.openismus.com/documents/linux/automake/a...
GNU automake manual
Using GNU Autotools
Autotools: A practitioner's guide to Autoconf, Automake
and Libtool
Translations
We know of the following translations of this article:
Ukranian
Copyright Murray Cumming, Openismus GmbH.
This work is licensed under a Creative Commons License.
Openismus | Impressum
07/12/12 13:29

You might also like