首页 > 代码库 > 一个高级的makefile文件

一个高级的makefile文件

该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 clean10 all: 11 12 # annotation when release version13 DEBUG       := y14 TARGET_PROG := main15 16 # project directory        17 DEBUG_DIR   := ./Debug18 RELEASE_DIR := ./Release19 BIN_DIR     := $(if $(DEBUG), $(DEBUG_DIR), $(RELEASE_DIR))20 21 # shell command22 CC    := gcc23 CXX   := g++24 RM    := rm -rf25 MKDIR := mkdir -p26 SED   := sed27 MV    := mv28 29 # init sources & objects & depends30 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 directory38 $(foreach dirname,$(sort $(dir $(sources_c) $(sources_cpp))),39   $(shell $(MKDIR) $(BIN_DIR)/$(dirname)))40 41 # complie & link variable42 CFLAGS     := $(if $(DEBUG),-g -O, -O2)43 CFLAGS     += $(addprefix -I ,$(sort $(dir $(sources_h))))44 CXXFLAGS    = $(CFLAGS)45 LDFLAGS    := 46 LOADLIBES  += #-L/usr/include/mysql47 LDLIBS     += #-lpthread -lmysqlclient48 49 # add vpath50 vpath %.h $(sort $(dir $(sources_h)))51 vpath %.c $(sort $(dir $(sources_c)))52 vpath %.cpp $(sort $(dir $(sources_cpp)))53 54 # generate depend files55 # actually generate after object generated, beacasue it only used when next make)56 ifneq "$(MAKECMDGOALS)" "clean"57 sinclude $(deps)58 endif59 60 # make-depend(depend-file,source-file,object-file,cc)61 define make-depend62   $(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 endef72 73 # rules to generate objects file74 $(BIN_DIR)/%.o: %.c75         @$(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: %.cpp79         @$(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-target84   REAL_TARGET += $(BIN_DIR)/$185   $(BIN_DIR)/$1: $286         $3 $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@87 endef88 89 # call add-target90 $(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)