help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dealing with multiple pattern targets in a single rule


From: Pete Johnson
Subject: Re: Dealing with multiple pattern targets in a single rule
Date: Mon, 14 Nov 2005 10:32:48 -0800
User-agent: Mozilla Thunderbird 1.0.6 (Windows/20050716)

Josef,

You need a separate rule for each type of target. However, there is no reason to duplicate the code for the action. Just put it in a variable.

For your example, change

%.ao %.bo %.co %.do : %.c
   gcc -DTYPE=${TYPE} -c -o $@ $<

to this...

compile_action = gcc -DTYPE=${TYPE} -c -o $@ $<

%.ao: %.c
   $(compile_action)

%.bo: %.c
   $(compile_action)

%.co: %.c
   $(compile_action)

%.do: %.c
   $(compile_action)

-Pete


Josef Drexler wrote:


I've looked through the make docs, but haven't found an answer to this: is there a way to tell make that "this rule works for these kinds of targets, but the commands must be run separately for each"?

For instance, consider the last rule in the following Makefile:

---------
all:    out-a out-b out-c out-d

SRC=$(wildcard src/*.c)

out-a:    $(SRC:%.c=%.ao)
out-b:    $(SRC:%.c=%.bo)
out-c:    $(SRC:%.c=%.co)
out-d:    $(SRC:%.c=%.do)

%.ao:    TYPE=A
%.bo:    TYPE=B
%.co:    TYPE=C
%.do:    TYPE=D

out-%:
    gcc -o $@ $^

%.ao %.bo %.co %.do : %.c
    gcc -DTYPE=${TYPE} -c -o $@ $<
---------

This works as long as I make each final target (out-%) separately, but if I run "make all", it thinks that the last rule makes all four target types simultaneously, which is not the case. So I have to run "make" four times to make all four targets.

I understand that the "multiple targets in a rule" example in the docs (bigoutput/littleoutput) only works for explicit rules and that I should use static pattern rules otherwise, but I just can't see how I would write such a rule in my case. I need to have four different types of object files made using the same commands with slightly different options.

While I could just copy the last rule for times for the four different types, in my actual Makefile, the commands are much more complex and the file would become unwieldy and harder to maintain if I had to make sure to make all changes to all versions of the same commands.

So is there another way of doing this more efficiently that I just can't see?





reply via email to

[Prev in Thread] Current Thread [Next in Thread]