RNAer
RNAer

Reputation: 31

how to get a particular dependency from the dependency list for a target

Suppose I have a following rule in the Makefile:

%.foo: %.bar %.spam %.bot
<tab> echo "hello1" > $<

how can I also echo "hello2" into the second dependency (but not the .bot file), i.e. the .spam file? Thanks

Upvotes: 3

Views: 844

Answers (2)

Eric Melski
Eric Melski

Reputation: 16790

What you're asking is antithetical to the normal operation of make: a rule should modify the files named on the left of the colon, not the files on the right of the colon. You haven't given much context here, so it's hard to give you more specific advise than that.

As far as the particular question you've asked, you could use something like this:

%.foo: %.bar %.spam %.bot
    echo "hello1" > $<
    echo "hello2" > $*.spam

This uses the $* automatic variable, which is defined as the part of the filename that matched the % character in a pattern rule.

Upvotes: 1

Beta
Beta

Reputation: 99094

%.foo: %.bar %.spam %.bot
    echo "hello1" > $<    
    echo hello2 > $(word 2,$^)

(Note that > overwrites, at least in the shells I know, which makes the whole exercise pretty pointless. To append, use >>.)

Upvotes: 6

Related Questions