Search code examples
makefilegnu-make

Chaining pattern rules in Makefile


I have a Makefile that, if simplified, boils down to this:

.PHONY: FORCE
FORCE:

a-%-b: FORCE
        @echo 'a-$*-b'

c-%-d: a-%-b

If I run make a-test-b, it prints a-test-b, as expected. However, if I try make c-test-d, it fails with

make: *** No rule to make target `c-test-d'.  Stop.

Is there any way I can make this work? This seems confusing to me, since something like this works:

%.txt1:
        touch $*.txt1

%.txt2: %.txt1
        cp $< $@

Here, I also have a pattern rule that has the target of another pattern rule as a prerequisite; however, contrary to the example above, it works perfectly well if I do make test.txt2 - it happily creates test.txt1 first, then copies it as test.txt2.


Solution

  • That's because this:

    c-%-d: a-%-b
    

    does not define a pattern rule. It cancels a pattern rule (which doesn't exist anyway). If you want to create a pattern rule you have to provide a recipe that the rule will run, even just this is sufficient:

    c-%-d: a-%-b ;
    

    (adding the ;).