Autotools Tips (configure.ac, Makefile.am, autoconf, automake, etc)

Posted by Benjamin Close on November 7, 2008 under OpenSource, Programming | 2 Comments to Read

The Autotools build system is a great system. It consists of a number of tools. These are

aclocal[1]autoconf[2]automake[3]autoheader[4], and to some extent libtool[5]

These tools all work together to aid in building applications, libraries and providing a consistent framework for doing so. The problem however, is the tools are hard to use, poorly documented and the learning curve to getting things done is extremely high. Added to this, the tutorials on the web show only the basics of using the system and leave all the useful stuff to the user. The problem is the useful stuff is hard to work out.

Hence this page is not about showing you yet another hello world autotools setup. Instead it give you tips on how to use the autotools and what you can put in those highly cryptic config files: configure.acMakefile.am,config.in.

configure.ac/configure.in Tips

This page lists common things that one often needs to do with autoconf. These can be placed either in theconfigure.ac file or if the project is old enough configure.in

Checking Which Operating System The User is Using

This check test to see if the build is for Linux, BSD, Mac, etc. This allows you to tune specific options depending on the platform. This test goes in the configure.ac or configure.in file (depending on what your using).

#
# Platform specific setup
#
#############################
AC_CANONICAL_HOST
# Check for which host we are on and setup a few things
# specifically based on the host
case $host_os in
  darwin* )
        # Do something specific for mac
        ;;
  linux*)
        # Do something specific for linux
        ;;
   *BSD*)
        # Do something specific for BSD
        ;;
    *)
        #Default Case
        AC_MSG_ERROR([Your platform is not currently supported])
        ;;
esac

Setting CFlags/CXXFlags/LDFlags/LIBS

In configure.ac setting ‘CFLAGS and similar variables can be done quite easily. You simply set the variable. However, make sure if your aiming to support multiple platforms you do it in a way that is supported. For instance/bin/sh – the true borne shell doesn’t understands the += variable syntax. Hence something like:

 CFLAGS+=" something"

will fail. You might say that everyone uses bash but things like MacOSX doesn’t. Instead use the syntax:

 CFLAGS="$CFLAGS something"

It’ll work on any platform.

Passing A Variable from configure.ac to Makefile.am

One of the first things that I found myself needing to do was pass the content of a variable from configure.ac toMakefile.am. Whilst this is burried in the autoconf documentation, here’s the 2 second version.

In configure.ac do something like:

 GL_LIBS="-lGLU -lGL"
 AC_SUBST(GL_LIBS)

Then in Makefile.am put:

myapplication_bin_LDADD=@GL_LIBS@
The AC_SUBST tells autoconfig that a substitution should happen when building the Makefile (s). HenceGL_LIBS is expanded correctly. Of course, you probably want to do a much better check for your libs 🙂

Defining a #defining for use in C/C++ code

In configure.ac

 AC_DEFINE([MYDEFINE],[SOMEVALUE],[Description: Make #define MYDEFINE SOMEVALUE])

in config.h.in

 #undef MYDEFINE

Checking for C++ headers/C++ Libraries, not pkg-config supported

AC_LANG_PUSH([C++])
AC_CHECK_HEADERS([boost/foreach.hpp])
AC_CHECK_LIBS([boost])
AC_LANG_POP([C++])

Checking the size of a type

AC_CHECK_SIZEOF(uid_t)
if test $ac_cv_sizeof_uid_t = "4"; then
...
fi

Checking the endianess of a platform



AC_CHECK_HEADERS([endian.h],
 AC_TRY_COMPILE([#include <endian.h>], [switch (1) { case __LITTLE_ENDIAN: break; case __BYTE_ORDER: break; } ],[is_little_endian=0], [is_little_endian=1]),
 AC_MSG_ERR([Can't Determine Endianess])
)
if test $is_little_endian = 1 ; then
...
fi

A Simple configure.ac

For those that just want a quick start, below is a simple configure.ac file

#
# A good reference for macros for this config file is at:
# http://sourceware.org/autobook/autobook/autobook_283.html

AC_PREREQ(2.50)

AC_INIT(PROJECTNAMEHERE, VERSIONHERE, EMAILADDRESSHERE)

# Define where autogenerated build files such as 'missing'
# should be located. This is also the location of any additional
# m4 macros that are required
AC_CONFIG_AUX_DIR(cfg)

#
# Init Automake subsystem
#
AM_INIT_AUTOMAKE(PROJECTNAMEHERE,VERSIONHERE)
AM_CONFIG_HEADER(config.h)

# Indicate this is a release build and that
# dependancies for changes between Makefile.am and Makefile.in
# should not be checked. This makes compiling faster. If you are working
# on the library, run: ./configure --enable-maintainer-mode to enable the dependancies
dnl AM_MAINTAINER_MODE

# Need the C++ Compiler, Installer and ranlib to strip libraries
#
AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T

AC_CONFIG_FILES([Makefile])

AC_OUTPUT

A Simple Makefile.am

Here’s a simple Makefile.am that can be used in the root directory of a project

# Indicate this is not a GNU project and that
# missing files should be automatically created
AUTOMAKE_OPTIONS=foreign

# Instruct aclocal to check the cfg directory for
# extra macros
ACLOCAL_AMFLAGS=-I cfg

INCLUDES= -I@top_srcdir@

# Global C Flags
#AM_CFLAGS = 

#EXTRA_DIST=libwcl.pc

# Programs to build
bin_PROGRAMS = evaluateTablet

# Evaluate Table Application
evaluateTablet_includedir=@top_srcdir@
evaluateTablet_LDADD=-l
evaluateTablet_SOURCES=.....

References

  1. http://www.gnu.org/software/automake/manual/html_node/Invoking-aclocal.html
  2. http://www.gnu.org/software/autoconf/
  3. http://www.gnu.org/software/automake/
  4. http://www.gnu.org/software/autoconf/manual/autoconf-2.57/html_node/autoconf_29.html
  5. http://www.gnu.org/software/libtool/


Donations keep this site alive

Add A Comment

*