Makefile的C++的一个模板,可用于根据不同源文件,生成多个可执行文件 。
CC = g++ DIR_INC = ./include DIR_SRC = ./src DIR_OBJ = ./obj DIR_BIN = ./bin # SRC = $(wildcard ${DIR_SRC}/*.cpp ) SRC_Server = $(wildcard ${DIR_SRC}/server.cpp ${DIR_SRC}/test1.cpp) SRC_Client = $(wildcard ${DIR_SRC}/client.cpp ${DIR_SRC}/test1.cpp) OBJ_server = $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC_Server} )) OBJ_client = $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC_Client} )) TARGET_Server = tcp_server TARGET_Client = tcp_client BIN_TARGET_Server = ${DIR_BIN}/${TARGET_Server} BIN_TARGET_Client = ${DIR_BIN}/${TARGET_Client} TARGET = ${DIR_BIN}/${TARGET_Server} ${DIR_BIN}/${TARGET_Client} all: $(TARGET) CFLAGS = -g -O2 -Wall -I${DIR_INC} ${DIR_OBJ}/%.o : ${SRC_Server} ${SRC_Client} @echo 开始编译文件 $(CC) ${CFLAGS} -c $? -mv *.o $(DIR_OBJ)/ @echo 编译文件完成 ${BIN_TARGET_Client} : ${OBJ_client} @echo 链接生成文件目标文件 ${CC} ${CFLAGS} ${OBJ_client} -o $@ ${BIN_TARGET_Server} : ${OBJ_server} @echo 链接生成文件目标文件 ${CC} ${CFLAGS} ${OBJ_server} -o $@ .PHONY : clean run clean: @echo 删除编译结果文件 -rm -f ${DIR_OBJ}/*.o ${BIN_TARGET_Server} ${BIN_TARGET_Client} run: @echo 执行文件 ${BIN_TARGET}