C语言 通过命令行附加到 GNU make 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2129391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append to GNU make variables via command line
提问by Michael Koval
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 debugging, I would like to append some flags to a single build without permanently editing the Makefile (e.g. add debugging symbols or set a preprocessor flag).
我使用的是GNU-化妆的Makefile建立与几个目标(一个C项目all,clean以及一些项目的具体目标)。在调试过程中,我想在不永久编辑 Makefile 的情况下将一些标志附加到单个构建中(例如添加调试符号或设置预处理器标志)。
In the past, I have done that as follows (using the debugging symbols example):
过去,我是这样做的(使用调试符号示例):
make target CFLAGS+=-g
Unfortunately, this is not appending to the CFLAGSvariable, but instead, clearing it and stopping it from compiling. Is there a clean way of doing this without defining some sort of dummy variable appended to the end of CFLAGSand LDFLAGS?
不幸的是,这不是附加到CFLAGS变量,而是清除它并阻止它编译。有没有一种干净的方法来做到这一点,而无需定义某种附加到CFLAGSand末尾的虚拟变量LDFLAGS?
回答by Carl Norum
Check out the override directive. You will probably need to modify the makefile once, but it should do what you want.
查看覆盖指令。您可能需要修改 makefile 一次,但它应该可以满足您的需求。
Example makefile:
示例生成文件:
override CFLAGS += -Wall
app: main.c
gcc $(CFLAGS) -o app main.c
Example command lines:
示例命令行:
$ make
gcc -Wall -o app main.c
$ make CFLAGS=-g
gcc -g -Wall -o app main.c
回答by aib
For the record, @Carl Norum's answer prependsthe variable, from the command line perspective.
为了记录在案,@Carl Norum时的回答预先考虑的变量,在命令行的观点。
I needed a way to actually append and came up with:
我需要一种方法来实际追加并提出:
override CFLAGS := -Wall $(CFLAGS)
回答by Jér?me Pouiller
There are two ways to pass variables to make:
传递变量给make有两种方式:
Using command line arguments:
make VAR=valueUsing environment:
export VAR=var; makeor (better because it change environment only for current command)
VAR=var make
使用命令行参数:
make VAR=value使用环境:
export VAR=var; make或(更好,因为它只为当前命令改变环境)
VAR=var make
They are slightly different. The first one is stronger. It mean you know what you want. The second may be considered like a hint. Difference between them is about operators =and +=(without override). These operators are ignored when a variable is defined on command line, but are not ignored when variable is defined in environment. Thus, I suggest you to have a Makefile with:
它们略有不同。第一个更强。这意味着你知道你想要什么。第二个可以被认为是一个提示。它们之间的区别在于运算符=和+=(没有override)。当在命令行上定义变量时,这些运算符会被忽略,但在环境中定义变量时不会被忽略。因此,我建议你有一个 Makefile :
CC ?= gcc
CFLAGS += -Wall
INTERNAL_VARS = value
and call it with:
并调用它:
CFLAGS=-g make
Notice, if you want to withdraw -Wall, you can use:
注意,如果你想提现-Wall,你可以使用:
make CFLAGS=
Please don't use overridekeyword, else you won't have any way to change a variable affected with override.
请不要使用override关键字,否则您将无法更改受override.
回答by sdaau
Just a note, as I got confused - let this be file testmake:
只是一个说明,因为我很困惑 - 让这成为文件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 overridedirectives deleted from the testmakefile:
随着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.
So,
所以,
- if a variable used
overrideonce, it can only be appended with another statement withoverride(the normal assignments will be ignored); - when there's been no
overrideat all; trying to append (as in+=) from the command line overwrites every instance of that variable.
- 如果一个变量使用
override一次,它只能附加另一个语句 withoverride(正常的赋值将被忽略); - 当根本没有的
override时候;尝试+=从命令行追加(如)会覆盖该变量的每个实例。

