make: Using Implicit
1
1 10.1 Using Implicit Rules
1 =========================
1
1 To allow 'make' to find a customary method for updating a target file,
1 all you have to do is refrain from specifying recipes yourself. Either
1 write a rule with no recipe, or don't write a rule at all. Then 'make'
1 will figure out which implicit rule to use based on which kind of source
1 file exists or can be made.
1
1 For example, suppose the makefile looks like this:
1
1 foo : foo.o bar.o
1 cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
1
1 Because you mention 'foo.o' but do not give a rule for it, 'make' will
1 automatically look for an implicit rule that tells how to update it.
1 This happens whether or not the file 'foo.o' currently exists.
1
1 If an implicit rule is found, it can supply both a recipe and one or
1 more prerequisites (the source files). You would want to write a rule
1 for 'foo.o' with no recipe if you need to specify additional
1 prerequisites, such as header files, that the implicit rule cannot
1 supply.
1
1 Each implicit rule has a target pattern and prerequisite patterns.
1 There may be many implicit rules with the same target pattern. For
1 example, numerous rules make '.o' files: one, from a '.c' file with the
1 C compiler; another, from a '.p' file with the Pascal compiler; and so
1 on. The rule that actually applies is the one whose prerequisites exist
1 or can be made. So, if you have a file 'foo.c', 'make' will run the C
1 compiler; otherwise, if you have a file 'foo.p', 'make' will run the
1 Pascal compiler; and so on.
1
1 Of course, when you write the makefile, you know which implicit rule
1 you want 'make' to use, and you know it will choose that one because you
11 know which possible prerequisite files are supposed to exist. ⇒
Catalogue of Built-In Rules Catalogue of Rules, for a catalogue of all
1 the predefined implicit rules.
1
1 Above, we said an implicit rule applies if the required prerequisites
1 "exist or can be made". A file "can be made" if it is mentioned
1 explicitly in the makefile as a target or a prerequisite, or if an
1 implicit rule can be recursively found for how to make it. When an
1 implicit prerequisite is the result of another implicit rule, we say
11 that "chaining" is occurring. ⇒Chains of Implicit Rules Chained
Rules.
1
1 In general, 'make' searches for an implicit rule for each target, and
1 for each double-colon rule, that has no recipe. A file that is
1 mentioned only as a prerequisite is considered a target whose rule
11 specifies nothing, so implicit rule search happens for it. ⇒
Implicit Rule Search Algorithm Implicit Rule Search, for the details of
1 how the search is done.
1
1 Note that explicit prerequisites do not influence implicit rule
1 search. For example, consider this explicit rule:
1
1 foo.o: foo.p
1
1 The prerequisite on 'foo.p' does not necessarily mean that 'make' will
1 remake 'foo.o' according to the implicit rule to make an object file, a
1 '.o' file, from a Pascal source file, a '.p' file. For example, if
1 'foo.c' also exists, the implicit rule to make an object file from a C
1 source file is used instead, because it appears before the Pascal rule
11 in the list of predefined implicit rules (⇒Catalogue of Built-In
Rules Catalogue of Rules.).
1
1 If you do not want an implicit rule to be used for a target that has
1 no recipe, you can give that target an empty recipe by writing a
1 semicolon (⇒Defining Empty Recipes Empty Recipes.).
1