【发布时间】:2011-06-08 11:01:49
【问题描述】:
我正在尝试使用递归 make 来消除诸如拥有多个 CFLAGS 变量之类的东西,每个目标一个变量。只有一级递归,这里没有发生疯狂的目录树遍历。我只想将我的目标文件转储到他们的目标特定文件夹中。
到目前为止,我已经想出了一些非常优雅的东西(编辑:好吧,它比我以前使用的单一 makefile 解决方案更优雅。重复太多了!)但不幸的是不起作用。
我认为通过在此处发布格式,很明显我正在尝试做什么。
# ./makefile
.PHONY: all clean
export CC = g++
export INCLUDE = -I ../include/
export SRC = Main.cpp Graphics.cpp Thread.cpp Net.cpp Otherstuff.cpp
export LINKEROPT = -lglew32 -lopengl32 -lsdl -lws2_32 -lglu32 -lmorelibraries
test:
$(MAKE) -f make.unittest
all:
$(MAKE) -f make.unittest
$(MAKE) -f make.debug
$(MAKE) -f make.release
clean:
-rm -rf build_* *.exe
# I am on windows so the targets are .exe's
这里是 make.debug 文件:
### sub-makefile for the debug build target. Contains target specific build settings.
DIRNAME = build_debug
TARGETNAME = program_debug
TARGETDESCR = DEBUG
CFLAGS = -Wextra -Wall -O0 -g3 -DDEBUG
### EVERYTHING AFTER THIS POINT IS A TEMPLATE
# my goal is to have as much of my makefile code being "reusable" as possible
# so that I can easily add targets.
OBJ = $(patsubst %.cpp,$(DIRNAME)/%.o,$(SRC))
DEPS = $(patsubst %.cpp,$(DIRNAME)/%.d,$(SRC))
-include $(DEPS)
# default behavior. Set up the build directory. Then build the debug target.
all: $(DIRNAME) $(TARGETNAME)
# this is the build dir
$(DIRNAME):
mkdir $(DIRNAME)
$(DIRNAME)/%.o: %.cpp
@echo -e "Compiling for $(TARGETDESCR): $< --> $@"
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
@echo -e "Generating dependencies: $< --> $(patsubst %.o,%.d,$@)"
$(CC) $(CFLAGS) $(INCLUDE) -MM -MT $@ -MF $(patsubst %.o,%.d,$@) $<
# I realize there is a way to generate the deps while compiling in one pass
# but I'll figure it out later
$(TARGETNAME): $(OBJ)
@echo -e "Linking $(TARGETDESCR): $@.exe"
$(CC) -L ../lib/win32/ -o $@ $(OBJ) $(LINKEROPT)
如您所见,我可以通过复制子 makefile 并稍微修改它,然后在主 makefile 中添加一些条目,非常快速地添加一个具有自己的 CFLAGS 集的新构建目标。
所以这里的问题是它无法识别文件中的更改。只有当我编辑 Main.cpp 时,它才会重新编译 build_debug/Main.o。我真的不确定我可以从哪里开始找出不正确的地方。
【问题讨论】:
-
main.d是否按预期包含在内?修改该文件并将$(info Including main.d...)添加到文件末尾,并确保在尝试重新构建时看到该消息。此外,请验证 .d 文件是否包含您希望它包含的所有内容以及所有路径是否正确。 -
要生成依赖项并一次性编译,请添加
-MD选项(gnu.org/software/gcc/news/dependencies.html)。