36

Possible Duplicate:
How to get the second dependency file using Automatic Variables in a Makefile?

I am using GNU make, and I'm using automatic variables such at $<, $^ etc. I know that $< is just the first prerequisite, and $^ is all the prerequisites. Is there a way to obtain just the second prerequisite?

Community
  • 1
  • 1
pauldoo
  • 18,087
  • 20
  • 94
  • 116
  • Could you say a little more about why you need this, or what you're trying to accomplish? You might be able to pick apart `$^` element by element (i.e. treat it as an array), and then grab the second that way. – Phil Miller Sep 05 '12 at 15:24
  • @Beta, Yep - this is a duplicate. I'll vote to close it as such. – pauldoo Sep 07 '12 at 12:44

2 Answers2

91

Assuming your prerequisites are regular tokens,

echo $(word 2,$^)

I often find myself giving the first argument a special position, and accessing the remaining prerequisites with

echo $(filter-out $<,$^)

*Though sometimes with the upper solution you might find repeating elements.

pandaero
  • 27
  • 5
tripleee
  • 175,061
  • 34
  • 275
  • 318
0

The best I can come up with, which I feel is a bit of a hack, is to use echo $^ | cut -f2 -d' ', à la:

output.txt: a.txt b.txt
    cat $< | ./something `echo $^ | cut -f2 -d' '` > $@

I'd love to hear a better answer than this. :)

pauldoo
  • 18,087
  • 20
  • 94
  • 116