我正在使用 GNU-make Makefile 构建一个具有多个目标的 C 项目(
all
、clean
和一些项目特定目标)。在调试过程中,我想在单个构建中附加一些标志,而无需永久编辑 Makefile(例如添加调试符号或设置预处理器标志)。
过去,我是这样做的(使用调试符号示例):
make target CFLAGS+=-g
不幸的是,这不是附加到
CFLAGS
变量,而是清除它并阻止它编译。有没有一种干净的方法可以做到这一点,而无需定义附加到 CFLAGS
和 LDFLAGS
末尾的某种虚拟变量?
查看覆盖指令。您可能需要修改一次 makefile,但它应该可以满足您的要求。
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
向 make 传递变量有两种方法:
使用命令行参数:
make VAR=value
使用环境:
export VAR=var; make
或者(更好,因为它只改变当前命令的环境)
VAR=var make
它们略有不同。第一个更强。这意味着你知道自己想要什么。第二个可以被认为是一个提示。它们之间的区别在于运算符
=
和 +=
(没有 override
)。在命令行中定义变量时,这些运算符将被忽略,但在环境中定义变量时,这些运算符不会被忽略。因此,我建议你有一个 Makefile:
CC ?= gcc
CFLAGS += -Wall
INTERNAL_VARS = value
并用以下方式调用它:
CFLAGS=-g make
注意,如果您想提现
-Wall
,您可以使用:
make CFLAGS=
请不要使用
override
关键字,否则您将无法更改受 override
影响的变量。
请注意,因为我很困惑 - 将此作为文件
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
文件中删除 testmake
指令后:
$ 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.
那么,
override
一次,它只能附加另一个带有 override
的语句(正常赋值将被忽略); override
时;尝试从命令行追加(如+=
)会覆盖该变量的每个实例。