自学内容网 自学内容网

4WritingRules

The order of rules is not significant, except for determining the default goal:the target for make to consider,
if you do not otherwise specify one.
The default goal is the first target of the first rule in the first makefile.

Rule Syntax

Because dollar signs are used to start make variable references, if you really want a dollar sign in a target or
prerequisite you must write two of them, ‘$$(see How to Use Variables).

A rule tells make two things: when the targets are out of date, and how to update them when necessary.

The criterion for being out of date is specified in terms of the prerequisites, which consist of file names separated
by spaces.
A target is out of date if it does not exist or if it is older than any of the prerequisites (by comparison of
last-modification times).

How to update is specified by a recipe.
This is one or more lines to be executed by the shell.

Types of Prerequisites

A normal prerequisite makes two statements:
First, it imposes an order in which recipes will be invoked:
the recipes for all prerequisites of a target will be completed before the recipe for the target is started.
Second, it imposes a dependency relationship:
if any prerequisite is newer than the target, then the target is considered out-of-date and must be rebuilt.

Using Wildcard Characters in File Names

A single file name can specify many files using wildcard characters.
The wildcard characters in make are ‘*’, ‘?’ and ‘[]’, the same as in the Bourne shell.
For example, *.c specifies a list of all the files (in the working directory) whose names end in ‘.c’.

Wildcard expansion is performed by make automatically in targets and in prerequisites.
In recipes, the shell is responsible for wildcard expansion.
In other contexts, wildcard expansion happens only if you request it explicitly with the wildcard function.

Phony Targets

A phony target is one that is not really the name of a file; rather it is just a name for a recipe to be executed
when you make an explicit request.
There are two reasons to use a phony target:
to avoid a conflict with a file of the same name, and to improve performance.

Rules without Recipes or Prerequisites

If a rule has no prerequisites or recipe, and the target of the rule is a nonexistent file, then make imagines this
target to have been updated whenever its rule is run.
This implies that all targets depending on this one will always have their recipe run.
clean: FORCE
        rm $(objects)
FORCE:
#当前目录下没有FORCE文件,当执行<make clean>时,不管当前目录有没有clean文件,这两个rule都会执行.

Special Built-in Target Names

Certain names have special meanings if they appear as targets.

.PHONY
The prerequisites of the special target .PHONY are considered to be phony targets.
When it is time to consider such a target, make will run its recipe unconditionally, regardless of whether a file
with that name exists or what its last-modification time is.
.PHONY:aaa
aaa:bbb
echo 456
#没有PHONY
#当aaa文件存在:bbb文件更新时执行;aaa文件更新时不执行
#有PHONY
#当aaa文件存在:即使aaa文件更新,make命令也会执行该rule
#ccc文件比bbb文件新,bbb文件比aaa文件新.
#此时<make>,先执行第二条rule,再执行第一条rule.
aaa:bbb
echo QQQ
bbb:ccc
echo WWW

https://stackoverflow.com/questions/51225819/why-make-all-works-as-expected-without-adding-all-to-phony-target


原文地址:https://blog.csdn.net/qq_53318060/article/details/137526395

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!