使用它在另一个目录中的对应部分进行单元测试的 Makefile
Makefile for unit tests using it's counter-part in another directory
假设我有这样的结构:
src/a/foo.cpp
src/b/bar.cpp
src/main.cpp
tests/a/test_foo.cpp
tests/b/test_bar.cpp
tests/test_all.cpp
我非常想要一个 Makefile 来构建源代码和构建测试。对于来源,我有这个设置:
src = $(shell find src -name "*.cpp")
objs = $(src:.c=.o)
bin = stuff
$(bin): $(objs)
$(CXX) -o $@ $^ $(LDFLAGS)
对于测试,我希望有一个类似的简单结构,其中每个测试都将构建它在 src
及其自身中的对应部分。
所以,对于 test_foo
,我想要一个这样的任务:
clang++ -o test_foo tests/test_foo.cpp src/foo.cpp
此外,您认为单独的 Makefile
用于测试和另一个用于二进制是个好主意吗?
非常感谢
只需将一些构建测试内容添加到您的 makefile 中即可。可能是这样的:
testsrcs := $(wildcard tests/test_*.cpp)
testbins := $(testsrcs:tests/%.cpp=%)
tests: $(testbins)
$(testbins): test_% : tests/test_%.cpp src/%.cpp
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
假设我有这样的结构:
src/a/foo.cpp
src/b/bar.cpp
src/main.cpp
tests/a/test_foo.cpp
tests/b/test_bar.cpp
tests/test_all.cpp
我非常想要一个 Makefile 来构建源代码和构建测试。对于来源,我有这个设置:
src = $(shell find src -name "*.cpp")
objs = $(src:.c=.o)
bin = stuff
$(bin): $(objs)
$(CXX) -o $@ $^ $(LDFLAGS)
对于测试,我希望有一个类似的简单结构,其中每个测试都将构建它在 src
及其自身中的对应部分。
所以,对于 test_foo
,我想要一个这样的任务:
clang++ -o test_foo tests/test_foo.cpp src/foo.cpp
此外,您认为单独的 Makefile
用于测试和另一个用于二进制是个好主意吗?
非常感谢
只需将一些构建测试内容添加到您的 makefile 中即可。可能是这样的:
testsrcs := $(wildcard tests/test_*.cpp)
testbins := $(testsrcs:tests/%.cpp=%)
tests: $(testbins)
$(testbins): test_% : tests/test_%.cpp src/%.cpp
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)