Setting Up Autotools

Posted on 11 May 2015 by Eric Oestrich

For a while I have wanted to start learning C, but I have always been daunted by compiling a project that is larger than 1 source file. I started reading 21st Century C and it finally got me past the hurdle of autotools for compiling C projects. These are the scripts I ended up with to start a simple project.

Folder structure for an empty C project with autotools

├── ChangeLog
├── NEWS
└── src
    ├── main.c

AUTHORS, ChangeLog, COPYING, INSTALL, NEWS, and README are required by autotools before it will generate the configure script. Right now I have them as empty files to move along.
# Run this to generate all the initial makefiles, etc.
set -e

autoreconf -i -f

I run this script after checking out the project on a new machine. Autotools will spit out all of the required files to let it compile. Your project folder will have a lot of new files that can be ignored.
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT([prog], [1], [])

# Checks for programs.

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.


This file generates the configure script. It expands from macros into the bash script. The best way I found to expand this file is looking at other projects and see what they use. For instance, checking to see if glib-2.0 is available and can be compiled against:

AC_CHECK_LIB([glib-2.0], [g_free])

This sets up automake to look in the src subfolder. Most of the work will happen in there.

bin_PROGRAMS = prog
prog_CFLAGS = # `pkg-config --cflags glib-2.0`
prog_LDFLAGS = # `pkg-config --libs glib-2.0`
prog_SOURCES = main.c # list out all source files

bin_PROGRAMS tells automake what binary files will be generated. prog_CFLAGS and prog_LDFLAGS sets up the CFLAGS and LDFLAGS appropriately. This is where you will add -I and -l flags. prog_SOURCES are what sources gets compiled into prog. The last three lines are named after the binary file that is generated.

All together

Once these files are in place you will be able to clone the repo, and within a few simple commands be compiling your project. Below is the full set of commands it takes.

$ git clone $GIT_REPO git_repo
$ cd git_repo
$ ./
$ ./configure
$ make
$ src/prog

With this set up I have been able to get past the big hurdle of compiling multiple C source files, and start learning C itself. I hope this helps others get over the hurdle as well.

comments powered by Disqus
Eric Oestrich
I am:
All posts
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.