我正在嘗試使用遞歸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): $< --> [email protected]"
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o [email protected]
@echo -e "Generating dependencies: $< --> $(patsubst %.o,%.d,[email protected])"
$(CC) $(CFLAGS) $(INCLUDE) -MM -MT [email protected] -MF $(patsubst %.o,%.d,[email protected]) $<
# 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): [email protected]"
$(CC) -L ../lib/win32/ -o [email protected] $(OBJ) $(LINKEROPT)
正如你所看到的,我可以很快用自己的一套CFLAGS
通過複製在子Makefile中添加一個新的構建目標和稍微修改它,然後在主makefile中添加一些條目。
所以這裏的問題是,它無法識別文件中的更改。只有當我編輯Main.cpp時,它纔會重新編譯build_debug/Main.o。我真的不確定我能從哪裏開始弄清楚什麼是不正確的。
'main.d'是否按預期包含在內?修改該文件並在文件末尾添加'$(info included main.d ...)',並確保在嘗試重新構建時看到該消息。另外,請驗證.d文件包含您希望它包含的所有內容,並且所有路徑都是正確的。 – bta 2011-06-07 17:20:47
要生成依賴關係並在一次編譯中添加'-MD'選項(http://www.gnu.org/software/gcc/news/dependencies.html)。 – bta 2011-06-07 17:30:31