Append value on the command line to a predefined macro in the makefile

M

micropentium

First of all, I should say I apologize if anybody here believe my
topic is tangent...

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
But it will completely replace the defined CPPFLAGS in the Makefile.
Obviously, the one defined on the command line has the precedence.

Is this doable through command line arguments to make?

Many thanks!
 
F

Flash Gordon

micropentium wrote:

Is this doable through command line arguments to make?

This is entirely about how to drive make and not about C. Either
comp.unix.programmer or, as it is GNU make you are using a GNU mailing
list, would be a better place to ask. You will find a higher
concentration of people who use make regularly that way.

Also the info pages should prove enlightening.
 
N

Nick Keighley

First of all, I should say I apologize if anybody here believe my
topic is tangent...

don't apologise for posting off-topic stuff, just don't do it!
 
F

Fred

First of all, I should say I apologize if anybody here believe my
topic is tangent...

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
But it will completely replace the defined CPPFLAGS in the Makefile.
Obviously, the one defined on the command line has the precedence.

Is this doable through command line arguments to make?

Many thanks!

In Makefile:
DEFINES = -DFOO $(CPPFLAGS)
gcc -c ${DEFINES} $<

Then command line:
make CPPFLAGS=-DBAR
 
T

Tom St Denis

I tried: make CPPFLAGS+=-DBAR

Learn to use bash [or your shell] and make.

Make takes environment variables as variables internally. In bash if
you wanted two things in a variable just quote it

CFLAGS="-DBAR -DFOO" make

Also in gmake [GNU make] the default for .c files is $(CC) with $
(CFLAGS) as the flags. So you don't need to reference it in your
makefile.

Tom
 
M

micropentium

I tried: make CPPFLAGS+=-DBAR

Learn to use bash [or your shell] and make.

Make takes environment variables as variables internally. In bash if
you wanted two things in a variable just quote it

CFLAGS="-DBAR -DFOO" make

Also in gmake [GNU make] the default for .c files is $(CC) with $
(CFLAGS) as the flags. So you don't need to reference it in your
makefile.

Tom

Hi Tom,

In order to make 'CFLAGS="-DBAR -DFOO" make' work under GNU make, you
have to explicitly give the -e flag to ask Make to import env
variables.
 
T

Tom St Denis

Learn to use bash [or your shell] and make.
Make takes environment variables as variables internally.  In bash if
you wanted two things in a variable just quote it
CFLAGS="-DBAR -DFOO" make
Also in gmake [GNU make] the default for .c files is $(CC) with $
(CFLAGS) as the flags.  So you don't need to reference it in your
makefile.

Hi Tom,

In order to make 'CFLAGS="-DBAR -DFOO" make'  work under GNU make, you
have to explicitly give the -e flag to ask Make to import env
variables.

No, you don't. I use make every day of my life practically...

CFLAGS="blah" make target

works fine. Some platforms have non-gnu "make" try issuing "gmake"
there.

Tom
 
M

micropentium

I tried: make CPPFLAGS+=-DBAR
Learn to use bash [or your shell] and make.
Make takes environment variables as variables internally. In bash if
you wanted two things in a variable just quote it
CFLAGS="-DBAR -DFOO" make
Also in gmake [GNU make] the default for .c files is $(CC) with $
(CFLAGS) as the flags. So you don't need to reference it in your
makefile.
Tom
In order to make 'CFLAGS="-DBAR -DFOO" make' work under GNU make, you
have to explicitly give the -e flag to ask Make to import env
variables.

No, you don't. I use make every day of my life practically...

CFLAGS="blah" make target

works fine. Some platforms have non-gnu "make" try issuing "gmake"
there.

Tom

Hi Tom,

No offense, I completely trust your profession. Let me put it this way
(correct me if I am wrong):

CFLAGS="-DBAR -DFOO" make will set CFLAGS to "-DBAR -DFOO" only if
there is no definition for CFLAGS in makefile. Otherwise, the value
assigned to CFLAGS through env will be overridden by the value defined
in the makefile. However -e flag could reverse this. Again, this is
just based upon my observation on a GNU Make 3.81 for x86_64-redhat-
linux-gnu

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top