使用 makefile 目标设置构建选项
这要么是微不足道的,要么与如何使用 make 的哲学背道而驰,但我希望有一个读取为“make debug”而不是“make DEBUG=1”的命令行。我尝试创建一个名为 debug 的虚假目标,除了设置 DEBUG 变量之外什么也不做,但是“make debug build”和“make build debug”之间存在差异 - 即在一种情况下,该变量在构建后设置发生了。
有没有办法优先考虑某些目标?
感谢您的帮助。
This is either trivial or runs counter to the philosophy of how make should be used, but I'd like to have a command line that reads as "make debug" rather than "make DEBUG=1". I tried creating a phony target called debug that did nothing except set the DEBUG variable, but then there was a difference between "make debug build" and "make build debug"--namely that in one case, the variable got set after the build happened.
Is there a way to give certain targets precedence?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅此页面中的“特定于目标的变量值”。
See 'Target-specific variable values' in this page.
您还可以通过查看 MAKECMDGOALS 变量来完成此操作,
这就是调用它时会发生的情况:
You can also do it by looking at the MAKECMDGOALS variable
This is what happens when you call it:
您可以编写以下内容:
这至少适用于 GNU Make、BSD Make 和 Interix Make。我没有尝试所有其他实现。
you can write the following:
This will at least work with GNU Make, BSD Make and Interix Make. I didn't try all the other implementations.
您可以使用 GnuMake 做的一件事是使用宏,通过内置的
foreach
扩展为规则。类似于:这将允许您输入
make debug.foo
或make foo.debug
,它会自动变成make DEBUG=1 foo
,它适用于您放入$(TARGETS)
中的任何目标One thing you can do with GnuMake is use macros that expand to rules with the
foreach
builtin. Something like:This will allow you to type
make debug.foo
ormake foo.debug
and it will automatically turn intomake DEBUG=1 foo
, and it works for any target you put in$(TARGETS)
如果您的调试仅与构建目标一起使用,您也可以进行调试调用构建,这样您只需键入
make debug
或make build
,其中make build
将是非调试的。但至于你的实际问题,我对 makefile 不够熟悉,无法回答它。
If your debug will only be used with the build target, you might as well make debug call build so you can just type
make debug
ormake build
, wheremake build
would be non-debug.But as for your actual question, I'm not familiar enough with makefiles to answer it.
一种方法是在构建和调试目标中设置依赖项和构建规则,但将调试选项添加到调试目标。一个简单的例子:
Makefile
Program.c
One approach is to set up the dependencies and build rules in both the build and debug targets, but add the your debugging options to the debug target. A simple example:
Makefile
Program.c