From: Rainer Weikusat on
micropentium <anderswang(a)gmail.com> writes:
> 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.

[...]

> Is this doable through command line arguments to make?

One possibility would be (in the Makefile, untested)

ifdef FOO
CPPFLAGS := $(CPPFLAGS) -DFOO
endif

ifdef BAR
CPPFLAGS := $(CPPFLAGS) -DBAR
endif

Afterwards, make BAR=1 should result in BAR, make FOO=1 in FOO
and make BAR=1 FOO=1 in both.

From: Ralf Fassel on
* micropentium <anderswang(a)gmail.com>
| 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?

A workaround is providing a separate variable for the user to set from
the command line:

CPPFLAGS=-DFOO ${CPPFLAGS_USER}

Then the user can invoke
make CPPFLAGS_USER=-DBAR
or even
make CPPFLAGS_USER=-DBAR CPPFLAGS=""
make CPPFLAGS_USER="-DBAR -UFOO"
to prevent/undo the -DFOO.

R'