我正在爲一個C++項目製作一個makefile,這個項目需要支持一些配置,即調試,發佈以及將來可能還有一些定製的配置。Makefile模式規則:循環makefile.o < - makefile依賴項被刪除
目前,我生成的.o文件的命名約定是$(SOURCE_FULLPATH).$(CONFIGURATION).o
。例如,ABC.cpp
以調試模式生成ABC.cpp.debug.o
。
現在我想編寫用於以獨立於配置的方式生成這些對象文件的模式規則。我所做的是:從每個XX.o
文件名,我剝去.debug
或.release
後綴XX
,並使用XX
的剩餘部分作爲源文件名。
%.o: $$(basename %)
$(CC) $(CC_FLAGS) $(INCLUDE_FOLDERS) -c -o [email protected] $<
有了這一招,我可以正確生成可執行,但我從make得到一個警告消息:
make: Circular makefile.o <- makefile dependency dropped.
我百思不得其解,因爲我不列出makefile
或makefile.o
作爲目標或依賴於我的makefile中的任何地方。我對SO進行了搜索,但大多數關於循環依賴的問題都在特定的用戶源文件上,而不是makefile本身。 任何人都可以幫助我理解導致循環依賴的原因,以及如何擺脫此警告消息?
下面列出了一個可以重現此問題的示例makefile。
.SECONDEXPANSION:
PROJECT := helloworld
CC := clang++
BUILD_FOLDER := Build
OBJ_FILE_SUFFIX := .o
# Source
CPP_FILES :=\
Source/hello.cpp \
Source/mysqrt.cpp \
INCLUDE_FOLDERS := \
-IInclude
# MMD outputs the dependency files (".d" files). These files will be used by
# this makefile to allow for dependency checking on .h files.
CC_FLAGS += -MMD
EXISTING_OBJ_FILES = $(wildcard $(addsuffix *.o, $(basename $(CPP_FILES))))
##--------------------
## Targets definition
##--------------------
.PHONY:default
default: all
.PHONY:all
all: debug release
.PHONY:debug release
# Add a 'debug'/'release' suffix to the name of the object file
# e.g. hello.cpp -> hello.cpp.debug.o
debug release: OBJ_FILES=$(addsuffix [email protected]$(OBJ_FILE_SUFFIX), $(CPP_FILES))
debug release: $${OBJ_FILES} # Use Secondary Expansion to get the obj names
$(CC) $^ -o $(BUILD_FOLDER)/$(PROJECT)[email protected]
# Strip configuration name from the end of the object file name
%.o: $$(basename %)
$(CC) $(CC_FLAGS) $(INCLUDE_FOLDERS) -c -o [email protected] $<
## clean: remove executable, all object files, and all dependency files
.PHONY:clean
clean:
-rm -f $(BUILD_FOLDER)/$(PROJECT) $(EXISTING_OBJ_FILES) $(EXISTING_OBJ_FILES:.o=.d)
# Include the dependent files so that in later builds, modified .h files
# will cause all .cpp dependent on them to rebuild
-include $(OBJ_FILES:.o=.d)
的文件夾結構是
makefile
Source
- hello.cpp
- mysqrt.cpp
Include
- mysqrt.h
的make debug
的全部輸出是
make: Circular makefile.o <- makefile dependency dropped.
clang++ -MMD -IInclude -c -o Source/hello.cpp.debug.o Source/hello.cpp
clang++ -MMD -IInclude -c -o Source/mysqrt.cpp.debug.o Source/mysqrt.cpp
clang++ Source/hello.cpp.debug.o Source/mysqrt.cpp.debug.o -o Build/helloworld_debug
一切是除了第一行良好。
如果任何人在我的makefile中有任何不好的做法(我仍然是makefile中的新手),任何人都可以指向我,我也會很感激。先謝謝你!