My directory structure is as followed:
tests\
test1.c
test2.c
test3.c
build\
test1
test2
test3
Concretely, I want to build test1.c
, test2.c
and test3.c
and output the executables in tests\build
.
To achieve that I have the following Makefile snippet:
TSTS = $(wildcard $(TEST_DIR)/*.c)
EXE = $(patsubst $(TEST_DIR)/%.c,$(TEST_BUILD)/%, $(TSTS))
TEST_DIR = tests
TEST_BUILD = tests/build
$(EXE): $(TSTS)
$(CC) $< $(CFLAGS_OUT) $@ (CFLAGS_LIBS_FLAG)$(LIB_NAME)
Which gives me something like that:
gcc -L/home/projects/loc/build -Iinclude tests/test1.c -o tests/build/test1 -lsalmalloc
gcc -L/home/projects/loc/build -Iinclude tests/test1.c -o tests/build/test2 -lsalmalloc
gcc -L/home/projects/loc/build -Iinclude tests/test1.c -o tests/build/test3 -lsalmalloc
The output paths are correct, but the source files are all tests/test1.c
!
Ignore the include
and other lib
related files because I have removed the flags to make it simple. Essentially, make
is building test cases from a single file, that is, for a single source file, test1.c
, three builds are being generated with different names, instead of each test have its own build. I have looked at here, here, here and here but cannot find the problem.
How can I fix this?