ifeq问题:比较包含点的2个字符串

use*_*076 4 gnu-make

我试图实现一个简单的字符串比较来获取文件的类型(使用其扩展名),如下所示:

extract_pkg: $(PKG)
    $(eval EXT := $(suffix $(PKG)))
    @echo $(EXT)
ifeq ($(EXT), .zip)
    @echo "is zip file"
else
    @echo "is not a zip file"
endif

extract_pkg : PKG = mypkg.zip
Run Code Online (Sandbox Code Playgroud)

但是,当我运行它时,它会进入else分支.我的猜测是,它与点有关,但我没有找到解决方案.谢谢你的帮助 !

编辑1:基本代码实际上有点像以下,它按预期工作:

test_cmp:
ifeq (.zip,.zip)
        @echo ".zip==.zip"
endif
ifeq (zip,zip)
        @echo "zip==zip"
endif
Run Code Online (Sandbox Code Playgroud)

因此问题出在其他地方!

Chr*_*odd 9

有一点需要注意 - if构造中的空格很重要.所以如果你有类似的东西:

ifeq ($(EXT), .zip)
Run Code Online (Sandbox Code Playgroud)

它只会匹配,如果$(EXT)扩展到完全" .zip" - 包括句点之前的空格.因此,您的第一个示例将始终打印is not a zip file,因为$(EXT)永远不会包含空格.


Mad*_*ist 9

你不能在食谱中使用ifeq()等.ifeq()是预处理程序语句:在读入makefile时会立即解释它们.在解析所有makefile并决定需要更新此目标之后,配方才会运行很久.因此,尝试使用eval等在配方中设置变量,然后使用ifeq()测试该变量不起作用.

你必须为此使用shell构造; 就像是:

extract_pkg: $(PKG)
        @EXT=$(suffix $<); \
         echo $$EXT; \
         if [ $$EXT = .zip ]; then \
            echo "is zip file"; \
        else \
            echo "is not a zip file"; \
        fi
Run Code Online (Sandbox Code Playgroud)