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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM