GNU make manual 翻译( 一百一十四)
繼續翻譯
4.10 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 recipe applies to all the targets, but its effect may vary because you can substitute the actual target name into the recipe using `$@'. The rule contributes the same prerequisites to all the targets also.This is useful in two cases.* You want just prerequisites, no recipe. For example:kbd.o command.o files.o: command.hgives an additional prerequisite to each of the three object filesmentioned.* Similar recipes work for all the targets. The recipes do not needto be absolutely identical, since the automatic variable `$@' can be used to substitute the particular target to be remade into thecommands (*note Automatic Variables::). For example:bigoutput littleoutput : text.ggenerate text.g -$(subst output,,$@) > $@is equivalent tobigoutput : text.ggenerate text.g -big > bigoutputlittleoutput : text.ggenerate text.g -little > littleoutputHere we assume the hypothetical program `generate' makes two typesof output, one if given `-big' and one if given `-little'. *NoteFunctions for String Substitution and Analysis: Text Functions,for an explanation of the `subst' function. Suppose you would like to vary the prerequisites according to the target, much as the variable `$@' allows you to vary the recipe. You cannot do this with multiple targets in an ordinary rule, but you can do it with a "static pattern rule". *Note Static Pattern Rules: Static Pattern.4.10 一個規則中的多個目的
===============================
和寫多個規則,每個規則一個目的的做法比較起來,帶有多個目的規則更加便利。同樣的片段作用于所有的目的,但是其作用范圍可能會變化,因為你可以在片段中通過 $@來替換世紀的目的名。規則也會把同樣的前提條件作用于所有的目的。
這在兩種情況下有用。
*你只需要 前提條件,不需要片段。例如:
kdo.o command.o files.o: command.h
對這三個目標文件,給出了一個額外的前提條件。
*為所有目的工作的同樣的片段。片段不需要絕對唯一,因為自動變量 $@能夠替換特定的目的,來重新建立指令(*note Automatic Variables::),例如:
bigoutput littleoutput : text.g
generate text.g -$(subst output,,$@) > $@
等價于:
bigoutput : text.g
generate text.g -big > bigoutput
littleoutput : text.g
generate text.g -little > littleoutput
在此書,我們假定假想的程序 generate 生成兩個類型的輸出,一個是給出 big 選項的時候,一個是給出little選項的時候。可以參照 *Note Functions for String Substitution and Analysis: Text Functions 中對 subst 函數的解釋。
設想一下你將為目的而變換前提條件。正如你用 $@的變量來對片段進行變化那樣;你在一個普通的帶有多個目的的規則里是不能達成的,但是你可以用 靜態模式規則來做到這一點。*Note Static Pattern Rules: Static Pattern。
后文待續
總結
以上是生活随笔為你收集整理的GNU make manual 翻译( 一百一十四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java项目经验——程序员成长的关键
- 下一篇: 窗体的ControlBox属性