Table of Contents Previous Slide Next Slide

Software Development in the UNIX Environment
The Make Utility

Make - a tool that automatically determines which source files of a program need to be recompiled and/or linked.
Make gets its knowledge of how to build your program from a file called the makefile (or Makefile). You can also
use make to install (deinstall) programs and to issue other commands from a makefile.

A makefile consists of rules with the following format:

target: dependencies ...
     commands
     ...

A Simple Example of a Makefile

myappl: srcfile1.o srcfile2.o srcfile3.o
    cc -o myapp1 srcfile1.o srcfile2.o srcfile3.o -lm
srcfile1.o: scrfile1.c myinclude.h
    cc -c srcfile1.c 
srcfile2.o: scrfile2.c myinclude.h
    cc -c srcfile2.c 
srcfile3.o: scrfile3.c myinclude.h
    cc -c srcfile3.c 
clean:
    rm -f myapp1 srcfile1.o srcfile2.o srcfile3.o

Note: You need to put a tab character at the beginning of each command line.

It is usually easiest to start with an existing makefile and edit it for a new application

Call your file Makefile so that it appears near the top of the directory listing.


Using Macros (Variables) in Makefiles

Macros make it easy to define commands and simplifies your makefile

Here is an example macro defintion:

OBJS=srcfile1.o srcfile2.o srcfile3.o

To use this macro type $(OBJS)

Here is a new version of the makefile

OBJS=srcfile1.o srcfile2.o srcfile3.o

myappl: $(OBJS)
    cc -o myapp1 $(OBJS) -lm
srcfile1.o: scrfile1.c myinclude.h
    cc -c srcfile1.c 
srcfile2.o: scrfile2.c myinclude.h
    cc -c srcfile2.c 
srcfile3.o: scrfile3.c myinclude.h
    cc -c srcfile3.c 
clean:
    rm -f myapp1 $(OBJS)

Predefined macros (make -p)

CC - C compiler command (cc)
FC - Fortran compiler command (f77)
CFLAGS - C compiler flags
FFLAGS - Fortran compiler flags

You can add macros to the command line : make myapp1 CC=gcc


Suffixes

predefined rules used by make ($< stands for the file that triggered the rule i.e. the .c or .f file)

.SUFFIXES: .c .f
.c.o:
    $(CC) $(CFLAGS) -c $<
.f.o:
    $(FC) (FFLAGS) -c $<

Use the command make -p to see a list of all predefined suffixes and macros.


Resources for learning about make

Managing Projects with make - Andrew Oram and Steve Talbott, O'Reilly & Associates, Inc 1991

GNU Make Manual - http://www.gnu.org/manual/make/html_mono/make.html