# compile
COMPILER = g++
COMPILE_STD_OPTIONS = -std=c++11
COMPILE_WARN_OPTIONS = -Wall
COMPILE_DEBUG_OPTIONS = -g -O0
# marrco
MARRCOS =
# directory
SRC_DIR = .
INCLUD_DIR = .
STATIC_LIB_DIR = lib
SHARED_LIB_DIR = lib
OUTPUT_DIR = output
# file
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
SRCS = $(notdir $(SRC_FILES))
OBJ_FILES = $(SRCS:.cpp=.o)
DEP_FILES = $(SRCS:.cpp=.d)
SRC_OBJ_FILES = $(SRC_FILES:.cpp=.o)
OBJ_OUTPUT_FILES = $(addprefix $(OUTPUT_DIR)/,$(OBJ_FILES))
DEP_OUTPUT_FILES = $(addprefix $(OUTPUT_DIR)/,$(DEP_FILES))
# static library
STATIC_LIBS =
# shared libraty
SHARED_LIBS =
# compile options
MARRCO_OPTIONS = $(addprefix -D, $(MARRCOS))
INCLUDE_DIR_OPTIONS = $(addprefix -I, $(INCLUD_DIR))
COMPILE_OPTIONS = $(COMPILE_STD_OPTIONS) \
$(COMPILE_WARN_OPTIONS) \
$(COMPILE_DEBUG_OPTIONS) \
$(MARRCO_OPTIONS) \
$(INCLUDE_DIR_OPTIONS) \
-ffunction-sections \
-fdata-sections \
-fPIC
# link options
STATIC_LIB_DIR_OPTINOS = $(addprefix -L, $(STATIC_LIB_DIR))
SHARED_LIB_DIR_OPTINOS = $(addprefix -L, $(SHARED_LIB_DIR))
STATIC_LIBS_OPTIONS = $(addprefix -l, $(STATIC_LIBS))
SHARED_LIBS_OPTIONS = $(addprefix -l, $(SHARED_LIBS))
LINK_OPTIONS = $(STATIC_LIB_DIR_OPTINOS) $(SHARED_LIB_DIR_OPTINOS ) $(STATIC_LIBS_OPTIONS) $(SHARED_LIBS_OPTIONS) -Wl,--gc-sections
TARGET = $(OUTPUT_DIR)/main.exe
# specify targets
.PHONY:mkdir clean vars
# default target
default:mkdir $(TARGET)
$(TARGET): $(OBJ_OUTPUT_FILES)
@echo "link"
$(COMPILER) $^ -o $@ $(LINK_OPTIONS)
@echo "all finished"
$(OUTPUT_DIR)/%.o : $(SRC_DIR)/%.cpp $(OUTPUT_DIR)/%.d
@echo "compile $@"
@$(COMPILER) $(COMPILE_OPTIONS) -c $< -o $@
$(OUTPUT_DIR)/%.d : $(SRC_DIR)/%.cpp
@(set -e; \
rm -f $@; \
$(COMPILER) $(COMPILE_OPTIONS) -MM $< > $@.$$$$; \
sed 's,.*\.o:,$*.o $@: ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$)
-include $(DEP_OUTPUT_FILES)
mkdir:
ifneq ($(OUTPUT_DIR), $(wildcard $(OUTPUT_DIR)))
@echo "mkdir"
@mkdir $(OUTPUT_DIR)
endif
clean:
rm -rf $(OUTPUT_DIR)
vars:
@echo "SRC_FILES:$(SRC_FILES)"
@echo "SRCS:$(SRCS)"
@echo "OBJ_FILES:$(OBJ_FILES)"
@echo "DEP_FILES:$(DEP_FILES)"
@echo "SRC_OBJ_FILES:$(SRC_OBJ_FILES)"
@echo "OBJ_OUTPUT_FILES:$(OBJ_OUTPUT_FILES)"
@echo "DEP_OUTPUT_FILES:$(DEP_OUTPUT_FILES)"
Makefile template
于 2023-10-20 20:03:06 首次发布
本文介绍了使用C++编译器进行项目构建的过程,包括设置编译标准、警告选项、调试选项,以及指定源文件、目标文件夹、库目录等,并提供了makefile规则用于编译和链接静态及共享库。
摘要由CSDN通过智能技术生成