Chapter 2: An Introduction to Makefiles 5
A recipe may follow each line that contains a target and prerequisites. These recipes say
how to update the target file. A tab character (or whatever character is specified by the
.RECIPEPREFIX variable; see Section 6.14 [Special Variables], page 73) must come at the
beginning of every line in the recipe to distinguish recipes from other lines in the makefile.
(Bear in mind that make does not know anything about how the recipes work. It is up to
you to supply recipes that will update the target file properly. All make does is execute the
recipe you have specified when the target file needs to be updated.)
The target ‘clean’ is not a file, but merely the name of an action. Since you normally
do not want to carry out the actions in this rule, ‘clean’ is not a prerequisite of any other
rule. Consequently, make never does anything with it unless you tell it specifically. Note
that this rule not only is not a prerequisite, it also does not have any prerequisites, so the
only purpose of the rule is to run the specified recipe. Targets that do not refer to files
but are just actions are called phony targets. See Section 4.5 [Phony Targets], page 29, for
information about this kind of target. See Section 5.5 [Errors in Recipes], page 49, to see
how to cause make to ignore errors from rm or any other command.
2.3 How make Processes a Makefile
By default, make starts with the first target (not targets whose names start with ‘.’). This
is called the default goal. (Goals are the targets that make strives ultimately to update. You
can override this behavior using the command line (see Section 9.2 [Arguments to Specify
the Goals], page 99) or with the .DEFAULT_GOAL special variable (see Section 6.14 [Other
Special Variables], page 73).
In the simple example of the previous section, the default goal is to update the executable
program edit; therefore, we put that rule first.
Thus, when you give the command:
make
make reads the makefile in the current directory and begins by processing the first rule. In
the example, this rule is for relinking edit; but before make can fully process this rule, it
must process the rules for the files that edit depends on, which in this case are the object
files. Each of these files is processed according to its own rule. These rules say to update
each ‘.o’ file by compiling its source file. The recompilation must be done if the source file,
or any of the header files named as prerequisites, is more recent than the object file, or if
the object file does not exist.
The other rules are processed because their targets appear as prerequisites of the goal.
If some other rule is not depended on by the goal (or anything it depends on, etc.), that
rule is not processed, unless you tell make to do so (with a command such as make clean).
Before recompiling an object file, make considers updating its prerequisites, the source
file and header files. This makefile does not specify anything to be done for them—the ‘.c’
and ‘.h’ files are not the targets of any rules—so make does nothing for these files. But
make would update automatically generated C programs, such as those made by Bison or
Yacc, by their own rules at this time.
After recompiling whichever object files need it, make decides whether to relink edit.
This must be done if the file edit does not exist, or if any of the object files are newer than
it. If an object file was just recompiled, it is now newer than edit, so edit is relinked.
Kommentare zu diesen Handbüchern