0

This question addresses how to get each dependency individually by number.

However, if I have 7 dependencies in a rule and I want to get the first 5, is there a canonical way to specify that I want each of the first 5?

I'm interested in this mostly for readability.

OutBin: In1.cc In2.cc In3.cc In4.cc In5.cc libA.a libB.a
    gcc <MAGIC_STRING_FOR_FIRST_FIVE_DEPENDENCIES> -o OutBin
merlin2011
  • 71,677
  • 44
  • 195
  • 329

1 Answers1

2

What about:

OutBin: In1.cc In2.cc In3.cc In4.cc In5.cc libA.a libB.a
    gcc $(wordlist 1,5,$^) -o OutBin

But if what you really want is the source files, the following is probably better because it is more generic and does not depend on the number or the order of dependencies:

OutBin: In1.cc In2.cc In3.cc In4.cc In5.cc libA.a libB.a
    gcc $(filter %.cc,$^) -o OutBin

Notes:

  1. Specifying libraries (libA.a libB.a) in the prerequisites but not using them in the recipe is useless. Isn't it simply gcc $^ -o OutBin, instead?
  2. You should consider using automatic variables in your recipes:

    OutBin: In1.cc In2.cc In3.cc In4.cc In5.cc libA.a libB.a
        gcc $^ -o $@
    

    It is again more generic, less error prone, and allows to use the same rule for several targets.

Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
  • Yes, there was an error in the question syntax. Sorry I was a bit sloppy there. If you would, please edit the answer to match the corrected question. I tried to edit and I realized you had text referring to the problem as well, so I rolled my edit back. – merlin2011 Jul 24 '18 at 17:52