Variable arguments list to a Makefile

2019-09-08 07:03发布

问题:

I would like do something like following. I would like to have a variable argument list for a Makefile.

make VAR_ARG_LIST=src1,src2,src3,src4

Can I do like this? If I can, how do I extract src1,src2 or src3 from the variable VAR_ARG_LIST inside the Makefile?

Thanks,

回答1:

You really haven't provided enough information for a solution. Why do you want to extract those values? What do you want to do with them?

However, I can answer the question you asked and hope it is useful. If you're using GNU make you can do this:

COMMA := ,
VAR_ARG_LIST_A := $(subst $(COMMA), ,$(VAR_ARG_LIST))
VAR_ARG_LIST_1 := $(word 1,$(VAR_ARG_LIST_A))
VAR_ARG_LIST_2 := $(word 2,$(VAR_ARG_LIST_A))

etc.



回答2:

If you want a list of targets in a macro for make to use, use blanks to separate them (and quotes to enclose them) on the command line:

make VAR_ARG_LIST="src1 src2 src3 src4"

This can be used inside the makefile without much trouble at all:

PROGRAMS = ${VAR_ARG_LIST}

all:  ${PROGRAMS}

and it will go off and create the programs src1, ... src4 from the rest of the rules in the makefile.

If that isn't roughly what you're after, then you need to clarify your question.