0

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.

austin
  • 5,816
  • 2
  • 32
  • 40
user972014
  • 3,296
  • 6
  • 49
  • 89
  • Can you give the exact errors you get? (Along with how you use that makefile). – Dettorer Nov 14 '14 at 15:50
  • No rule to make target `tests/%.out', needed by `test_results' And in general, it seems like it's not the best way – user972014 Nov 14 '14 at 15:53
  • I don't think there is a problem with your way of doing that (apart from the few errors). If indeed you only have one binary file, a bunch of test files in a folder and want one output file per test input, it seems good to me. – Dettorer Nov 14 '14 at 16:10

1 Answers1

1

% 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 $< > $@
Community
  • 1
  • 1
Dettorer
  • 1,247
  • 1
  • 8
  • 26