How to get the invoking target of makefile?

MakefileGnuGnu Make

Makefile Problem Overview


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?

Makefile Solutions


Solution 1 - Makefile

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).

Solution 2 - Makefile

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustinView Question on Stackoverflow
Solution 1 - MakefileBetaView Answer on Stackoverflow
Solution 2 - MakefileVladView Answer on Stackoverflow