2
votes

Let the file "prefix-hi.c" being present in the current directory ("> touch prefix-hi.c"). Then, create the following Makefile:

prefix-%.o: prefix-%.c prefix-%-generated.c
    @echo Specific Rule 

%.o: %.c
    @echo General Rule

prefix-%-generated.c:
    touch prefix-$*-generated.c

Making in two steps gives the sequence

> make prefix-hi-generated.c
touch prefix-hi-generated.c
> make prefix-hi.o
Specific Rule

Deleting the generated file and trying to build in one step results in

> rm -f prefix-hi-generated.c
> make prefix-hi.o
General Rule

That is, GNU Make does not recognize the opportunity to build "prefix-hi-generated.c" from the last rule. Adding an explicit rule

prefix-hi-generated.c:
    touch prefix-hi-generated.c

changes everything. Now the one-step sequence results in

> rm -f prefix-hi-generated.c
> make prefix-hi.o
touch prefix-hi-generated.c
Specific Rule

From my angle, this behavior seems quirky.

  • Is there a rational explanation for things being the way they are?
  • How could one force GNU Make to apply the 'Specific Rule' for files starting with "prefix-" without using explicit rules?
1

1 Answers

2
votes

This is explained fully in the GNU make manual

Specifically:

Note however, that a rule whose prerequisites actually exist or are mentioned always takes priority over a rule with prerequisites that must be made by chaining other implicit rules.

This is an explicit exception to the general rule that the most narrowly matching (shortest stemmed) rule will always be caused to match. The reason your last example works how you like, is because it isn't a pattern matching based rule at all so is checked before pattern matching is resolved.