如图为当前目录
需要编写一个makefile,将cpp文件编译成静态库,存到lib路径下,然后编译测试文件:
CXX = g++
AR = ar -rcs
LIB_DIR = lib
INCLUDE = -Iinclude
LIB_PATH = -L./lib
LIBS = -lHyLog
SRCS_CPP = $(wildcard ./HyLog.cpp)
SRCS = $(notdir $(SRCS_CPP))
OBJS = $(patsubst %.cpp, $(COMPILE_DIR)/%.o, $(SRCS))
DEP = $(patsubst %.cpp, $(COMPILE_DIR)/%.cpp.d, $(SRCS))
LIB_NAME=libHyLog.a
TARGET=$(LIB_DIR)/$(LIB_NAME)
all:$(TARGET)
$(TARGET): $(OBJS)
$(AR) $@ $^
$(COMPILE_DIR)/%.o: ./%.cpp $(COMPILE_DIR)/%.cpp.d
$(CXX) $(CFLAGS) $(INCLUDE) -c $< -o $@
$(COMPILE_DIR)/%.cpp.d: ./%.cpp
$(CXX) $(CFLAGS) $(INCLUDE) -MM -E -c $< -o $@
@sed 's/.*\.o/$(subst /,\/,$(dir $@))&/g' $@ > $@.tmp
@mv $@.tmp $@
.PHONY: clean
test:
$(CXX) testmain.cpp $(LIB_PATH) $(LIBS) $(INCLUDE) -o test
clean:
rm -rf $(LIB_DIR)/$(LIB_NAME)
rm -rf test;
参考文章:https://blog.youkuaiyun.com/tianyexing2008/article/details/124737771
END。