Saturday, March 08, 2014

Tutorial Simple makefile and automake

This tutorial is to help beginners a quick start on Makefile and automake.


Before we started using makefile, this is how we compile program every time after we changed the program:

$ vi hello.c
int main()
printf("Hello, world!\n");
return 0;

$ cc hello.c -o hello

If you have more than 1 source file:

$ cc hello.c foo.c bar.c -o hello

If you have some compiler parameter:

$ gcc -Wall -g -O2 hello.c foo.c bar.c -o hello

You get what I mean. Even with the help of readline (with doskey like) function, compilation will be a pain. When your project is getting complicated, you need makefile to help you to do the job.


This is how a simple Makefile looks like:

$ vi Makefile
# This is a simple makefile

Program = hello

all: $(Program)

    cc hello.c -o hello

$ make [-f Makefile]

cc hello.c -o hello

Makefile is collection of rules. The rule looks like this:

target: dependencies
[tab] system command

The white space before the command is tab. If you encounter error *** missing separator. Stop. Change the white space to tab before the command.

When you type make command. The make program look for all: target, which depends on hello. If the file hello is not there or out dated, the command for target hello will be executed.

There are more features for Makefile like implicit rules and internal macros, but not cover here.


Different unix system may use a different C compile, different parameter option for the compiler. For example, gcc has a -Wall option but Sun Workshop C compiler don't have -Wall option.

The Free Software Foundation (FSF) created an automake software tools to generated Makefile on different unix system. The configure script which will detect the system setting and generate the Makefile accordingly.

How to generate configure script?

Step 1:
Crete these 2 files:

To generate
autoscan -> configure.scan ->

$ autoscan; mv -i configure.scan
$ vi
AC_INIT([helloworld], [1.0.0])

# Checks for programs.


If you are using, it is old. The new version use
If system complain no Just create 1. $ touch

$ vi
bin_PROGRAMS = hello
hello_SOURCES = hello.c

Step 2:
Before you continue further, make sure you have automake and autoconf software installed.

$ aclocal
$ autoconf
$ automake --add-missing --copy --include-deps error: required file './AUTHORS' not found error: required file './ChangeLog' not found

The software should install the missing files. If it still complains some missing files, just create it manually. You might want to have your own COPYING and INSTALL file.

$ touch AUTHORS ChangeLog
$ automake --add-missing --copy --include-deps

$ ./configure

checking for a BSD-compatible install... /usr/local/bin/ginstall -c
config.status: creating Makefile

$ make

gcc -g -O2 -o hello hello.o

Automake is much more powerful that what I have shown you here. This is just to show you a very simple way to quick start a Makefile or automaker.

No comments: