1
votes

I thought that having multiple targets in a rule was the same as defining multiple rules with the same recipe for each of the targets; however, I seem to have a problem when the target/dependency have patterns.

Here's a simplified sample makefile with two different methods for building files.

all: $(foreach n, 1 2 3, $(foreach l, a b, out.$(l).$(n).txt))

1.txt 2.txt 3.txt:
    touch $@

#Method One
out.a.%.txt out.b.%.txt: %.txt
    touch $@

#Method Two
out.a.%.txt: %.txt
   touch $@
out.b.%.txt: %.txt
   touch $@

Using method one with a "make -n" I get

touch 1.txt
touch out.a.1.txt
touch 2.txt
touch out.a.2.txt
touch 3.txt
touch out.a.3.txt

but with method two I get the desired output

touch 1.txt
touch out.a.1.txt
touch out.b.1.txt
touch 2.txt
touch out.a.2.txt
touch out.b.2.txt
touch 3.txt
touch out.a.3.txt
touch out.b.3.txt

I didn't see any exceptions to the multiple target in a rule in the on-line docs. Did i miss a part of the documentation that explains why this doesn't work? Is there an easy way to make it work? (In reality, I have many more combinations of files to build than in this simple example).

(Using GNU Make 3.81)

1

1 Answers

3
votes

Implicit (pattern) rules and explicit rules work in the opposite way from each other. For explicit rules, multiple targets define multiple separate rules, one for each target. For pattern rules, multiple targets means that a single invocation of the pattern rule recipe builds all the targets.

See the documentation on pattern rules.

Oh and BTW, no, there is no alternative to writing the pattern rule multiple times. You can use various methods to automatically generate the pattern rules, but that's about it.