Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when multiple pattern rules match a target

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?

like image 616
Joe Doyle Avatar asked Jul 12 '12 15:07

Joe Doyle


People also ask

What is pattern rule in makefile?

A pattern rule looks like an ordinary rule, except that its target contains the character ' % ' (exactly one of them). The target is considered a pattern for matching file names; the ' % ' can match any nonempty substring, while other characters match only themselves.

What is $@ in makefile?

$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.

What is percentage in makefile?

Special characters in a makefile In commands, a percent symbol ( % ) is a file specifier. To represent % literally in a command, specify a double percent sign ( %% ) in place of a single one. In other situations, NMAKE interprets a single % literally, but it always interprets a double %% as a single % .

What is a makefile target?

A simple makefile consists of "rules" with the following shape: target ... : dependencies ... command ... ... A target is usually the name of a file that is generated by a program; examples of targets are executable or object files.


1 Answers

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.
like image 62
Reinier Torenbeek Avatar answered Oct 05 '22 23:10

Reinier Torenbeek