从命令行传递附加变量到 make
我可以将变量作为命令行参数传递给 GNU Makefile 吗?换句话说,我想传递一些参数,这些参数最终将成为 Makefile 中的变量。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我可以将变量作为命令行参数传递给 GNU Makefile 吗?换句话说,我想传递一些参数,这些参数最终将成为 Makefile 中的变量。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
您可以通过多种方式从 makefile 外部设置变量:
从环境 - 每个环境变量都会转换为具有相同名称和值的 makefile 变量。
您可能还想设置
-e
选项(又名--environments-override
),并且您的环境变量将覆盖 makefile 中的分配(除非这些分配他们自己使用override
指令 但是,不建议这样做,使用?=
赋值(条件变量赋值运算符,只有在变量尚未定义时才有效)要好得多、更灵活:请注意,某些变量不是从环境继承的:
MAKE
从脚本名称中获取SHELL
要么在 makefile 中设置,要么默认为/bin/sh
(基本原理:命令在 makefile 中指定,并且它们是特定于 shell 的) .从命令行 -
make
可以将变量赋值作为命令行的一部分,与目标混合:但是makefile中对
FOO
变量的所有赋值都将被忽略,除非您使用override
指令 赋值。 (效果与环境变量的-e
选项相同)。从父 Make 导出 - 如果从 Makefile 调用 Make,通常不应显式编写如下变量赋值:
相反,更好的解决方案可能是导出这些变量。导出变量会使其进入每个 shell 调用的环境中,并且从这些命令中进行调用会选择上面指定的这些环境变量。
您还可以使用不带参数的
export
导出所有变量。You have several options to set up variables from outside your makefile:
From environment - each environment variable is transformed into a makefile variable with the same name and value.
You may also want to set
-e
option (aka--environments-override
) on, and your environment variables will override assignments made into makefile (unless these assignments themselves use theoverride
directive . However, it's not recommended, and it's much better and flexible to use?=
assignment (the conditional variable assignment operator, it only has an effect if the variable is not yet defined):Note that certain variables are not inherited from environment:
MAKE
is gotten from name of the scriptSHELL
is either set within a makefile, or defaults to/bin/sh
(rationale: commands are specified within the makefile, and they're shell-specific).From command line -
make
can take variable assignments as part of his command line, mingled with targets:But then all assignments to
FOO
variable within the makefile will be ignored unless you use theoverride
directive in assignment. (The effect is the same as with-e
option for environment variables).Exporting from the parent Make - if you call Make from a Makefile, you usually shouldn't explicitly write variable assignments like this:
Instead, better solution might be to export these variables. Exporting a variable makes it into the environment of every shell invocation, and Make calls from these commands pick these environment variable as specified above.
You can also export all variables by using
export
without arguments.最简单的方法是:
然后在你的makefile中你可以引用
$(foo)
。请注意,这不会自动传播到子品牌。如果您使用子品牌,请参阅本文:将变量传递给子品牌-制作
The simplest way is:
Then in your makefile you can refer to
$(foo)
. Note that this won't propagate to sub-makes automatically.If you are using sub-makes, see this article: Communicating Variables to a Sub-make
假设您有一个如下所示的 makefile:
然后您可以将其命名为
make action argument=something
Say you have a makefile like this:
You would then call it
make action argument=something
看来命令参数覆盖了环境变量。
Makefile:
示例运行:
It seems command args overwrite environment variable.
Makefile:
Example run:
来自手册:
所以你可以(从bash):
在你的Makefile中产生一个变量
FOOBAR
。From the manual:
So you can do (from bash):
resulting in a variable
FOOBAR
in your Makefile.这里没有引用另一个选项,它包含在 Stallman 和 McGrath 的 GNU Make 书中(参见 http://www.chemie.fu-berlin.de/chemnet/use/info/make/make_7.html)。它提供了示例:
它涉及验证给定参数是否出现在
MAKEFLAGS
中。例如..假设您正在研究 c++11 中的线程,并且您已将研究分为多个文件(class01
、...、classNM
)并且您想要:编译全部并单独运行,或者一次编译一个并在指定标志(例如-r
)的情况下运行它。因此,您可以想出以下Makefile
:有了它,您就可以: 使用
make -r class02
构建并运行一个文件;make
或make all
构建所有内容;make -r
构建并运行所有内容(假设它们都包含某种特定类型的断言内容,并且您只想测试它们)There's another option not cited here which is included in the GNU Make book by Stallman and McGrath (see http://www.chemie.fu-berlin.de/chemnet/use/info/make/make_7.html). It provides the example:
It involves verifying if a given parameter appears in
MAKEFLAGS
. For example .. suppose that you're studying about threads in c++11 and you've divided your study across multiple files (class01
, ... ,classNM
) and you want to: compile then all and run individually or compile one at a time and run it if a flag is specified (-r
, for instance). So, you could come up with the followingMakefile
:Having that, you'd:
make -r class02
;make
ormake all
;make -r
(suppose that all of them contain some certain kind of assert stuff and you just want to test them all)如果你创建一个名为 Makefile 的文件并添加一个像这样的变量 $(unittest)
那么你就可以在 Makefile 中使用这个变量,即使有通配符
示例:
我使用 BOOST_TEST 并给参数提供通配符 --run_test=$(unittest)
然后我将能够使用正则表达式来过滤掉我想要我的 Makefile 的测试
运行
If you make a file called Makefile and add a variable like this $(unittest)
then you will be able to use this variable inside the Makefile even with wildcards
example :
I use BOOST_TEST and by giving a wildcard to parameter --run_test=$(unittest)
then I will be able to use regular expression to filter out the test I want my Makefile
to run
然后在 Makefile 中使用变量
$(ROOT_DIR)
。Then use the variable,
$(ROOT_DIR)
in the Makefile.