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

标签 variables makefile conditional

我的 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/

相关文章:

xml - 条件依赖注入(inject)

javascript - html元素形式的条件显示

postgresql - 在 plpgsql 触发器函数中用 "array_to_string"填充变量

javascript - 内部函数变量丢失外部函数的 'this' 值

C 项目中的 C++ 静态库链接

c++ - 如何解决 OSX cctools 中的构建错误?

在 R 中创建新列时通过变量名引用数据框

javascript - 变量在 iPad 上未定义与在浏览器中定义的原因是什么?

java - 如何创建一个 Makefile 来编译和运行带有命令行参数的 java 代码?

php - Laravel 条件语句 : is when() efficient?