在make中使用条件

时间:2014-11-13 14:21:09

标签: makefile gnu-make

我想在食谱中指定一个var来检查另一个食谱中的var。 可以做到这一点吗?

根据gmake manual,您应该能够使用构造:

ifeq ($(strip $(foo)),)

这个示例Makefile正在生成一个OK行和一个ERR行:

rule: my_file1
ifeq ($(strip $(MY_VAR1)),)
    @echo "ERR: MY_VAR1 is set to $(MY_VAR1) so this line should NOT be shown"
else
    @echo "OK: MY_VAR1 in set to $(MY_VAR1)"
endif
ifeq ($(strip $(MY_VAR2)),)
    @echo "OK: MY_VAR2 is not set so this line should be shown"
else
    @echo "ERR: MY_VAR2 in not set so this line should NOT be shown"
endif

my_file1:
    @echo "Building the file $@"
    # Some complex stuff
    # ...
    # Then assign a var to check in "rule"
    $(eval MY_VAR1+=$@)

运行make时的输出:

$ make
Building the file my_file1
# Some complex stuff
# ...
# Then assign a var to check in "rule"
ERR: MY_VAR1 is set to my_file1 so this line should NOT be shown
OK: MY_VAR2 is not set so this line should be shown

如何修复ifeq - 行,以便显示两个OK行并且没有ERR行?

我跑gmake 3.81。

1 个答案:

答案 0 :(得分:1)

你不能使用ifeq,因为ifeq就像一个预处理器语句:它在解析makefile时发生 。根据经验,在读取makefile时解析配方之外的任何(不用TAB缩进)。只有在解析了所有makefile之后,才会运行配方,并且仅在运行的配方是评估的配方的内容。

如果你想在配方中测试运行另一个配方的结果,你必须使用$(if ...)之类的函数,或者使用shell if语句,而不是make ifeq等。