From: Jens Thoms Toerring on
micropentium <anderswang(a)gmail.com> wrote:
> Hi,

> I have a c file foo.c:

> int main(void)
> {
> #ifdef FOO
> printf("FOO!\n");
> #endif
> #ifdef BAR
> printf("BAR!\n");
> #endif
> return 0;

> }

> and a Makefile may be like this:
> CPPFLAGS=-DFOO
> a.out:foo.o
> gcc -o $@ $^
> %.o:%.c
> gcc -c $< ${CPPFLAGS}

> if I make it and run a.out (I am on a GNU Make 3.81), the output is
> FOO!. If I want to print out BAR! or FOO!BAR!, I could always append -
> DBAR onto CPPFLAGS.

> So, my question is: what if I want to append -DBAR on the command
> line?

> I tried: make CPPFLAGS+=-DBAR

What you do here is set 'CPPFLAGS' to '-DBAR' in the environment
first (the '=' will only matter if CPPFLAGS had a value before.
Then make is started, which reads the enviroment and sets varia-
bles accordingly before starting to interpret the Makefile.

Whith in the Makefile you now have

CPPFLAGS = -DFOO

which overwrites the value from the environment.

> But it will completely replace the defined CPPFLAGS in the Makefile.
> Obviously, the one defined on the command line has the precedence.

No, it's the other way round, a new value assigned in the Makefile
will overwrite what was set previously, e.g. via the environment.

> Is this doable through command line arguments to make?

I'm not 100% certainr what you want to do. If you want to have
'-DFOO' and '-DBAR' to be set during compilation then just change
in the Makefile

CPPFLAGS += -DFOO

and invoke make with

CPPFLAGS=-DBAR make

If you want to have '-DFOO' only set if CPPFLAGS isn't set to
something else do e.g.

ifndef CPPFLAG
CPPFLAGS=-DFOO
endif

in the Makefile to avoid changing an already set value of CPPFLAG.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: micropentium on
> > But it will completely replace the defined CPPFLAGS in the Makefile.
> > Obviously, the one defined on the command line has the precedence.
>
> No, it's the other way round, a new value assigned in the Makefile
> will overwrite what was set previously, e.g. via the environment.

It depends on how you send this parameter to make

If you do:
CPPFLAGS=-DXX make
then what you said is absolutely correct: "a new value assigned in the
Makefile
will overwrite what was set previously, e.g. via the environment.".
But, you could always override that by add -e flag to make:
CPPFLAGS=-DXX make -e,
which actually replace the value of $CPPFLAGS with the one defined in
env

On the other hand, if you pass variable as make's arguments:
make CPPFLAGS=-DXX
then argument has the higher precedence than the same variable defined
in the makefile.