makefile GNU Pattern Rules Targets matching multiple Pattern Rules


Example

If a target matches multiple pattern rules, make will use the one whose prerequisites exist or can be built. For example:

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@
%.o: %.s
    $(AS) $(ASFLAGS) $< -o $@

Will compile foo.c to foo.o or assemble foo.s to foo.o, depending on which one of foo.c or foo.s exists.

If multiple rules have prerequisites that exist or can be built, make will use the rule that matches to the shortest stem. For example:

f%r:
    @echo Stem is: $*
fo%r:
    @echo Stem is: $*

Will use the second rule to make the target foo.bar, echoing Stem is: o.ba.

If multiple rules match to the shortest stem, make will use the first one in the Makefile.