user972014
user972014

Reputation: 3856

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. Each file under ./tests/%.in is a test case.

And I want from each of them to create ./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.

Upvotes: 0

Views: 263

Answers (1)

Dettorer
Dettorer

Reputation: 1336

% 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:

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. 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:

.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 $< > $@

Upvotes: 1

Related Questions