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

Discussion in 'C Programming' started by micropentium, Jan 27, 2010.

  1. micropentium

    micropentium Guest

    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!
     
    micropentium, Jan 27, 2010
    #1
    1. Advertising

  2. micropentium

    Flash Gordon Guest

    Re: Append value on the command line to a predefined macro in themake file

    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
     
    Flash Gordon, Jan 27, 2010
    #2
    1. Advertising

  3. Re: Append value on the command line to a predefined macro in themake file

    On 27 Jan, 22:32, micropentium <> 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!
     
    Nick Keighley, Jan 28, 2010
    #3
  4. micropentium

    micropentium Guest

    Re: Append value on the command line to a predefined macro in themake file

    On Jan 28, 3:57 am, Nick Keighley <>
    wrote:
    > On 27 Jan, 22:32, micropentium <> 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/help-make/2003-02/msg00001.html
     
    micropentium, Jan 28, 2010
    #4
  5. micropentium

    Fred Guest

    Re: Append value on the command line to a predefined macro in themake file

    On Jan 27, 2:32 pm, micropentium <> 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
     
    Fred, Jan 28, 2010
    #5
  6. micropentium

    Tom St Denis Guest

    Re: Append value on the command line to a predefined macro in themake file

    On Jan 27, 5:32 pm, micropentium <> 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
     
    Tom St Denis, Jan 28, 2010
    #6
  7. micropentium

    micropentium Guest

    Re: Append value on the command line to a predefined macro in themake file

    On Jan 28, 10:47 am, Tom St Denis <> wrote:
    > On Jan 27, 5:32 pm, micropentium <> 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.
     
    micropentium, Jan 28, 2010
    #7
  8. micropentium

    Tom St Denis Guest

    Re: Append value on the command line to a predefined macro in themake file

    On Jan 28, 11:49 am, micropentium <> wrote:
    > On Jan 28, 10:47 am, Tom St Denis <> wrote:
    >
    >
    >
    > > On Jan 27, 5:32 pm, micropentium <> 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
     
    Tom St Denis, Jan 28, 2010
    #8
  9. micropentium

    micropentium Guest

    Re: Append value on the command line to a predefined macro in themake file

    On Jan 28, 12:35 pm, Tom St Denis <> wrote:
    > On Jan 28, 11:49 am, micropentium <> wrote:
    >
    >
    >
    > > On Jan 28, 10:47 am, Tom St Denis <> wrote:

    >
    > > > On Jan 27, 5:32 pm, micropentium <> 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
     
    micropentium, Jan 28, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    9
    Views:
    16,722
    RichardShepherd
    Jan 1, 2012
  2. J.G.Harston

    Predefined macro namespace

    J.G.Harston, Dec 5, 2003, in forum: C Programming
    Replies:
    6
    Views:
    649
    Dan Pop
    Dec 9, 2003
  3. utab
    Replies:
    3
    Views:
    395
    Sherm Pendley
    Jul 5, 2007
  4. Replies:
    2
    Views:
    499
    Rüdiger Ranft
    Jan 9, 2009
  5. Predefined macro list.

    , Dec 31, 2008, in forum: C Programming
    Replies:
    2
    Views:
    1,406
    Rüdiger Ranft
    Jan 9, 2009
Loading...

Share This Page