【问题标题】:How to do repeated excution by makefile [duplicate]如何通过makefile重复执行[重复]
【发布时间】:2022-01-20 18:39:57
【问题描述】:

我正在使用 makefile 来编译我的程序。编译后,我想自动做一些测试,它将需要几个不同的文件作为输入。我正在尝试做这样的事情:

test:
   gcc test.c -o test

run:
   ./test test1.nlb >> test_output.txt
   ./test test2.nlb >> test_output.txt
   ...

我们可能有更多的测试文件,所以我在想是否有办法避免输入所有这些名称。我尝试过类似的方法:

FILE = $(wildcard *.nlb)
run:
   $(foreach file,$(FILE),./test $(file) >> test_output.txt)

但它没有用。 关于如何编写这样一个makefile的任何想法?提前致谢!

【问题讨论】:

    标签: makefile


    【解决方案1】:

    假设通配符扩展为foo.nlb bar.nlb baz.nlb。然后你的食谱:

    $(foreach file,$(FILE),./test $(file) >> test_output.txt)
    

    将扩展为:

    ./test foo.nlb >> test_output.txt ./test bar.nlb >> test_output.txt ./test baz.nlb >> test_output.txt
    

    你可以清楚地看到,它不是一个有效的 shell 命令。请记住,make 函数(有一些特殊例外)基本上是 文本操作 函数。他们不运行命令,他们只是按摩文本。

    您有两个选择:您可以使用 shell 语法编写循环,而不是 make 语法:

    for file in $(FILE); do ./test $$file >> test_output.txt; done
    

    或者您可以在 make foreach 循环中添加分号,以分隔命令:

    $(foreach file,$(FILE),./test $(file) >> test_output.txt ;)
                                                            ^^
    

    这将扩展为:

    ./test foo.nlb >> test_output.txt ; ./test bar.nlb >> test_output.txt ; ./test baz.nlb >> test_output.txt
    

    这是一个有效的 shell 脚本。

    或者更好的是,使用&& 作为分隔符,这样如果测试失败,您会注意到它:

    $(foreach file,$(FILE),./test $(file) >> test_output.txt &&)
    

    【讨论】:

    • 非常感谢!两种方式都有效。
    猜你喜欢
    • 2019-02-03
    • 1970-01-01
    • 2017-07-09
    • 2023-04-10
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多