Makefile条件错误

Chr*_*art 8 conditional makefile gnu-make

我试图做一个make语句来检查架构.我非常接近它的工作:

test:
        ifeq ("$(shell arch)", "armv7l")
                echo "This is an arm system"
        else
                echo "This is not an arm system."
        endif
Run Code Online (Sandbox Code Playgroud)

我有一个问题:虽然这似乎解决了ifeq ("i386", "armv7l")哪个应该是假的,但我收到以下错误:

$ make
ifeq ("i386", "armv7l")
/bin/sh: -c: line 0: syntax error near unexpected token `"i386",'
/bin/sh: -c: line 0: `ifeq ("i386", "armv7l")'
make: *** [test] Error 2
Run Code Online (Sandbox Code Playgroud)

因此,它解析为两个字符串相互比较,但存在语法错误.这有什么不对?

Mad*_*ist 14

你不能ifeq在配方中使用make语句.食谱(以TAB开头的行)传递给shell.shell不明白ifeq; 这是一个make构造.

您必须在配方中使用shell if语句.并且,您不必$(shell ...)在配方中使用,因为您已经在shell中.

test:
        if [ `arch` = armv7l ]; then \
            echo "This is an arm system"; \
        else \
            echo "This is not an arm system."; \
        fi
Run Code Online (Sandbox Code Playgroud)

这可能不是解决这个问题的最好方法,但由于你没有提供任何关于你真正想要做什么的信息,我们可以说.

  • 感谢使用`endif`与`fi`进行修复:这是一个愚蠢的错误.但是,使用`==`而不是`=`是不正确的.shell的测试工具使用single- =运算符来比较字符串.Bash有一个不可移植的扩展,它允许它也识别`==`进行字符串比较.由于make _always_运行`/ bin/sh`,无论用户的shell如何,这都会在任何`/ bin/sh`实际上不是bash且在这方面遵守标准的系统上失败. (3认同)

Ide*_*lic 13

正如MadScientist所说,makeifeq线条传递给shell,但如果你正确地编写它,你绝对可以将make构造ifeq与命令中的命令混合在一起.你只需要了解如何make解析Makefile:

如果一行以a开头TAB,则无论该行在文件中的哪个位置,都将其视为shell的命令.

如果它不以a开头TAB,make则将其解释为其自己语言的一部分.

因此,要修复您的文件,请避免使用以下命令启动make条件TAB:

test:
ifeq ("$(shell arch)", "armv7l")
        echo "This is an arm system"
else
        echo "This is not an arm system."
endif
Run Code Online (Sandbox Code Playgroud)