A GNU Make Tutorial

An Example

Version 2 -- Variables

Example 1 shows only one of many (approximately 40) tests. A simple-minded makefile would have 40 pairs of similar rules, all differing by the names of the files involved but having the same basic structure. One of the problems with such a makefile is that the location of the programs to run or the command-line options may change (for instance, when testing on different hardware platforms). Changing 40 instances of the commands is tedious and error-prone. Variables can ease this burden considerably.

A variable is defined with the syntax

var_name = definition
and is expanded with with $(var_name).

Useful variables for the makefile defined above would include:

dlxasm = ../dlxasm/$(ARCH)/dlxasm
dlxcpu = ../dlxcpu/$(ARCH)/dlxcpu
dlxcpuopt = -b -d -p -a -m 8192
Notice that the definition of dlxasm and dlxcpu include the expansion of another variable, $(ARCH). This variable an environment variable -- set in the Unix environment, outside of the makefile. It has been arranged to contain either DS3100 or SPARC, depending on the kind of machine the user is running. The test suite then automatically chooses the correct versions of the executable to use for the test.

A makefile using these variables follows.

# Assemble the program.
t1.Addi.o:	t1.Addi.dlx dlxasm
	$(dlxasm) -o t1.Addi.o < t1.Addi.dlx
# Run the test
t1.Addi:	t1.Addi.o t1.Addi.out dlxcpu
	$(dlxcpu) $(dlxcpuopt) t1.Addi.o > t1.Addi.tmp
	diff t1.Addi.tmp t1.Addi.out
	rm t1.Addi.tmp 

[Previous] [Next] [Tutorial Index] [GNU Make]

Copyright 1996 by Byron Weber Becker