If make wants to build a file like xyz.o
and there's no explicit rule for that target, it will look at the implicit (pattern and suffix) rules to try to find a rule. If there's a rule that matches the target AND make knows how to build all the prerequisites, then the rule will match and make will try to use it.
If make can't build one or more of the prerequisites then that implicit rule doesn't match and make will continue looking for a different implicit rule. This is not an error! In the particular case of object files, make has built-in rules that know how to build a .o
from C source files, C++ source files, FORTRAN source files, assembly files, etc. They all have a target pattern of %.o
but only one of them (usually) will match... the others are thrown out but it's not an error.
If, at the end of the implicit rule search no rule has matched the target, but the target exists, then there's no problem: make assumes that this file is a source file (like xyz.c
or xyz.h
) that isn't supposed to be built, and just use it.
If after searching all the rules and not finding a match, and the target file (xyz.o
) does not exist, THEN you have an error and make will report that it doesn't know how to build that target.
a.o
, and that file already exists, and you don't have any prerequisites defined in the makefile, then make won't complain because the file already exists. A pattern rule where the prerequisite doesn't exist and can't be found is not an error: it just means the pattern rule doesn't match. There a lots of pattern rules that could build a filea.o
; at most one of them will match. It's fine if none of them match, too. – MadScientist