Makefile 模板(二)

Makefile 模板

模板介绍

  1. 支持存放中间文件的文件夹检查和创建
  2. 支持源文件位于不同文件夹内

模板

OBJOUT := ./out/
EXEOUT := ./out/
INCLUDE_DIR := ./include
SRC_DIR_TEST = ./src/test/
SRC_DIR_THREADPOLL = ./src/WorkThread/
LIB := -lpthread

SRC := $(wildcard $(SRC_DIR_TEST)*.cpp)
SRC += $(wildcard $(SRC_DIR_THREADPOLL)*.cpp)
OBJ := $(patsubst %.cpp,%.o, $(SRC))

TARGET := threadpool
CC := g++
CXXFLAG = -std=c++11
CFLAGS := -I$(INCLUDE_DIR)

all: PRE_CHECK $(TARGET) 
$(TARGET) : $(OBJ)
	@echo "SRC: " $(SRC)
	@echo "OBJ: " $(OBJ)
	@echo "TARGET: " $(TARGET)
	$(CC) $(CFLAGS) $(CXXFLAG) $^ -o $(TARGET) $(LIB)
	mv $^ $(OBJOUT)
	@echo "Compiling" $@ "end\n"

%.o : %.cpp
	echo "Compiling " $< "...."
	$(CC) $(CFLAGS) $(CXXFLAG) -c $^ -o $@ $(LIB)
	@echo "Compiling " $@ "end\n"

PRE_CHECK:
	@if [ ! -d $(OBJOUT) ];then \
		echo "Folder $(OBJOUT) does not exist, creating it..."; \
		mkdir $(OBJOUT); \
	else	\
		echo "Folder $(OBJOUT) already exists, nothing to do";	\
	fi

.PHONY: clean

clean:
	rm -rf $(TARGET) $(OBJOUT)* $(OBJOUT)

posted @ 2023-11-17 09:58  王清河  阅读(22)  评论(0编辑  收藏  举报