这可能有些琐碎,或者与make的使用哲学相悖,但我想要一个类似于“make debug”而不是“make DEBUG=1”的命令行。我尝试创建一个名为debug的伪目标,除了设置DEBUG变量外没有任何操作,但是这样会导致“make debug build”和“make build debug”之间存在差异--也就是说,在某些情况下,变量在编译后才被设置。
是否有一种方法可以使某些目标具有优先级?
感谢您的帮助。
是否有一种方法可以使某些目标具有优先级?
感谢您的帮助。
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]
你可以写以下内容:
.PHONY: debug
debug:
$(MAKE) -$(MAKEFLAGS) build DEBUG=1
build:
echo makeflags='$(MAKEFLAGS)' debug=${DEBUG}
这个至少可以与GNU Make、BSD Make和Interix Make一起使用。我没有尝试过所有其他的实现。
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.foo
或make foo.debug
,它会自动转换为make DEBUG=1 foo
,并且对于您放置在$(TARGETS)
中的任何目标都有效。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;
}
make debug
或make build
,其中make build
将是非调试版本。
至于你实际的问题,我对Makefile不够熟悉,无法回答。
target: DEBUG=1
,而不是将他指向文档... 对吗? - dlamotte