2
votes

I have this makefile:

.PHONY: all
all: foo.o

makefile: ;

%.inc:
    echo INC
    touch $@

%.o: bar.inc
    echo O
    touch $@

%.o:
    FAIL

I expect it to use the first %.o rule, create bar.inc as a prerequisite, and then create foo.o. Instead it uses the last rule and I get this:

FAIL
make: FAIL: Command not found
make: *** [foo.o] Error 127

It works fine if I either change %.inc to bar.inc or change %.o to foo.o. The documentation says

In order for the pattern rule to apply, its target pattern must match the file name under consideration and all of its prerequisites (after pattern substitution) must name files that exist or can be made.

Does the pattern rule %.inc not count as "can be made"? I am using GNU Make 3.81.

1

1 Answers

3
votes

The relevant detail you are missing is in section 10.5.4 How Patterns Match.

Specifically (emphasis mine):

A pattern rule can be used to build a given file only if there is a target pattern that matches the file name, and all prerequisites in that rule either exist or can be built. The rules you write take precedence over those that are built in. 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.

So the issue isn't that your prerequisite cannot be made but rather that make isn't even trying since your other rule "wins".

MadScientist covers this with a bit of extra information and detail in his answer here.