我有一個使用自動依賴關係生成的C++程序的Makefile。 %.d配方來自GNU Make手冊。Makefile將自己添加爲目標
問題是,以某種方式將「Makefile」添加爲目標,然後隱式規則導致它假定它是可執行文件並使用我的src /%。cpp規則嘗試編譯src/Makefile.cpp。在查看調試信息時,這總是在包含執行之後發生。
No need to remake target `build/Sprite.d'.
Considering target file `Makefile'.
Looking for an implicit rule for `Makefile'.
...
Trying pattern rule with stem `Makefile'.
Trying implicit prerequisite `Makefile.o'.
Looking for a rule with intermediate file `Makefile.o'.
我知道include會導致給定的Makefiles在必要時被重建。它是否也嘗試重建當前的Makefile?如果是這樣,我該如何阻止它,如果沒有,那麼爲什麼將「Makefile」添加爲目標?
此外,包含被執行,導致.d文件被重新映射,即使我在命令行上指定目標(例如make clean
)。有什麼辦法可以阻止這種情況發生?
# $(call setsuffix,newsuffix,files)
# Replaces all the suffixes of the given list of files.
setsuffix = $(foreach file,$2,$(subst $(suffix $(file)),$1,$(file)))
# $(call twinfile,newdir,newsuffix,oldfile)
# Turns a path to one file into a path to a corresponding file in a different
# directory with a different suffix.
twinfile = $(addprefix $1,$(call setsuffix,$2,$(notdir $3)))
MAIN = main
SOURCE_DIR = src/
INCLUDE_DIR = include/
BUILD_DIR = build/
SOURCES = $(wildcard $(SOURCE_DIR)*.cpp)
OBJECTS = $(call twinfile,$(BUILD_DIR),.o,$(SOURCES))
DEPENDENCIES = $(call twinfile,$(BUILD_DIR),.d,$(SOURCES))
CXX = g++
LIBS = -lpng
CXXFLAGS = -I $(INCLUDE_DIR)
.PHONY: all
all: $(MAIN)
$(MAIN): $(OBJECTS)
$(CXX) $(LIBS) $^ -o $(MAIN)
include $(DEPENDENCIES)
%.o: $(BUILD_DIR)stamp
$(CXX) $(CXXFLAGS) -c $(call twinfile,$(SOURCE_DIR),.cpp,[email protected]) -o [email protected]
$(BUILD_DIR)%.d: $(SOURCE_DIR)%.cpp $(BUILD_DIR)stamp
@ echo Generate dependencies for $ [email protected]$$$$; \
sed 's,\($*\)\.o[ :]*,$(BUILD_DIR)\1.o [email protected] : ,g' [email protected]; \
rm -f [email protected]$$$$
$(BUILD_DIR)stamp:
mkdir -p $(BUILD_DIR)
touch [email protected]
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)
.PHONY: printvars
printvars:
@ echo $(SOURCES)
@ echo $(OBJECTS)
@ echo $(DEPENDENCIES)
其他建議:可以通過在`ifneq包裹`include`(,$(過濾得一乾二淨,$(MAKECMDGOALS))可以防止`.d`文件 「使清潔」 翻拍`HTTP ://www.gnu.org/software/make/manual/make.html#Goals – slowdog 2010-11-25 01:23:44