使用 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))
用法:
优点:
以上是GNU Make 能否简化在目录结构中使用类似规则构建大量可执行文件的过程,从而允许从主目录和各个项目文件夹进行编译?的详细内容。更多信息请关注PHP中文网其他相关文章!