con-f-use
con-f-use

Reputation: 4028

Can one append to a make variable without overwriting what's set in the Makefile?

Let's consider the following Makefile:

.PHONY : all

OPTS += -DBLA
OPTS += -DBLUBB
STUFF = default

all :
    ./do_something $(OPTS) $(STUFF)

One can pass variables on the command line. So with the following call

confus@confusion:/tmp/$ make STUFF=foo

make will run ./do_something -DBLA -DBLUBB foo.

Contrary to what I thought one can't append to variables:

confus@confusion:/tmp/$ make STUFF+=foo OPTS+=-DMOREOPT

will simply run ./do_something -DMOREOPT foo (as if I had left out the plus signs), when I'd expect it to ./do_something -DBLA -DBLUBB -DMOREOPT default foo.

Is there a way to append to a make variable with a command line option?

Upvotes: 0

Views: 175

Answers (1)

MadScientist
MadScientist

Reputation: 100856

If this is GNU make, you have to use the override directive in your makefile to specify that you want the values set in the makefile to take precedence over the command line values:

override OPTS += -DBLA
override OPTS += -DBLUBB
override STUFF += default

If it matters, note that this will put the settings provided on the command line first, and the settings in the makefile last.

Upvotes: 2

Related Questions