51

How to get the invoking target of the GNU make Makefile?

for example, I invoke make with the following command line:

make a-target

How can I get the invoking target "a-target" in the Makefile and assign it to a variable?

Further more, if more than one target is specified on the command line:

make target1 target2 ...

How do I get all of them?

user1129682
  • 889
  • 8
  • 24
Justin
  • 653
  • 1
  • 5
  • 7

2 Answers2

82

The variable MAKECMDGOALS contains the list of targets that were specified on the command line, no matter how many (it's empty if there were none).

Petr
  • 61,569
  • 11
  • 147
  • 305
Beta
  • 92,251
  • 12
  • 140
  • 145
20

Maybe you need $@?

See http://www.gnu.org/software/make/manual/make.html#Automatic-Variables for more details.

Vlad
  • 34,303
  • 6
  • 78
  • 192