makefile GNU Pattern Rules Targets matching multiple Pattern Rules


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:

    @echo Stem is: $*
    @echo Stem is: $*

Will use the second rule to make the target, echoing Stem is:

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