I have a bunch of
.c test source file in directory
tests/. Now I want to compile and link them respectively and output the executable
tests/. So I wrote a makefile, which didn't work.
# ... TestDir := tests TestSourceFile := $(shell sh -c "ls tests/*.c") TestTargetFile := $(subst .c,.out,$(TestSourceFile)) TestFrame := testframe.o TestNeededObjectFile := $(TestFrame) \ + util.o \ + tokennames.o \ + lex.yy.o \ + hex.o \ .PHONY: test-% test-%: $(TestDir)/%.out $^ .PHONY: test test: $(TestTargetFile) @for t in $(TestTargetFile); do \ $$t ; \ done $(TestDir)/%.out: $(TestDir)/%.o $(TestNeededObjectFile) gcc -o $@ $^ %.o : %.c gcc -c $(CFLAGS) $^ clean: rm -rf *.o lextest *.yy.? *.tab.? *.output $(TokenNameFile) \ $(TestDir)/*.out
When I run
add.c is in
tests/), I am expecting to see
tests/ but instead there is an error:
> make test-add make: *** No rule to make target 'tests/add.out', needed by 'test-add'. Stop.
I want to know how to write this makefile correctly and why this makefile is wrong.