110Chapter4 • Using GNU make
You can also use wildcards and variables to specify file names with the include directives.
The following include directive includes all files with last part as “.in” and files specified by the
variable
MKINCS.
include *.in $(MKINCS)
4.2The
make
Rules
Rules are the most important part of a makefile. Rules control how a project should be built.
Each rule has a common structure which is discussed in this section. A typical makefile is intro-
duced as an example to show you how rules are written in makefiles.
4.2.1Anatomy of a Rule
As mentioned earlier, a typical rule looks like the following:
Target: Dependencies
Commands
A target is the objective of the rule that depends upon a list of dependencies. Commands
are used to build the target. The target name is followed by a colon. Commands start with a TAB
character.
Multiple Commands in the Same Line
A rule can have multiple commands to build target. These commands can be listed in mul-
tiple lines or in a single line. If commands are listed in a single line, they are separated by a
semicolon. It should be noted that the difference is whether or not they are executed in the same
subshell. Commands listed in one line are executed in the same subshell. If each command is
listed in a separate line, each line starts with a TAB character in the start of the line. The follow-
ing rule, used to remove some files from the current directory, has two commands. The first is
echo
and the second is the
rm
command.
clean:
@echo "Deleting files ..."
rm -f $(OBJS) *~
These two commands can also be listed as follows:
clean:
@echo "Deleting files ..." ; rm -f $(OBJS) *~
C A U T I O N Each line containing commands starts with a TAB
character. The TAB character distinguishes command lines from
other lines. A common error is to replace the TAB character with
a multiple space character. The result is that commands on this
line are not executed.
Next Page >>
<< Previous Page
Back to the Table of Contents