代码之家  ›  专栏  ›  技术社区  ›  Michael Koval

通过命令行附加到GNU make变量

  •  54
  • Michael Koval  · 技术社区  · 15 年前

    all , clean

    在过去,我是这样做的(使用调试符号示例):

    make target CFLAGS+=-g
    

    CFLAGS 变量,但将其清除并停止编译。有没有一种干净的方法可以做到这一点,而无需定义附加到函数末尾的某种伪变量 LDFLAGS ?

    4 回复  |  直到 15 年前
        1
  •  91
  •   Carl Norum    15 年前

    查看 override directive . 您可能需要修改makefile一次,但它应该做您想做的事情。

    override CFLAGS += -Wall
    
    app: main.c
        gcc $(CFLAGS) -o app main.c 
    

    命令行示例:

    $ make
    gcc -Wall -o app main.c 
    $ make CFLAGS=-g
    gcc -g -Wall -o app main.c 
    
        2
  •  31
  •   aib    13 年前

    预备

    我需要一种实际附加的方法,并提出:

    override CFLAGS := -Wall $(CFLAGS)
    
        3
  •  23
  •   Jérôme Pouiller    6 年前

    有两种方法可以传递变量以生成:

    • 使用命令行参数:

      make VAR=value
      
    • 使用环境:

      export VAR=var; make
      

      或者(更好,因为它仅为当前命令更改环境)

      VAR=var make
      

    它们略有不同。第一个更强。这意味着你知道你想要什么。第二种可能被认为是一种暗示。它们之间的区别在于运算符 = += override

    CC ?= gcc
    CFLAGS += -Wall
    INTERNAL_VARS = value
    

    并称之为:

    CFLAGS=-g make
    

    注意,如果您想退出 -Wall

    make CFLAGS=
    

    请不要使用 推翻 推翻 .

        4
  •  7
  •   sdaau    12 年前

    只是一个便条,因为我感到困惑-让这个文件 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)))
    

    然后:

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

    override 试做 文件:

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

    所以

    • 推翻 一次,它只能附加另一个带有 推翻
    • 当没有 推翻 完全试图附加(如在 += )从命令行覆盖该变量的每个实例。