程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 一個比較通用的makefile

一個比較通用的makefile

編輯:C++入門知識

.PHONY : all clean

DEBUG := YES

CC := gcc
CXX := g++
LD := g++
AR := ar rc

HOME_PATH := $(realpath .)
SOURCE_PATH := ${HOME_PATH}/src
INC_PATH := -I${HOME_PATH}/src
DEP_PATH := ${HOME_PATH}/dep
SRCS := $(wildcard ${SOURCE_PATH}/*.c) $(wildcard ${SOURCE_PATH}/*.cpp)
LIB_PATH := -L${HOME_PATH}/lib

DEBUG_CFLAGS := -g -O0 -Wall -Wconversion -Wuninitialized -D_DEBUG -m64 -pg
RELEASE_CFLAGS := -O3 -m64
DEBUG_CXXFLAGS := ${DEBUG_CFLAGS}
RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}
DEBUG_LDFLAGS := --static
RELEASE_LDFLAGS := --static

ifeq (YES, ${DEBUG})
	CFLAGS := ${DEBUG_CFLAGS}
	CXXFLAGS := ${DEBUG_CXXFLAGS}
	LDFLAGS := ${DEBUG_LDFLAGS}
	BIN_PATH := ${HOME_PATH}/bin/debug
	OBJ_PATH := ${HOME_PATH}/obj/debug
else
	CFLAGS := ${RELEASE_CFLAGS}
	CXXFLAGS := ${RELEASE_CXXFLAGS}
	LDFLAGS := ${RELEASE_LDFLAGS}
	BIN_PATH := ${HOME_PATH}/bin/release
	OBJ_PATH := ${HOME_PATH}/obj/release
endif

BIN := ${BIN_PATH}/miku
LIBS := -Wl,-dn -lev -Wl,-dn -lglog -Wl,-dn -lpthread

OBJS := $(patsubst %,${OBJ_PATH}/%.o,$(basename ${notdir ${SRCS}}))
DEPS := $(patsubst %,${DEP_PATH}/%.d,$(basename ${notdir ${SRCS}}))

all : ${BIN_PATH} ${OBJ_PATH} ${DEP_PATH} ${BIN}

${BIN}: ${OBJS}
	${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIB_PATH} ${LIBS}

${BIN_PATH}:
	mkdir -p ${BIN_PATH}
${OBJ_PATH}:
	mkdir -p ${OBJ_PATH}
${DEP_PATH}:
	mkdir ${DEP_PATH}

${DEP_PATH}/%.d : ${SOURCE_PATH}/%.c
	@set -e;\
	rm -f $@;\
	${CC} -MM ${CFLAGS} ${INC_PATH} $< > [email protected];\
	sed 's,\($*\)\.o[ :]*,${OBJ_PATH}/\1.o $@ : ,g' < [email protected] > $@;\
	rm -f [email protected]

${DEP_PATH}/%.d : ${SOURCE_PATH}/%.cpp
	@set -e;\
	rm -f $@;\
	${CC} -MM ${CFLAGS} ${INC_PATH} $< > [email protected];\
	sed 's,\($*\)\.o[ :]*,${OBJ_PATH}/\1.o $@ : ,g' < [email protected] > $@;\
	rm -f [email protected]

-include ${DEPS}

${OBJ_PATH}/%.o : ${SOURCE_PATH}/%.c
	${CC} -c ${CFLAGS} ${INC_PATH} $< -o ${OBJ_PATH}/${basename ${notdir $@}}.o

${OBJ_PATH}/%.o : ${SOURCE_PATH}/%.cpp
	${CXX} -c ${CXXFLAGS} ${INC_PATH} $< -o ${OBJ_PATH}/${basename ${notdir $@}}.o

clean:
	-rm -f ${HOME_PATH}/dep/*.d
	-rm -f ${HOME_PATH}/obj/debug/*.o
	-rm -f ${HOME_PATH}/obj/release/*.o
	-rm -f ${HOME_PATH}/bin/debug/*
	-rm -f ${HOME_PATH}/bin/release/*

  基於 gcc -MM 實現的頭文件依賴推導。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved