Programming Utilities Guide

Writing a Simple Makefile

The basic format for a makefile target entry is:

Table 4-1 Makefile Target Entry Format
target .  .  .  : [ dependency .  .  .  ] 
            [ command ]
             . . .

In the first line, the list of target names is terminated by a colon. This, in turn, is followed by the dependency list, if there is one. If several targets are listed, this indicates that each such target is to be built independently using the rule supplied.

Subsequent lines that start with a TAB are taken as the command lines that comprise the target rule. A common error is to use SPACE characters instead of the leading TAB.

Lines that start with a # are treated as comments up until the next (unescaped) NEWLINE and do not terminate the target entry. The target entry is terminated by the next non-empty line that begins with a character other than TAB or #, or by the end of the file.

A trivial makefile might consist of just one target shown in the following figure:

Table 4-2 A Trivial Makefile
test: 
     ls test 
     touch test

When you run make with no arguments, it searches first for a file named makefile, or, if there is no file by that name, Makefile. If either of these files is under SCCS control, make checks the makefile against its history file. If it is out of date, make extracts the latest version.

If make finds a makefile, it begins the dependency check with the first target entry in that file. Otherwise, you must list the targets to build as arguments on the command line. make displays each command it runs while building its targets.

$ make 
ls test
test not found
touch test
ls test
test

Because the file test was not present (and therefore out of date), make performed the rule in its target entry. If you run make a second time, it issues a message indicating that the target is now up to date and skips the rule:

$ make 
`test' is up to date.

Note -

make invokes a Bourne shell to process a command line if that line contains any shell metacharacters, such as a semicolon (;) redirection symbols (<, >, >>, |), substitution symbols (*, ?, [], $, =), or quotes, escapes or comments (", ', `, \, #, etc.:), If a shell is not required to parse the command line, make exec()'s the command directly.


Line breaks within a rule are significant in that each command line is performed by a separate process or shell.

This means that a rule such as:

test: 
       cd /tmp 
       pwd

behaves differently than you might expect, as shown below.

$ make test
cd /tmp 
pwd
/usr/tutorial/waite/arcana/minor/pentangles 

You can use semicolons to specify a sequence of commands to perform in a single shell invocation:

test: 
cd /tmp ; pwd 

Or, you can continue the input line onto the next line in the makefile by escaping the NEWLINE with a backslash (\). The escaped NEWLINE is treated as white space by make.

The backslash must be the last character on the line. The semicolon is required by the shell.

test: 
       cd /tmp ; \ 
       pwd