2012-07-12 76 views
14

GNU make manual當多個模式規則匹配目標

很可能有不止一個形式規則,將符合這些標準。在這種情況下,make會選擇最短的規則(也就是最具體匹配的模式)。

所以它讓我感到驚訝的是:

$ touch make_specific.cpp 

$ cat Makefile.general_first 
%.o: %.cpp 
@echo using general rule 
$(CXX) -c $< -o [email protected] 

%_specific.o: %_specific.cpp 
@echo using specific rule 
$(CXX) -c $< -o [email protected] 

$ make -B -f Makefile.general_first make_specific.o 
using general rule 
g++44 -c make_specific.cpp -o make_specific.o 

多模式規則與目標匹配,而且由於乾的%_specific.o : %_specific.cpp規則(在這種情況下,「讓」)比杆爲%.o : %.cpp短規則,我預計要選擇具體規則,但事實並非如此。

我錯過了什麼?

回答

16

您可能使用的版本低於3.82

在版本3.81及更低版本中,選擇標準不同; make會選擇符合該模式的第一條規則。您提到的文檔是版本3.82。該版本確實會根據您的期望選擇具有最具體乾的規則。

make源代碼樹中的文件NEWS

Version 3.82 
... 
* WARNING: Backward-incompatibility! 
    The pattern-specific variables and pattern rules are now applied in the 
    shortest stem first order instead of the definition order (variables 
    and rules with the same stem length are still applied in the definition 
    order). This produces the usually-desired behavior where more specific 
    patterns are preferred. To detect this feature search for 'shortest-stem' 
    in the .FEATURES special variable. 
+1

感謝。我使用3.81,所以這很可能是原因。等待系統管理員安裝3.82進行驗證。 – 2012-07-16 15:10:05

+0

有沒有辦法用clearmake而不是GNU make來實現這一點?我無法找到clearmake中有關模式規則選擇的任何信息... – dragonator 2017-03-29 13:46:18

+0

謝謝。我試圖理解爲什麼會發生這種事。我的錯;我從未注意到我使用的版本號:) – Fernando 2018-01-26 16:26:53