Append to GNU make variables via command line

前端 未结 4 751
执念已碎
执念已碎 2020-12-07 20:14

I am using a GNU-make Makefile to build a C project with several targets (all, clean, and a few project specific targets). In the process of debugg

4条回答
  •  醉酒成梦
    2020-12-07 20:48

    Just a note, as I got confused - let this be file testmake:

    $(eval $(info A: CFLAGS here is $(CFLAGS)))
    
    override CFLAGS += -B
    
    $(eval $(info B: CFLAGS here is $(CFLAGS)))
    
    CFLAGS += -C
    
    $(eval $(info C: CFLAGS here is $(CFLAGS)))
    
    override CFLAGS += -D
    
    $(eval $(info D: CFLAGS here is $(CFLAGS)))
    
    CFLAGS += -E
    
    $(eval $(info E: CFLAGS here is $(CFLAGS)))
    

    Then:

    $ make -f testmake
    A: CFLAGS here is 
    B: CFLAGS here is -B
    C: CFLAGS here is -B
    D: CFLAGS here is -B -D
    E: CFLAGS here is -B -D
    make: *** No targets.  Stop.
    $ make -f testmake CFLAGS+=-g
    A: CFLAGS here is -g
    B: CFLAGS here is -g -B
    C: CFLAGS here is -g -B
    D: CFLAGS here is -g -B -D
    E: CFLAGS here is -g -B -D
    make: *** No targets.  Stop.
    

    With the override directives deleted from the testmake file:

    $ make -f testmake
    A: CFLAGS here is 
    B: CFLAGS here is -B
    C: CFLAGS here is -B -C
    D: CFLAGS here is -B -C -D
    E: CFLAGS here is -B -C -D -E
    make: *** No targets.  Stop.
    $ make -f testmake CFLAGS+=-g
    A: CFLAGS here is -g
    B: CFLAGS here is -g
    C: CFLAGS here is -g
    D: CFLAGS here is -g
    E: CFLAGS here is -g
    make: *** No targets.  Stop.
    

    So,

    • if a variable used override once, it can only be appended with another statement with override (the normal assignments will be ignored);
    • when there's been no override at all; trying to append (as in +=) from the command line overwrites every instance of that variable.

提交回复
热议问题