2011-08-16 32 views
24

可能重複:
GNU Makefile rule generating a few targets from a single source fileGNU使:多個目標的單一規則

如果我有一個Makefile規則是這樣的:

a b c: 
    echo "Creating a b c" 
    touch a b c 

output: a b c 
    cat a b c > output 

,我跑make -j9輸出

make看到3個依賴項(a,b,c),尋找如何生成它們:(上面的「a b c」規則),但接下來會發生什麼?是否應該認識到,只需要運行一次「a b c」規則即可創建全部3個目標?

這是什麼使實際做:

[[email protected] test]$ make -j9 output -n 
echo "Creating a b c" 
touch a b c 
echo "Creating a b c" 
touch a b c 
echo "Creating a b c" 
touch a b c                                                                  
cat a b c > output                                                                
[[email protected] test]$ 

相同的配方運行3次,一旦每個依存性的統治「輸出」!

有誰知道它爲什麼這樣表現?

+0

相關:http:// stackoverflow。com/questions/3016258/generate-multiple-target-using-single-action-rule and http://stackoverflow.com/questions/2973445/gnu-makefile-rule-generating-a-few-targets-from-a-單源文件 – krlmlr

回答

27

a b c:規則告訴請,這是如何建立這些目標的任何,而不是如何建立所有的人都。 Make不夠聰明,無法分析這些命令,並推斷一旦運行該規則將構建全部三個。知道(從output規則),它必須重建a,bc,所以這就是它的作用。它運行的第一條規則爲a,一條爲b,一條爲c。如果你想一次重建他們,做這樣的事情

a b c: 
    echo "Creating [email protected]" 
    touch [email protected] 

​​

或者更好的是:

如果要單獨重建它們,這樣做

THINGS = a b c 
.PHONY: things 
things: 
    echo "Creating $(THINGS)" 
    touch $(THINGS) 

output: things 
    cat $(THINGS) > output 
+2

很好的答案!謝謝。是創造一個假目標的唯一方式,還是有一個更清晰的方式來表達「a **和** b **和** c是由這個規則建立的」? – Pavel

+0

@Pavel,我不知道一個更乾淨的方式(我可能會採取第一個選項)。但是,我仍然試圖找出AUZKamath的答案。 – Beta

+12

請參閱http://www.cmcrossroads.com/ask-mr-make/12908-rules-with-multiple-outputs-in-gnu-make瞭解處理這種情況的方法,包括唯一的「正確的」在gmake中指定具有多個輸出的規則的方法(當然是模式規則!)。 –

6

abc是三個不同的目標/目標,沒有先決條件。我要說,無論什麼時候要求它都會建立目標。

a b c: 
    echo "Creating a b c" 
    touch a b c 

您正在詢問make構建具有b c作爲先決條件的目標命名輸出。 因此,目標一個B C順序建立,最終輸出建立。

現在在你的情況下,所有的目標都會在任何一個被調用時總是被構建。所以爲了避免多餘的構建,你必須爲目標a,b,c添加先決條件。僅當'a'不存在時才構建目標'a'。類似地,'b'和'c'

a b c: [email protected] 
    echo "Creating a b c" 
    touch a b c 

然而,這不可取。理想情況下,Makefile目標應該非常具體。

+2

這工作,但我看不出如何。傑克凱利送你了嗎? – Beta

+1

但是,使用'make -j3 a b c'運行時,會創建三個作業。 – krlmlr

+2

任何人都可以解釋這是如何工作的?我不能通過閱讀https://www.gnu.org/software/make/manual/make.html#index-_0024_0040 – Xiphias