Makefile 自动搜索 c 和 cpp 文件, 并生成 .a 静态库文件

最近 又弄linux 下的 .a 静态库编译, 于是想 做个 一劳永逸的Makefile, 经过一番折腾, 最后成功了

只需要 改两个 参数 就可以执行了(MYLIBVPATH), 代码 如下:

 

PWD=$(shell pwd)
#INCS=-I$(PWD)/include

# change to you project name
MYLIB = SQLiteCpp.a
# change to you project file dir
VPATH = include:src:sqlite3
# the obj dir
OBJDIR = obj

###########################################################################
# auth lzpong # source files SRCS
= $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.cpp)) SRCSC = $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.c)) # obj files OBJS_1 = $(addsuffix .o,$(basename $(SRCS))) OBJSC_1 = $(addsuffix .o,$(basename $(SRCSC))) OBJS = $(foreach n,$(notdir $(OBJS_1)),$(OBJDIR)/$(n)) OBJSC = $(foreach n,$(notdir $(OBJSC_1)),$(OBJDIR)/$(n)) # head files HEADERS = $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.h)) HEADERS += $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.hpp)) HEADERS += $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.inc)) CC = gcc CXX = g++ -std=c++11 INCS = $(patsubst %,-I%,$(subst :, ,$(VPATH))) CFLAGS += $(INCS) CXXFLAGS += $(INCS) LIBS += -lncurses -lesl -lpthread -lm LDFLAGS += -L. PICKY = -O2 #SOLINK = -shared -Xlinker -x #DEBUG = -g -ggdb #LIBEDIT_DIR = ./ all: $(MYLIB) $(MYLIB): $(OBJS) $(SRCS) $(OBJSC) $(SRCSC) $(HEADERS) ar rcs $(MYLIB) $(OBJS) $(OBJSC) ranlib $(MYLIB) # *.cpp files commpare $(OBJS): $(SRCS) $(HEADERS) @test -d $(OBJDIR) | mkdir -p $(OBJDIR) $(CXX) -c $(SRCS) $(INCS) mv *.o $(OBJDIR)/ # *.c file commpare $(OBJSC): $(SRCSC) $(HEADERS) @test -d $(OBJDIR) | mkdir -p $(OBJDIR) $(CC) -c $(SRCSC) $(INCS) mv *.o $(OBJDIR)/ clean: rm -rf $(OBJDIR) rm -f *.o *.a

 

posted @ 2018-06-20 20:22 _Ong 阅读(...) 评论(...) 编辑 收藏