在另一个目录下使用单元测试的Makefile。

问题描述 投票:0回答:1

假设我有一个这样的结构。

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 用于测试,另一个用于二进制?

非常感谢

c makefile gnu-make
1个回答
0
投票

只要在你的Makefile中添加一些构建测试的东西。 也许是这样的。

testsrcs := $(wildcard tests/test_*.cpp)
testbins := $(testsrcs:tests/%.cpp=%)

tests: $(testbins)

$(testbins): test_% : tests/test_%.cpp src/%.cpp
        $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
© www.soinside.com 2019 - 2024. All rights reserved.