Makefile:模式规则作为第一条规则
Makefile: Pattern rule as first rule
我知道 make
如果在没有任何参数的情况下调用,通常会执行第一个目标。但是如果第一个目标是模式规则会怎样呢?我这里有一个 Makefile,如下所示:
%.o: %.cc
gcc -c -o $@ $<
main: main.o helper.o
gcc main.o helper.o -o $@
根据我对 make
的理解,只要调用它 w/o 在这种情况下,任何参数都可能会导致某种错误,因为据我所知,第一个目标始终是默认目标,如果 make
没有给出任何参数则没有意义。但是,当我使用此 Makefile 调用 make
时,它会构建 main
目标(当然,还会递归地构建目标 main.o
和 helper.o
)。
那么,总是真的是make
在寻找第一个目标时会忽略模式规则吗?将那些真正想成为默认目标的目标放在目标前面是否会被认为是不好的风格?在我看来,这有点令人困惑。
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 target of the first rule in the
first makefile. If the first rule has multiple targets, only the first
target is taken as the default. There are two exceptions: a target
starting with a period is not a default unless it contains one or more
slashes, ‘/’, as well; and, a target that defines a pattern rule has
no effect on the default goal. (See Defining and Redefining Pattern
Rules.)
我知道 make
如果在没有任何参数的情况下调用,通常会执行第一个目标。但是如果第一个目标是模式规则会怎样呢?我这里有一个 Makefile,如下所示:
%.o: %.cc
gcc -c -o $@ $<
main: main.o helper.o
gcc main.o helper.o -o $@
根据我对 make
的理解,只要调用它 w/o 在这种情况下,任何参数都可能会导致某种错误,因为据我所知,第一个目标始终是默认目标,如果 make
没有给出任何参数则没有意义。但是,当我使用此 Makefile 调用 make
时,它会构建 main
目标(当然,还会递归地构建目标 main.o
和 helper.o
)。
那么,总是真的是make
在寻找第一个目标时会忽略模式规则吗?将那些真正想成为默认目标的目标放在目标前面是否会被认为是不好的风格?在我看来,这有点令人困惑。
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 target of the first rule in the first makefile. If the first rule has multiple targets, only the first target is taken as the default. There are two exceptions: a target starting with a period is not a default unless it contains one or more slashes, ‘/’, as well; and, a target that defines a pattern rule has no effect on the default goal. (See Defining and Redefining Pattern Rules.)