简体   繁体   English

如何使用Makefile创建测试结果

[英]How to use makefile to create test results

I have a C program - example.c and once it compiles I want to use it to generate test results. 我有一个C程序example.c ,一旦编译,我想用它来生成测试结果。 Each file under ./tests/%.in is a test case. ./tests/%.in下的每个文件都是一个测试用例。

And I want from each of them to create ./tests/%.out 我想从他们每个./tests/%.out创建./tests/%.out

I tried something like that: 我尝试过这样的事情:

all: test_results

example: example.c
    gcc example.c -o example

test_results: example ./tests/%.out

./tests/%.out: ./tests/%.in
   ./example $^ > $@

But I get errors, and it doesn't really seem to do the job. 但是我遇到了错误,而且似乎并没有真正做到。

% is the wildcard character only in a pattern rule, if you want to get every files under a directory, use * along with the wildcard function instead: %仅在模式规则中是通配符,如果要获取目录下的每个文件,请使用*wildcard一起使用:

all: test_results

example: example.c
    gcc example.c -o example

TEST_INPUT = $(wildcard tests/*.in)
test_results: example $(TEST_INPUT:.in=.out)

./tests/%.out: ./tests/%.in
    ./example $^ > $@

Also, you can get rid of the ./ prefix of your paths and may want to make the all and test_results rules phony . 此外,您还可以摆脱的./你的路的前缀,可能希望使alltest_results规则 The example dependency of your test_results rule is misplaced too (it won't update the .out files if example is outdated), it should be a dependency of the .out files themselves: 您的test_results规则的example依赖项也放错了位置(如果example已过时,它不会更新.out文件),它应该是.out文件本身的依赖项:

.PHONY: all
all: test_results

example: example.c
    gcc example.c -o example

TEST_INPUT = $(wildcard tests/*.in)
.PHONY: test_results
test_results: $(TEST_INPUT:.in=.out)

tests/%.out: tests/%.in example
    ./example $< > $@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM