使用GNU Make 構建具有類似規則的多個可執行文件
問題:
GNU Make 可以嗎方便在目錄結構中建立具有相似規則的多個可執行文件,從而能夠從主目錄和各個專案目錄進行編譯?
答案:
是的,GNU Make 可以處理這項任務採用簡潔有效的方法。以下是兩個實現所需功能的makefile:
project.mk:
all : % : forward_ # build any target by forwarding to the main makefile $(MAKE) -C .. project_dirs=$(notdir ${CURDIR}) $@ .PHONY : forward_
Makefile:
# one directory per project, one executable per directory project_dirs := $(shell find * -maxdepth 0 -type d ) # executables are named after its directory and go into the same directory exes := $(foreach dir,${project_dirs},${dir}/${dir}) all : ${exes} # the rules .SECONDEXPANSION: objects = $(patsubst %.cpp,%.o,$(wildcard $(dir )*.cpp)) # link ${exes} : % : $$(call objects,$$*) Makefile g++ -o $@ $(filter-out Makefile,$^) ${LDFLAGS} ${LDLIBS} # compile .o and generate dependencies %.o : %.cpp Makefile g++ -c -o $@ -Wall -Wextra ${CPPFLAGS} ${CXXFLAGS} -MD -MP -MF ${@:.o=.d} $< .PHONY: clean clean : rm -f $(foreach exe,${exes},$(call objects,${exe})) $(foreach dir,${project_dirs},$(wildcard ${dir}/*.d)) ${exes} # include auto-generated dependency files -include $(foreach dir,${project_dirs},$(wildcard ${dir}/*.d))
Makefile:
要建立特定項目,請導航至項目目錄並執行 make。
要建立所有項目,請從主目錄 (all_lessons) 執行 make。以上是GNU Make 能否簡化在目錄結構中使用類似規則建構大量可執行檔的過程,從而允許從主目錄和各個專案資料夾進行編譯?的詳細內容。更多資訊請關注PHP中文網其他相關文章!