Makefile template

# 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
 
# link options
STATIC_LIB_DIR_OPTIOS = $(addprefix -L, $(STATIC_LIB_DIR))
STATIC_LIBS_OPTIONS = $(addprefix -l, $(STATIC_LIBS))
SHARED_LIBS_OPTIONS = $(addprefix -l, $(SHARED_LIBS))
LINK_OPTIONS = $(STATIC_LIBS_OPTIONS) $(STATIC_LIB_DIR_OPTIOS) $(SHARED_LIBS_OPTIONS)
	
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)"

你可能感兴趣的:(linux系统编程,linux,c++,c语言)