说我有一个简单的
Makefile
就像这样:
CXXFLAGS+=-O3 -Wall -pednatic -MMD -Isrc
SRCS=$(shell find src -name '*.cpp')
OBJS=${SRCS:.cpp=.o}
TEST_SRCS=$(shell find test -name '*.cpp')
TEST_OBJS=${TEST_SRCS:.cpp=.o}
all: bin/product
bin/product: $(OBJS)
$(CXX) ($CXX_FLAGS) -o $@ $^
test: test/runner
./test/runner
test/runner: $(TEST_OBJS) $(OBJS)
$(CXX) ($CXX_FLAGS) -o $@ $^
-include ${OBJS:.o=.d}
-include ${TEST_OBJS:.o=.d}
.PHONY: all test
这里要注意的重要一点是,我有两个独立的二进制目标(一个产品的对象来自
src/
和一个测试运行器
SRC公司/
和
test/
)中。我想换个
CXXFLAGS
只是为了测试中的目标(
test/runner
,但同时
test/foo_test.o
)但不在
src
是的。当然,我可以定义自己的规则来覆盖隐式规则(注意
-Itest
,这是我希望如何编译测试的众多差异之一):
test/%.o: test/%.cpp
$(CXX) $(CXXFLAGS) -Itest -c -o $@ $^
但这有一个不幸的结果,即覆盖了
*.d
文件创建者
-MMD
是的。例如
test/foo_test.d
以下内容:
test/foo_test.o: test/foo_test.cpp src/foo.o
是否有方法重写中对象的隐式规则
测试/
不失去依赖?