String GNUMakefile中的字符串比较始终为true

String GNUMakefile中的字符串比较始终为true,string,makefile,gnu-make,String,Makefile,Gnu Make,我在GNUMakefile中有一个比较字符串的逻辑条件。不管我比较的变量值是多少,它总是落在if分支中 myRecipe: $(eval PLAT := /tmp) \ if [ $(SYS_NAME) = linux-x86 ]; then \ $(eval PLAT := /temp) \ echo $(PLAT); \ fi 如果$(SYS_NAME)是linux-x86,它将执行以下操作: \ if [ linux

我在GNUMakefile中有一个比较字符串的逻辑条件。不管我比较的变量值是多少,它总是落在if分支中

myRecipe:
    $(eval PLAT := /tmp) \
    if [ $(SYS_NAME) = linux-x86 ]; then \
        $(eval PLAT := /temp) \
        echo $(PLAT); \
    fi
如果$(SYS_NAME)是linux-x86,它将执行以下操作:

\
        if [ linux-x86 = linux-x86 ]; then \
             \
            echo /temp; \
    fi        
/temp
\
    if [ aix61 = linux-x86 ]; then \
             \
            echo /temp; \
    fi
如果$(SYS_NAME)是aix61,则执行为:

\
        if [ linux-x86 = linux-x86 ]; then \
             \
            echo /temp; \
    fi        
/temp
\
    if [ aix61 = linux-x86 ]; then \
             \
            echo /temp; \
    fi

为什么字符串比较不能正常工作?

不能混合shell条件:

if [ $(SYS_NAME) = linux-x86 ]; then
使用GNU make函数:

$(eval PLAT := /temp)
GNU make函数将无条件执行

您应该使用GNU make conditionals
ifeq
etc(可能使用
$(shell)


另外,请确保不要将make变量
${FOO}
与shell变量
$${FOO}

混淆。不能混合使用shell条件:

if [ $(SYS_NAME) = linux-x86 ]; then
使用GNU make函数:

$(eval PLAT := /temp)
GNU make函数将无条件执行

您应该使用GNU make conditionals
ifeq
etc(可能使用
$(shell)

另外,请确保不要将make变量
${FOO}
与shell变量
$${FOO}

混淆。当make决定执行您的配方时,它会一次性展开整个配方,然后在单独的shell中逐个执行结果中的每一行

因此,在您的示例中,make展开

痛苦的细节:

  • $(eval PLAT:=/tmp)
    成为空字符串。作为扩展的副作用,
    PLAT
    make变量变为
    /tmp
  • $(系统名称)
    变成
    linux
    (比如说)
  • $(eval PLAT:=/tmp)
    为空,
    PLAT
    变为
    /temp
Make留下了

if [ linux = linux-x86 ]; then \
    echo /temp; \
fi
它会尽职尽责地将其传递到单个shell(因为它是一行)。

当make决定执行您的配方时,它会一次性展开整个配方,然后在单独的shell中逐个执行结果中的每一行

因此,在您的示例中,make展开

痛苦的细节:

  • $(eval PLAT:=/tmp)
    成为空字符串。作为扩展的副作用,
    PLAT
    make变量变为
    /tmp
  • $(系统名称)
    变成
    linux
    (比如说)
  • $(eval PLAT:=/tmp)
    为空,
    PLAT
    变为
    /temp
Make留下了

if [ linux = linux-x86 ]; then \
    echo /temp; \
fi
它尽职尽责地将其传递到单个外壳(因为它是一条单线)