Makefile template

本文介绍了使用C++编译器进行项目构建的过程,包括设置编译标准、警告选项、调试选项,以及指定源文件、目标文件夹、库目录等,并提供了makefile规则用于编译和链接静态及共享库。
摘要由CSDN通过智能技术生成
# 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)"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值