Komal R.
Komal R.

Reputation: 31

How to save make argument in a variable

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?

Upvotes: 1

Views: 305

Answers (2)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136266

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

Upvotes: 1

Jens
Jens

Reputation: 72649

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.

Upvotes: 2

Related Questions