1

I am working on a (GNU) Makefile and need to save arguments in a variable. Like if I give the command make projectX, then I need to assign projectX to some variable.

I tried to assign projectX like this, assuming that argument 1 would be projectX.

PRODUCT := "$1"

But this does not work.

What is the best way to assign make arguments in a variable?

Komal R.
  • 31
  • 2

2 Answers2

2

From the GNU make manual:

Make will set the special variable MAKECMDGOALS to the list of goals you specified on the command line. If no goals were given on the command line, this variable is empty. Note that this variable should be used only in special circumstances.

And note that usually you would/should/must use $@ to refer to the target of a rule.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • There is also `$(MAKEFLAGS)`, which contains all other arguments (i.e. flags rather than tagets). – HolyBlackCat Oct 08 '19 at 21:46
  • Remember that `MAKECMDGOALS` is a list and may be empty (if no target is provided) or have multiple targets (like in `make clean projectX`). Therefore while the answer is correct, I would rather use approach with a variable instead to have less tricky scenarios. – raspy Oct 17 '19 at 13:17
1

You can also assign variables in make command line:

make PRODUCT=bla

Which is often used for debug/release builds:

make # builds debug version
make MODE=release # builds release version
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271