Multiple Targets in a Rule
A rule with multiple targets is equivalent to writing many rules, each with one target, and all identical aside from that. The same
commands apply to all the targets, but their effects may vary because you can substitute the actual target name into the command using
`$@'. The rule contributes the same dependencies to all the targets also.
This is useful in two cases.
You want just dependencies, no commands. For example:
kbd.o command.o files.o: command.h
gives an additional dependency to each of the three object files mentioned.
Similar commands work for all the targets. The commands do not need to be absolutely identical, since the automatic variable `$@'
can be used to substitute the particular target to be remade into the commands. For example:
bigoutput littleoutput : text.g
generate text.g -$(subst output,,$@) > $@
is equivalent to
bigoutput : text.g
generate text.g -big > bigoutput
littleoutput : text.g
generate text.g -little > littleoutput
Here we assume the hypothetical program generate makes two types of output, one if given `-big' and one if given `-little'.
Suppose you would like to vary the dependencies according to the target, much as the variable `$@' allows you to vary the commands.
You cannot do this with multiple targets in an ordinary rule, but you can do it with a static pattern rule.
July 23th Thursday (七月 二十三日 木曜日)
最新推荐文章于 2025-08-21 09:06:15 发布
本文介绍Makefile中使用多目标规则的方法,包括如何为多个目标指定相同的依赖项和命令,以及利用特殊变量$@实现根据不同目标调整命令的能力。此外,还讨论了静态模式规则在处理目标特异性依赖时的应用。
1335

被折叠的 条评论
为什么被折叠?



