当多个模式规则与目标匹配时

Joe*_*yle 16 makefile gnu-make

GNU使手动

有可能不止一个模式规则符合这些标准.在这种情况下,make将选择具有最短词干的规则(即,最具体匹配的模式).

所以它让我感到惊讶:

$ touch make_specific.cpp

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

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

$ make -B -f Makefile.general_first make_specific.o
using general rule
g++44 -c make_specific.cpp -o make_specific.o
Run Code Online (Sandbox Code Playgroud)

多模式规则匹配目标,并因为对干%_specific.o : %_specific.cpp规则(在这种情况下,"制作")比杆的短%.o : %.cpp规则,我预期的要选择的特定规则,但事实并非如此.

我错过了什么?

Rei*_*eek 17

您可能正在使用低于的make版本3.82.

在版本3.81和更低版本中,选择标准不同; make会选择匹配模式的第一条规则.您指的文档是版本3.82.该版本确实选择具有最特定词干的规则,这符合您的期望.

从文件NEWSmake源代码树:

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.
Run Code Online (Sandbox Code Playgroud)