Makefile的模板
说明: 0) Makefile文件放置于项目的根目录。
- 项目源代码文件放置于src目录下面,可以在src下面建立子目录分别放置每个模块的源代码文件。Makefile会自动搜索并生成依赖以及编译这些源文件。 项目的主启动文件(含有main()函数)放置于src/main目录下面。 如果有其它的含main()的启动文件,请放置于src/xxx目录下面,例如测试目录: src/test_xxx。使用时,拷贝一份Makfile重命名,修改src_module的值,然后用make -f xxx.mk 来编译需要的文件。
- 编译的自动依赖文件放置于 bin/depend目录下面。
- 调试版本(debug)编译的中间结果放置于 bin/debug, 发布版本(release)放置于bin/release
- 清除分为 clean 和 cleanall
- 使用时只需要修改 cc ,link 和 src_file_type的值就可以了。
PROGRAM = hello CC = gcc CFLAGS = -Wall LINK = gcc #LDFLAGS =
SRC_FILE_TYPE = c
#==================================================
module define
SRC_MODULES = main conf util
#================================================= #directory define SRC_DIR := src BUILD_DIR := ./bin DEPS_DIR := $(BUILD_DIR)/depend
#================================================== #program build mode is: debug/release BUILD_MODE := debug #BUILD_MODE := release
#Auto dependent generated? (YES/NO) #AUTO_DEPENDENT = YES AUTO_DEPENDENT = NO
#================================================== #add the lib you used here #LIBS := -lLib1 -lLib2 -lLib3 #LIBS := -lpthread LIBS := -lpthread -lxml2 #LIB_PATH := -Lpath1 -Lpath2 -Lpath3 LIB_PATH := INCLUDE_PATH := -I $(SRC_DIR) -I ./src/include -I /usr/local/include/libxml2 #INCLUDE_PATH := -I/usr/lib/XXX/include
#================================================= #get out put directory ifeq ($(BUILD_MODE),debug) OUTPUT_DIR := $(BUILD_DIR)/debug CFLAGS += -g -O0
else ifeq ($(BUILD_MODE),release) OUTPUT_DIR := $(BUILD_DIR)/release CFLAGS += -O3
else $(error "BUILD_MODE error!(release/debug)") endif endif
##set target target = $(PROGRAM) target_test = $(PROGRAM)_test
#add path #VPATH = $(shell find $(SRC_DIR) -type d) #VPATH = $(SRC_DIR) VPATH += $(addprefix $(SRC_DIR)/, $(SRC_MODULES))
#FIND_SRC_FILES = $(shell find $(SRC_DIR) -maxdepth 1 -name "*.$(SRC_FILE_TYPE)") #SRC_FILES = $(notdir $(FIND_SRC_FILES) )
FIND_SRC_FILES = $(foreach dir, $(VPATH), $(wildcard $(dir)/*.$(SRC_FILE_TYPE))) SRC_FILES = $(notdir $(FIND_SRC_FILES) )
OBJ_FILES = $(SRC_FILES:.$(SRC_FILE_TYPE)=.o)
#auto depend file? ifeq ($(AUTO_DEPENDENT) ,YES) DEP_FILES = $(SRC_FILES:.$(SRC_FILE_TYPE)=.d) endif
OUTPUT_OBJS = $(addprefix $(OUTPUT_DIR)/,$(OBJ_FILES)) OUTPUT_DEPS = $(addprefix $(DEPS_DIR)/,$(DEP_FILES))
#INCLUDE_PATH += $(addprefix -I , $(VPATH))
##====== start of init shell ======##
exec init shell command
$(shell mkdir -p "$(OUTPUT_DIR)") $(shell mkdir -p "$(DEPS_DIR)") #$(foreach dir, $(SRC_MODULES),$(shell mkdir -p "$(DEPS_DIR)/$(dir)"))
##======= end of init ==============##
#add target .PHONY:all all: $(BUILD_DIR)/$(target)
.PHONY: help help: @echo "make's target is: " @echo " make all -- make all and generate target!" @echo " make run -- make all and run the target! " @echo " make clean -- make clean the object files!" @echo " make cleanall -- make clean all ,clean everything!" @echo "Test:" @echo " make test -- make test target!" @echo " make testrun -- make test target and run it!" @echo " make help -- this help!" @echo " " @echo " "
#link all objs and libs $(BUILD_DIR)/$(target): $(OUTPUT_DEPS) $(OUTPUT_OBJS) @echo "|------------------------------------------------|" @echo $(SRC_FILES) $(LINK) $(LIB_PATH) $(OUTPUT_OBJS) $(CFLAGS) -o $@ $(LIBS) @echo "Make '$(BUILD_DIR)/$(target)' finished!"
#compile source files into object files $(OUTPUT_DIR)/%.o: %.$(SRC_FILE_TYPE) $(CC) $(CFLAGS) $(INCLUDE_PATH) -I $(basename $<) -c $< -o $@
$(DEPS_DIR)/%.d: %.$(SRC_FILE_TYPE) @echo -n "$(OUTPUT_DIR)/" > $@ $(CC) -MM $(INCLUDE_PATH) $< >> $@
.PHONY:test $(BUILD_DIR)/$(target_test): $(OUTPUT_DEPS) $(OUTPUT_OBJS) @echo "--------->make test!" $(LINK) $(LIB_PATH) $(OUTPUT_OBJS) $(CFLAGS) -o $@ $(LIBS)
.PHONY:test test: $(BUILD_DIR)/$(target_test)
.PHONY:testrun testrun: test $(BUILD_DIR)/$(target_test)
.PHONY:run run: $(BUILD_DIR)/$(target)
.PHONY:clean clean: -rm -rf $(OUTPUT_DIR) -rm -f $(BUILD_DIR)/$(target) -rm -f $(BUILD_DIR)/$(target_test)
.PHONY:cleanall cleanall:clean -rm -rf $(DEPS_DIR)
#dep files -include $(OUTPUT_DEPS)