The command section of a rule

The commands within a rule are executed by "make" whenever "make" determines that the target must be rebuilt. The command section of a rule is a list of one or more commands that will be passed to the shell by "make".

So far, we have only looked at make rules with one command. For example:


project2: proj2.c support.c myheader.h
	gcc -o project2.c support.c

In this rule, the only command necessary to build "project2" is:

	gcc -o project2.c support.c"

However, we may need to execute more than one command to build the target from its dependencies. For example, the documentary part of my Preliminary Exam is written in LaTeX. To build the so-called "Device Independent" file (which is later converted to Postscript for printing), I run LaTeX three times and BibTex once (so that LaTeX can resolve references that it couldn't in previous passes).

The command section of a "make" rule is the set of lines immediately following the target line which start with a tab character. The first line which does not start with a tab signals "make" that there are no more commands for that rule. For my Prelim, the following rule suffices:


prelim.dvi:     prelim.tex ../Bib/prelim.bib
	latex prelim
	bibtex prelim
	latex prelim
	latex prelim

When "make" decides that "prelim.dvi" must be rebuilt, it will execute the commands "latex prelim", "bibtex prelim", "latex prelim", and "latex prelim", one at a time, in order from top to bottom.

If one of the commands fails, "make" will stop and report an error without processing the remaining commands or the rest of the rules necessay to build the ultimate target. "make" can be told to ignore an error from a command and continue with the remaining commands (and with the rest of the make process) by placing a '-' character before the first word of the command (just after the tab character):


prelim.dvi:     prelim.tex ../Bib/prelim.bib
	-latex prelim
	bibtex prelim
	latex prelim
	latex prelim

In this rule, "make" is told to continue even if the first execution of LaTeX fails. This example is not a case where this feature would normally be used though, because the later commands really do require the first command to work properly. You are much more likely to see something like this:

test:
	-mkdir ./test
	cp myproj test
	test/test < inputfile > test/outputfile

Here, "mkdir ./test" will fail if the subdirectory "test" already exists. Our actual goal in calling this command was to be sure that "test" exists, not to be sure that the command was successful. We don't care if "mkdir" failed, as long as "test" exists before we try to copy "myproj" to it. Placing the dash before "mkdir ./test" keeps us from having to remove the file "test" before executing the commands for this rule.
This page was last modified .