【问题标题】:Nested For loop in makefilemakefile 中的嵌套 For 循环
【发布时间】:2015-06-20 12:33:31
【问题描述】:

我正在尝试通过 makefile 循环遍历特定目录中的 .c 文件。

我使用了以下代码,但它似乎不起作用:

DIR= Sources \
     Sources_2

@for entry in ${DIR} ;                  \
do                                         \
    @for i in $${entry}/*.c ;                \
    do                                     \
        echo "Processing $${i}";             \
        #Building Commands go here
    done                                   \
done

我收到错误:“/bin/sh: -c: line 3: syntax error near unexpected token `do'”

【问题讨论】:

    标签: shell makefile


    【解决方案1】:

    您不应在第二个 for 循环附近使用 at 符号 @@ 应该用在整个 shell 命令的开头。以下对我有用:

    DIR= Sources \
         Sources_2
    
    all:
        @for entry in ${DIR};                   \
        do                                      \
            for i in $${entry}/*.c;             \
            do                                  \
                echo "Processing $${i}";        \
            done                                \
        done
    

    【讨论】:

    • @user3438074 这是您的 Makefile 本身中的一些错误,而不是在 shell 脚本中。我已经测试了上面的代码,它完全可以工作。
    • 好的,谢谢 Oleg,我会看看 Makefile 出了什么问题...非常感谢您的帮助
    【解决方案2】:

    如下更改Makefile

    @for entry in ${DIR} ; do            \
        for i in $$entry/*.c ; do     \
            echo "Processing $$i";     \
            #Building Commands go here
        done                              \
    done
    

    原因是for循环的语法使用错误。

    【讨论】:

    • 检查更新的答案。删除entry的大括号
    • 显示你正在使用的整个Makefile
    • @user3438074 :这可能是由于没有正确格式化 Makefile。确保在 makefile 中使用 而不是 作为制表符宽度
    猜你喜欢
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    相关资源
    最近更新 更多