Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Append value on the command line to a predefined macro in the makefile

Reply
Thread Tools

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

 
 
micropentium
Guest
Posts: n/a
 
      01-27-2010
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!
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      01-27-2010
micropentium wrote:

<snip>

> 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.
--
Flash Gordon
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-28-2010
On 27 Jan, 22:32, micropentium <andersw...@gmail.com> wrote:

> 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!


 
Reply With Quote
 
micropentium
Guest
Posts: n/a
 
      01-28-2010
On Jan 28, 3:57 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> On 27 Jan, 22:32, micropentium <andersw...@gmail.com> wrote:
>
> > 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!


Thank you for the 2 gentlemen above who lead me to the right
direction. I found a thread on gnu make maillist for this:
http://lists.gnu.org/archive/html/he.../msg00001.html

 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      01-28-2010
On Jan 27, 2:32*pm, micropentium <andersw...@gmail.com> wrote:
> 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

--
Fred K
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      01-28-2010
On Jan 27, 5:32*pm, micropentium <andersw...@gmail.com> wrote:
> 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
 
Reply With Quote
 
micropentium
Guest
Posts: n/a
 
      01-28-2010
On Jan 28, 10:47 am, Tom St Denis <t...@iahu.ca> wrote:
> On Jan 27, 5:32 pm, micropentium <andersw...@gmail.com> wrote:
>
> > 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.
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      01-28-2010
On Jan 28, 11:49*am, micropentium <andersw...@gmail.com> wrote:
> On Jan 28, 10:47 am, Tom St Denis <t...@iahu.ca> wrote:
>
>
>
> > On Jan 27, 5:32 pm, micropentium <andersw...@gmail.com> wrote:

>
> > > 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.


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
 
Reply With Quote
 
micropentium
Guest
Posts: n/a
 
      01-28-2010
On Jan 28, 12:35 pm, Tom St Denis <t...@iahu.ca> wrote:
> On Jan 28, 11:49 am, micropentium <andersw...@gmail.com> wrote:
>
>
>
> > On Jan 28, 10:47 am, Tom St Denis <t...@iahu.ca> wrote:

>
> > > On Jan 27, 5:32 pm, micropentium <andersw...@gmail.com> wrote:

>
> > > > 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.

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
command line tool to convert VC project files to Makefile Dave C++ 9 01-01-2012 05:56 PM
Predefined macro list. jason.cipriani@gmail.com C++ 2 01-09-2009 01:06 PM
Predefined macro list. jason.cipriani@gmail.com C Programming 2 01-09-2009 01:06 PM
Predefined macro names for machine dependent compilation utab C++ 3 07-05-2007 10:37 PM
Predefined macro namespace J.G.Harston C Programming 6 12-09-2003 05:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57