makefile:意外令牌','附近的语法错误

时间:2018-10-12 18:18:51

标签: makefile

在执行以下代码时,我未能成功解决该问题:

%.o: %.c 
if [ $(notdir $<) = file1.c ]; then \
    echo "  >>  $(notdir $<) is excluded"; \
else\ 
    ifneq ($(FLAG1),)
        $(run_function1)
    endif
    ifneq ($(FLAG2),)
        $(run_function2)
    endif
fi

问题如下:

 if [ file2.c = file1.c ]; then \
 echo "  >>  file2.c is excluded"; \
 else\
 ifneq (,)
 /bin/sh: -c: line 4: syntax error near unexpected token `,'
 /bin/sh: -c: line 4: ` ifeq (,)'

有什么主意吗?

1 个答案:

答案 0 :(得分:1)

您正在配方中混合使用Shell和Makefile语法。请记住,除了$()变量扩展(即ifneq和类似的Make条件,如果缩进后,它们不会扩展),几乎没有修改就直接传递给了shell。尤其请参见此处的文档的第一段和第4项:https://www.gnu.org/software/make/manual/html_node/Recipe-Syntax.html

您的意思是这样的,但是由于\行的连续包含了随后的ifndef / endif(是任何人的想法?),因此此操作不起作用:

%.o: %.c 
    if [ $(notdir $<) = file1.c ]; then \
        echo "  >>  $(notdir $<) is excluded"; \
    else \
ifneq ($(FLAG1),)
        $(run_function1); \
endif
ifneq ($(FLAG2),)
        $(run_function2); \
endif
    fi

条件Make函数仍然应该起作用:

%.o: %.c 
    if [ $(notdir $<) = file1.c ]; then \
        echo "  >>  $(notdir $<) is excluded"; \
    else \
        $(if $(FLAG1),,$(run_function1);) \
        $(if $(FLAG2),,$(run_function2);) \
    fi

或者更可能使用shell条件语句:

%.o: %.c 
    if [ $(notdir $<) = file1.c ]; then \
        echo "  >>  $(notdir $<) is excluded"; \
    else \
        if [ -n $(FLAG1) ] \
            $(run_function1); \
        fi \
        if [ -n $(FLAG2) ] \
            $(run_function2); \
        fi \
    fi