说明:
0) Makefile文件放置于项目的根目录。
1) 项目源代码文件放置于src目录下面,可以在src下面建立子目录分别放置每个模块的源代码文件。Makefile会自动搜索并生成依赖以及编译这些源文件。
2) 编译的自动依赖文件放置于 bin/depend目录下面。
3) 调试版本(debug)编译的中间结果放置于 bin/debug, 发布版本(release)放置于bin/release
4) 清除分为 clean 和 cleanall
5) 使用时只需要修改 cc ,link 和 src_file_type的值就可以了。
PROGRAM = hello
CC = g++
CFLAGS = -Wall
LINK = g++
LDFLAGS =
SRC_FILE_TYPE = cxx
SRC_DIR := src
BUILD_DIR := ./bin
DEPS_DIR := $(BUILD_DIR)/depend
#program build mode is: debug/release
BUILD_MODE := debug
#BUILD_MODE := release
#add the lib you used here
#LIBS := -lLib1 -lLib2 -lLib3
#LIBS := -lpthread
LIBS :=
#LIB_PATH := -Lpath1 -Lpath2 -Lpath3
LIB_PATH :=
INCLUDE_PATH :=
#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)
#add path
VPATH = $(shell find $(SRC_DIR) -type d)
FIND_SRC_FILES = $(shell find $(SRC_DIR) -name "*.$(SRC_FILE_TYPE)")
SRC_FILES = $(notdir $(FIND_SRC_FILES) )
OBJ_FILES = $(SRC_FILES:.$(SRC_FILE_TYPE)=.o)
DEP_FILES = $(SRC_FILES:.$(SRC_FILE_TYPE)=.d)
OUTPUT_OBJS = $(addprefix $(OUTPUT_DIR)/,$(OBJ_FILES))
OUTPUT_DEPS = $(addprefix $(DEPS_DIR)/,$(DEP_FILES))
##====== start of init shell ======##
## exec init shell command
$(shell mkdir -p "$(OUTPUT_DIR)")
$(shell mkdir -p "$(DEPS_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 " make help -- this help!"
@echo " "
@echo " "
##
#link all objs and libs
$(BUILD_DIR)/$(target): $(OUTPUT_DEPS) $(OUTPUT_OBJS)
@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) -c $< -o $@
@echo "compiling '$<' finished!"
#
$(DEPS_DIR)/%.d: %.$(SRC_FILE_TYPE)
@echo -n "$(OUTPUT_DIR)/" > $@
$(CC) -MM $(INCLUDE_PATH) $< >> $@
#dep files
-include $(OUTPUT_DEPS)
.PHONY:run
run: all
$(BUILD_DIR)/$(target)
.PHONY:clean
clean:
rm -rf $(OUTPUT_DIR)
rm -f $(BUILD_DIR)/$(target)
.PHONY:cleanall
cleanall: clean
rm -rf $(DEPS_DIR)
- Makefile.rar (1 KB)
- 下载次数: 2