【发布时间】:2015-05-04 18:29:03
【问题描述】:
我正在尝试解决一个特定问题,其中变量在一个配方中分配,然后在其他配方中进行解释,所有这些都是在运行时进行的。据我了解, ifeq 条件在解析过程中进行评估,这对我不起作用,因为其中一些总是错误的。有没有办法实现我正在尝试做的事情(预期输出如下)?如果需要,我会提供更多信息。
我在 Linux Mint 17.1 上使用 make 版本 3.81。
这是我目前所拥有的:
fourth =
all: check valueOfFourth definitionOfFourth
.PHONY: all
check:
@echo "TEST"$(cnt)
ifeq ($(first),$(second))
@echo "1. First condition"
$(eval fourth = "first")
else ifeq ($(first),$(third))
@echo "1. Second condition"
$(eval fourth = "second")
else
@echo "1. Conditions weren't met"
endif
valueOfFourth:
ifeq ($(fourth),"first")
@echo "2. First"
else ifeq ($(fourth),"second")
@echo "2. Second"
else
@echo "2."
endif
definitionOfFourth:
ifeq ($(fourth),)
@echo "3. Variable is not defined"
else
@echo "3. Variable is defined"
endif
它是这样调用的:
make cnt="1" first="x" second="x" third="y" && printf "\n" && \
make cnt="2" first="x" second="y" third="x" && printf "\n" && \
make cnt="3" first="x" second="y" third="z"
预期输出:
TEST1
1. First condition
2. First
3. Variable is defined
TEST2
1. Second condition
2. Second
3. Variable is defined
TEST3
1. Conditions weren't met
2.
3. Variable is not defined
实际输出:
TEST1
1. First condition
2.
3. Variable is not defined
TEST2
1. Second condition
2.
3. Variable is not defined
TEST3
1. Conditions weren't met
2.
3. Variable is not defined
很明显,只有“检查”目标做了它应该做的事情,其他两个根本不起作用。
【问题讨论】:
-
你为什么要这样做?你想用这个解决什么问题?使用特定于目标的变量分配(适用于所有目标的先决条件)对您有帮助吗?
-
@EtanReisner 这些只是教授给我们的练习。我,我自己,一般来说是全新的。这与构建源代码没有太大关系,它只是在玩变量和控制流。说明不是很清楚,这肯定没有帮助。
-
练习是让它与这里写的 make 一起工作?或者,更一般地说,在 make 中使用条件来影响通过 make 过程发生的事情?
-
不是让它按书面形式工作(这就是我写的试图解决这个问题),而是通过使用 ifeq、变量、虚假目标等来获得预期的输出。
-
您是否有特定的虚假目标或需要支持的命令行来执行此操作?