使用 makefile 目标设置构建选项

发布于 2024-08-31 18:47:52 字数 233 浏览 2 评论 0原文

这要么是微不足道的,要么与如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪我闹别瞎闹 2024-09-07 18:47:52

请参阅此页面中的“特定于目标的变量值”。

See 'Target-specific variable values' in this page.

浅紫色的梦幻 2024-09-07 18:47:52

您还可以通过查看 MAKECMDGOALS 变量来完成此操作,

ifneq "$(findstring debug, $(MAKECMDGOALS))" ""
DEBUG = 1
endif

build:
    @echo build and DEBUG is [$(DEBUG)]

debug:

这就是调用它时会发生的情况:

$ make build
build and DEBUG is []
$ make build debug
build and DEBUG is [1]
make: Nothing to be done for `debug'.
$ make debug build
make: Nothing to be done for `debug'.
build and DEBUG is [1]

You can also do it by looking at the MAKECMDGOALS variable

ifneq "$(findstring debug, $(MAKECMDGOALS))" ""
DEBUG = 1
endif

build:
    @echo build and DEBUG is [$(DEBUG)]

debug:

This is what happens when you call it:

$ make build
build and DEBUG is []
$ make build debug
build and DEBUG is [1]
make: Nothing to be done for `debug'.
$ make debug build
make: Nothing to be done for `debug'.
build and DEBUG is [1]
〗斷ホ乔殘χμё〖 2024-09-07 18:47:52

您可以编写以下内容:

.PHONY: debug
debug:
        $(MAKE) -$(MAKEFLAGS) build DEBUG=1

build:
        echo makeflags='$(MAKEFLAGS)' debug=${DEBUG}

这至少适用于 GNU Make、BSD Make 和 Interix Make。我没有尝试所有其他实现。

you can write the following:

.PHONY: debug
debug:
        $(MAKE) -$(MAKEFLAGS) build DEBUG=1

build:
        echo makeflags='$(MAKEFLAGS)' debug=${DEBUG}

This will at least work with GNU Make, BSD Make and Interix Make. I didn't try all the other implementations.

雪若未夕 2024-09-07 18:47:52

您可以使用 GnuMake 做的一件事是使用宏,通过内置的 foreach 扩展为规则。类似于:

TARGETS := build all foo bar baz

define DEBUG_TARGET_RULE
$(1).debug:
        $(MAKE) DEBUG=1 $(1)
debug.$(1):
        $(MAKE) DEBUG=1 $(1)
endef

$(foreach target,$(TARGETS),$(eval $(call DEBUG_TARGET_RULE,$(target))))

这将允许您输入 make debug.foomake 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:

TARGETS := build all foo bar baz

define DEBUG_TARGET_RULE
$(1).debug:
        $(MAKE) DEBUG=1 $(1)
debug.$(1):
        $(MAKE) DEBUG=1 $(1)
endef

$(foreach target,$(TARGETS),$(eval $(call DEBUG_TARGET_RULE,$(target))))

This will allow you to type make debug.foo or make foo.debug and it will automatically turn into make DEBUG=1 foo, and it works for any target you put in $(TARGETS)

静水深流 2024-09-07 18:47:52

如果您的调试仅与构建目标一起使用,您也可以进行调试调用构建,这样您只需键入 make debugmake 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 or make build, where make build would be non-debug.

But as for your actual question, I'm not familiar enough with makefiles to answer it.

最佳男配角 2024-09-07 18:47:52

一种方法是在构建和调试目标中设置依赖项和构建规则,但将调试选项添加到调试目标。一个简单的例子:

Makefile

program:        program.c
                gcc -o program program.c
debug:          program.c
                gcc -D DEBUG -o program program.c

Program.c

#include <stdio.h>
int main(void) {
#ifdef DEBUG
  printf("DEBUG on!\n");
#endif
  printf("in the program\n");
  return 0;
}

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:        program.c
                gcc -o program program.c
debug:          program.c
                gcc -D DEBUG -o program program.c

Program.c

#include <stdio.h>
int main(void) {
#ifdef DEBUG
  printf("DEBUG on!\n");
#endif
  printf("in the program\n");
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文