Enze Chi
Enze Chi

Reputation: 1763

How to append variable which defined in include makefile from command line?

I have a makefile include local.mk which define a variable DEFS. I hope to append that variable from command.

Here is what I have tried.

local.mk:

DEFS += -DVAR1

Makefile:

DEFS ?=
$(info ${DEFS})
include local.mk
$(info ${DEFS})

test:
    echo ${DEFS}

When I run:

make DEFS+=-DVAR2

I expect to get -DVAR1 -DVAR2 but I only got -DVAR2.

How could I append DEFS variable instead of override it from command line?

Upvotes: 1

Views: 673

Answers (2)

Beta
Beta

Reputation: 99134

Use the override directive in local.mk:

override DEFS += -DVAR1

Upvotes: 1

Etan Reisner
Etan Reisner

Reputation: 81012

I feel like this should be possible done the way you are trying but I'm having trouble getting the details straight in my head at the moment.

That being said you can do this instead:

DEFS=-DVAR2 make

and get what you want (order aside)

$ DEFS=-DVAR2 make -f start.mk
DEFS=-DVAR2
DEFS=-DVAR2 -DVAR1
echo '-DVAR2 -DVAR1'
-DVAR2 -DVAR1

Upvotes: 1

Related Questions