我编写了一个受here启发的makefile,但在生成中间输出文件(.o文件)时无法使用调试标志运行make:
CFLAGS=-g -Wall -Wextra -DNDEBUG $(OPTFLAGS)
SOURCES=$(wildcard src/**/*.cpp src/*.cpp)
OBJECTS=$(patsubst %.cpp,%.o,$(SOURCES))
TEST_SRC=$(wildcard tests/*_tests.cpp)
TESTS=$(patsubst %.cpp,%,$(TEST_SRC))
TARGET=build/main.out
all: $(TARGET) tests
$(TARGET): build $(OBJECTS)
g++ $(OBJECTS) -o $@
build:
@mkdir -p build
@mkdir -p bin
.PHONY: tests
tests: CFLAGS += $(TARGET)
tests: $(TESTS)
sh ./tests/runtests.sh
clean:
rm -rf build $(OBJECTS) $(TESTS)
rm -f tests/tests.log
find . -name "*.gc*" -exec rm {} \;
运行make时,我得到以下输出:
g++ -c -o src/ob/abc.o src/ob/abc.cpp
g++ -c -o src/ob/main.o src/ob/main.cpp
g++ src/ob/abc.o src/ob/main.o -o build/main.out
sh ./tests/runtests.sh
Running unit tests:
我知道我没有在目标为$(CFLAGS)
的地方添加$(TARGET)
。如何添加CFLAGS
以便在将各个cpp文件编译为.o时,它会在CFLAGS
中定义的标志中发生?