20

The GNU make manual says

It is possible that more than one pattern rule will meet these criteria. In that case, make will choose the rule with the shortest stem (that is, the pattern that matches most specifically).

So it surprised me that:

$ 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

Multiple pattern rules match the target, and since the stem for the %_specific.o : %_specific.cpp rule ('make' in this case) is shorter than the stem for the %.o : %.cpp rule, I expected the specific rule to be selected, but it's not.

What am I missing?

1 Answer 1

21

You are probably using a make version lower than 3.82.

In version 3.81 and lower, the selection criterion was different; make would choose the first rule that matched the pattern. The documentation you are referring to is for version 3.82. That version does choose the rule with the most specific stem, which is according to your expectations.

From the file NEWS in the make source tree:

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.
3
  • 1
    Thanks. I am using 3.81, so that's most likely the cause. Waiting on the sysadmins to install 3.82 to verify.
    – Joe Doyle
    Jul 16, 2012 at 15:10
  • Is there a way to achieve this with clearmake instead of GNU make? I can't find any information about the pattern rule selection in clearmake...
    – dragonator
    Mar 29, 2017 at 13:46
  • Thanks. I was scratching my head trying to understand why this was happening. My bad; I never paid attention to the version number I was using :)
    – Fernando
    Jan 26, 2018 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.