gpt4 book ai didi

variables - Makefile:ifeq 指令将特殊变量与常量进行比较不起作用

转载 作者:行者123 更新时间:2023-12-05 00:07:44 27 4
gpt4 key购买 nike

我的 Makefile 有一个小问题。我想更改有关工作目录的命令。我在测试当前目标目录 ($(*D)) 的规则中添加了一个条件指令。

问题是 make 总是转到我测试的第二个分支,即使我的文件在 mySpecialDirectory 中并且 echo 确实打印了“mySpecialDirectory”。

.c.o .cpp.o .cc.o:
ifeq ($(*D),mySpecialDirectory)
@echo "I'm in mySpecialDirectory! \o/"
$(CC) $(DEBUG_FLAGS) $(MYSPECIALFLAGS) -c $< -o $@
else
@echo "Failed! I'm in $(*D)"
$(CC) $(DEBUG_FLAGS) $(NOTTHATSPECIALFLAGS) -c $< -o $@
endif

最佳答案

这是预期的行为。

Conditional Statements

All instances of conditional syntax are parsed immediately, in their entirety; this includes the ifdef, ifeq, ifndef, and ifneq forms. Of course this means that automatic variables cannot be used in conditional statements, as automatic variables are not set until the command script for that rule is invoked. If you need to use automatic variables in a conditional you must use shell conditional syntax, in your command script proper, for these tests, not make conditionals.


$(*D) 是一个自动变量。
相反,请考虑执行以下操作:
.c.o .cpp.o .cc.o:
$(CC) $(DEBUG_FLAGS) $(if $(subst mySpecialDirectory,,$(*D)),$(NOTTHATSPECIALFLAGS),$(MYSPECIALFLAGS)) -c $< -o $@
这个想法是通过用空字符串替换 $(subst) 来将 mySpecialDirectory 滥用到一些相等性测试中。然后,如果 $(*D) 扩展等于 mySpecialDirectory ,它会被空字符串完全替换,并且 $(if) 的 else 部分按照以下方式进行评估:

$(if condition,then-part[,else-part])

The if function provides support for conditional expansion in a functional context (as opposed to the GNU make makefile conditionals such as ifeq (see Syntax of Conditionals).

The first argument, condition, first has all preceding and trailing whitespace stripped, then is expanded. If it expands to any non-empty string, then the condition is considered to be true. If it expands to an empty string, the condition is considered to be false.


注意这个 hack 中 then-partelse-part 之间的翻转。
希望这可以帮助!

关于variables - Makefile:ifeq 指令将特殊变量与常量进行比较不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1834663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com