An advanced makefile

Source: Internet
Author: User

This makefile is applicable to small C/C ++ projects that generate several executable files. You only need to put the makefile in the source code root directory make, it automatically finds all source code files under the directory (including subdirectories) (supported. CPP. c. h format) and automatically generate the correct dependency, and in order not to pollute the source code folder, the source code and the intermediate files in the compilation process are separated (the intermediate files compiled in debug mode are under the DEBUG directory, the release mode is in the release directory ).

 1 ########################################################### 2 # 3 # MAKEFILE FOR C/C++ PROJECT 4 # Author: Abbey ([email protected]) 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,[email protected]),[        DISCUZ_CODE_0        ]lt;,[email protected],$(CC))76         $(CC) $(CFLAGS) -o [email protected] -c [        DISCUZ_CODE_0        ]lt;77 78 $(BIN_DIR)/%.o: %.cpp79         @$(call make-depend,$(patsubst %.o,%.d,[email protected]),[        DISCUZ_CODE_0        ]lt;,[email protected],$(CXX))80         $(CXX) $(CXXFLAGS) -o [email protected] -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 [email protected]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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.