【问题标题】:makefile: syntax error near unexpected token `,'makefile:意外标记“,”附近的语法错误
【发布时间】:2018-10-12 18:18:51
【问题描述】:

执行以下代码时,我没有成功解决该问题:

%.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 (,)'

有什么想法吗?

【问题讨论】:

    标签: makefile


    【解决方案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
    

    【讨论】:

    • 与另一个问题一样,您不能使用ifneq,除非FLAG1FLAG2 是全局设置的:如果它们特定于这个目标,它将不起作用。此外,run_function1/run_function2 的定义可能会产生很大的不同——如果它们包含 make 函数,则无论 FLAG1FLAG2 的值如何,如果您使用第二个选项,它们都会运行。跨度>
    • @Geza Lore 第一个提案会产生一个问题,即 makefile 末尾缺少“endif”
    • 能否请您提供一个可重现的示例?
    • %.o: %.c if [ $(notdir $> $(notdir $
    猜你喜欢
    • 2012-08-31
    • 2012-10-20
    • 2012-09-29
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2014-02-26
    相关资源
    最近更新 更多