Saturday, March 08, 2014

Tutorial Simple makefile and automake

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

Introduction

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

$ vi hello.c
#include
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.

Makefile

This is how a simple Makefile looks like:

$ vi Makefile
# This is a simple makefile

Program = hello

all: $(Program)

hello:
    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.

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

Automake

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:
1. configure.ac
2. Makefile.am

To generate configure.ac
autoscan -> configure.scan -> configure.ac

$ autoscan; mv -i configure.scan configure.ac
$ vi configure.ac
AC_INIT([helloworld], [1.0.0])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Note:
If you are using configure.in, it is old. The new version use configure.ac.
If system complain no config.h.in. Just create 1. $ touch config.h.in

$ vi Makefile.am
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

Makefile.am: error: required file './AUTHORS' not found
Makefile.am: 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:

Post a Comment