标签:discuz style blog color 文件 io
该Makefile适用于最后生成若干个可执行文件的小型C/C++工程,只要将该Makefile放在源码根目录下make,它会自动查找该目录下(包括子目录)的所有源码文件(支持.cpp .c .h格式)并自动生成正确的依赖关系,并且为了不污染源码文件夹,源码和编译过程中的中间文件会分开(Debug模式编译生成的中间文件在Debug目录下,Release模式在Release目录下)。
1 ########################################################### 2 # 3 # MAKEFILE FOR C/C++ PROJECT 4 # Author: Abbey (Abbeychenx@gmail.com) 5 # Date: 2014/07/07 6 # 7 ########################################################### 8 9 .PHONY: all clean 10 all: 11 12 # annotation when release version 13 DEBUG := y 14 TARGET_PROG := main 15 16 # project directory 17 DEBUG_DIR := ./Debug 18 RELEASE_DIR := ./Release 19 BIN_DIR := $(if $(DEBUG), $(DEBUG_DIR), $(RELEASE_DIR)) 20 21 # shell command 22 CC := gcc 23 CXX := g++ 24 RM := rm -rf 25 MKDIR := mkdir -p 26 SED := sed 27 MV := mv 28 29 # init sources & objects & depends 30 sources_all := $(shell find . -name "*.c" -o -name "*.cpp" -o -name "*.h") 31 sources_c := $(filter %.c, $(sources_all)) 32 sources_cpp := $(filter %.cpp, $(sources_all)) 33 sources_h := $(filter %.h, $(sources_all)) 34 objs := $(addprefix $(BIN_DIR)/,$(strip $(sources_cpp:.cpp=.o) $(sources_c:.c=.o))) 35 deps := $(addprefix $(BIN_DIR)/,$(strip $(sources_cpp:.cpp=.d) $(sources_c:.c=.d))) 36 37 # create directory 38 $(foreach dirname,$(sort $(dir $(sources_c) $(sources_cpp))),39 $(shell $(MKDIR) $(BIN_DIR)/$(dirname))) 40 41 # complie & link variable 42 CFLAGS := $(if $(DEBUG),-g -O, -O2) 43 CFLAGS += $(addprefix -I ,$(sort $(dir $(sources_h)))) 44 CXXFLAGS = $(CFLAGS) 45 LDFLAGS := 46 LOADLIBES += #-L/usr/include/mysql 47 LDLIBS += #-lpthread -lmysqlclient 48 49 # add vpath 50 vpath %.h $(sort $(dir $(sources_h))) 51 vpath %.c $(sort $(dir $(sources_c))) 52 vpath %.cpp $(sort $(dir $(sources_cpp))) 53 54 # generate depend files 55 # actually generate after object generated, beacasue it only used when next make) 56 ifneq "$(MAKECMDGOALS)" "clean" 57 sinclude $(deps) 58 endif 59 60 # make-depend(depend-file,source-file,object-file,cc) 61 define make-depend 62 $(RM) $1; 63 $4 $(CFLAGS) -MM $2 | 64 $(SED) ‘s,\($(notdir $3)\): ,$3: ,‘ > $1.tmp; 65 $(SED) -e ‘s/#.*//‘ 66 -e ‘s/^[^:]*: *//‘ 67 -e ‘s/ *\\$//‘ 68 -e ‘/^$/ d‘ 69 -e ‘s/$/ :/‘ < $1.tmp >> $1.tmp; 70 $(MV) $1.tmp $1; 71 endef 72 73 # rules to generate objects file 74 $(BIN_DIR)/%.o: %.c 75 @$(call make-depend,$(patsubst %.o,%.d,$@),[ DISCUZ_CODE_0 ]lt;,$@,$(CC)) 76 $(CC) $(CFLAGS) -o $@ -c [ DISCUZ_CODE_0 ]lt; 77 78 $(BIN_DIR)/%.o: %.cpp 79 @$(call make-depend,$(patsubst %.o,%.d,$@),[ DISCUZ_CODE_0 ]lt;,$@,$(CXX)) 80 $(CXX) $(CXXFLAGS) -o $@ -c [ DISCUZ_CODE_0 ]lt; 81 82 # add-target(target,objs,cc) 83 define add-target 84 REAL_TARGET += $(BIN_DIR)/$1 85 $(BIN_DIR)/$1: $2 86 $3 $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@ 87 endef 88 89 # call add-target 90 $(foreach targ,$(TARGET_PROG),$(eval $(call add-target,$(targ),$(objs),$(CXX)))) 91 92 all: $(REAL_TARGET) $(TARGET_LIBS) 93 94 clean: 95 $(RM) $(BIN_DIR)
一个高级的makefile文件,布布扣,bubuko.com
标签:discuz style blog color 文件 io
原文地址:http://www.cnblogs.com/ckLXHL/p/3864333.html