我正在使用
例如:
你可以从上面的代码中看到,我正在复制多行
但是,如果我稍后尝试使用它们(仍在define内部),例如
测试两个答案
我尝试了eriktous和Ise Wisteria的方法,它们都“可行”。它们都没有创建私有变量,只是全局变量,这对我来说没问题,我只需要小心碰撞变量,并且不能进行任何递归调用(我也没有计划)。
eriktous的方法的问题在于添加$$使得该变量在外部被评估,也就是说,$$作为单个$输出,并且仅在调用目标时进行评估。因此,调用宏两次将覆盖它。
Ise Wisteria的方法确实做到了我所需要的。调用eval确保立即声明该变量,并且在宏被evaled时可用并扩展该变量。这意味着我无法在宏内将其设置为两个不同的值,但我也可以接受这一点。
这是我用来测试它的makefile。
define
创建宏。然而,在define结构内部,我无法创建变量。分配变量不会导致错误,但当我稍后尝试使用它时,其值为空。例如:
#######################################################
## JIDL_RULE
## Macro to build and install PHP and JS files
## from idl.json files
#######################################################
## param $(1) Full path to idl.json file
## param $(2) Path to directory to copy PHP file into (relative to where make is run from)
## param $(3) Path to directory to copy JS file into (relative to where make is run from)
##########################################################
define JIDL_RULE
# Create the output directory if it doesn't exist
$(2):
mkdir -p $(2)
$(3):
mkdir -p $(3)
# Rule to generate a PHP file. Notice that we have to prepend `pwd`
$(call GENERATED_FILE,$(1),$(2),php): $(1)
$(PHPHOME)/bin/php -f $(JIDL2PHP) -- `pwd`/$$< $(DATADEF_HOME) > $$@
# Rule to generate a JS file
$(call GENERATED_FILE,$(1),$(3),js): $(1)
$(PHPHOME)/bin/php -f $(JIDL2JS) -- `pwd`/$$< $(DATADEF_HOME) > $$@
# Add those generated files to the all target
all:: $(call GENERATED_FILE,$(1),$(2),php) $(call GENERATED_FILE,$(1),$(3),js) $(2) $(3)
# Remove generated files on clean:
clean::
-$(RM) -f $(call GENERATED_FILE,$(1),$(2),php) $(call GENERATED_FILE,$(1),$(3),js)
# Rules to install generated files
$(call PHP_RULE, $(call GENERATED_FILE,$(1),$(2),php))
$(call JS_RULE, $(call GENERATED_FILE,$(1),$(3),js))
endef
你可以从上面的代码中看到,我正在复制多行
$(call GENERATED_FILE,$(1),$(2),php)
和 $(call GENERATED_FILE,$(1),$(3),js)
的内容。因此,我尝试在宏中的前两个语句中创建两个变量,如下所示:PHP_OUT_FILE := $(call GENERATED_FILE,$(1),$(2),php)
JS_OUT_FILE := $(call GENERATED_FILE,$(1),$(3),js)
但是,如果我稍后尝试使用它们(仍在define内部),例如
$(PHP_OUT_FILE)
或$(JS_OUT_FILE)
,那么变量为空。我希望能够从我的宏中删除这种重复。我做错了吗?不可能吗?还有其他方法吗?测试两个答案
我尝试了eriktous和Ise Wisteria的方法,它们都“可行”。它们都没有创建私有变量,只是全局变量,这对我来说没问题,我只需要小心碰撞变量,并且不能进行任何递归调用(我也没有计划)。
eriktous的方法的问题在于添加$$使得该变量在外部被评估,也就是说,$$作为单个$输出,并且仅在调用目标时进行评估。因此,调用宏两次将覆盖它。
Ise Wisteria的方法确实做到了我所需要的。调用eval确保立即声明该变量,并且在宏被evaled时可用并扩展该变量。这意味着我无法在宏内将其设置为两个不同的值,但我也可以接受这一点。
这是我用来测试它的makefile。
define TEST
$(eval INNERVAR := Blah $(1))
inner::
echo Using EVAL: INNERVAR = $(INNERVAR)
echo
endef
define TEST2
INNERVAR2 := Blah $(1)
inner::
echo Using double dollar sign: INNERVAR2 = $$(INNERVAR2)
echo
endef
$(eval $(call TEST,TEST_1_A))
$(eval $(call TEST,TEST_1_B))
$(eval $(call TEST2,TEST_2_A))
$(eval $(call TEST2,TEST_2_B))
inner::
echo is that var really private? $(INNERVAR)
echo is that var really private? $(INNERVAR2)
以下是输出结果:我省略了make运行语句的回显,以便更容易查看。
Using EVAL: INNERVAR = Blah TEST_1_A
Using EVAL: INNERVAR = Blah TEST_1_B
# Calling the macro twice overwrites the global variable and since
# it's not expanded immediately, calling the macro with different params
# will output the last value that we set the variable to
Using double dollar sign: INNERVAR2 = Blah TEST_2_B
Using double dollar sign: INNERVAR2 = Blah TEST_2_B
# As expected, the variables are not private to the macro.
is that var really private? Blah TEST_1_B
is that var really private? Blah TEST_2_B