资料来源GNU文档
Makefiles contain five kinds of things:
explicit rules, implicit rules, variable definitions, directives, and comments.
What a Rule Looks Like
target:prerequisites
<tab>recipe
A target is usually the name of a file that is generated by a program;
examples of targets are executable or object files.
A target can also be the name of an action to carry out, such as ‘clean’(see Phony Targets).
A prerequisite is a file that is used as input to create the target.
A recipe is an action that make carries out.
A recipe may have more than one command, either on the same line or each on its own line.
Please note: you need to put a tab character at the beginning of every recipe line!
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.
when type <make>
make reads the makefile in the current directory and begins by processing the first rule.
//make要先处理goal依赖的prerequisites的rule,最后才处理goal所在的rule.
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, that rule is not processed, unless you tell make to do so
(with a command such as make clean).
Rules for Cleaning the Directory
# A rule such as this should not be placed at the beginning of the makefile, because we do not want it to run by default!
# Since clean is not a prerequisite of the default goal, this rule will not run at all if we give the command make
# with no arguments.
# In order to make the rule run, we have to type <make clean>.
# 当前目录下有名为clean的文件时,指令不再执行
# In this example, the clean target will not work properly if a file named clean is ever created in this directory.
# Since it has no prerequisites, clean would always be considered up to date and its recipe would not be executed.
clean:
rm xxx
# In practice, we might want to write the rule in a somewhat more complicated manner to handle unanticipated situations.
# This prevents make from getting confused by an actual file called clean.
# 此时无论有无名为clean的文件,指令都会执行
.PHONY:clean
clean:
rm xxx
https://www.gnu.org/software/make/manual/html_node/Introduction.html
https://www.gnu.org/software/make/manual/html_node/How-Make-Works.html