裹递归扩展变量与同名
问题描述:
新一个我可以使用+ =到追加递归扩展变量,它工作得很好。裹递归扩展变量与同名
在现实世界中,有时也需要前插或包裹可变进一些代码,无需扩大。
我在这里找到类似的问题GNU make: prepend a recursively expanded variable? 但无法快速改变我的情况。
基本上,我需要这样的:
define Xdef
@echo $(1)
endef
define Xdef
@echo Before
# Value of original Xdef is here. i.e. echo $(1)
@echo After
endef
all:
$(call Xdef,Hello world)
随着未来结果:
$ make
Before
Hello world
After
感谢。
答
好的,你让我想到了这件事,它有可能 - 虽然它很丑,但我会强烈建议不要使用这个,因为你会混淆尝试维护makefile的任何人。你可以通过在makefile中创建一个新的定义来完成。原来的定义必须是一个?=
,所以当的makefile重新解析它不会覆盖新定义...
include new.mak
define Xdef ?=
@echo $(1)
endef
.PHONY: all
all:
@echo running [email protected]
$(call Xdef,hello world)
new.mak: Makefile
@echo rebuilding [email protected]
@echo "define Xdef =" >> [email protected]
@echo " @echo Before" >> [email protected]
@echo '$(call Xdef,\$$(1))' >> [email protected]
@echo " @echo After" >> [email protected]
@echo "endef" >> [email protected]
clean:
rm new.mak
我不知道你想做的事是可能的。如果你试图在自己的定义中引用一个延迟变量,它将不能正确地扩展(我只是试图崩溃)。如果你尝试使用立即定义,变量会立即被扩展,所以'$(1)'解决为空白。 – John