溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Makefile 遞歸遍歷目錄(含子目錄) 編譯動態(tài)庫

發(fā)布時間:2020-04-07 18:05:42 來源:網(wǎng)絡(luò) 閱讀:5866 作者:styyzx 欄目:開發(fā)技術(shù)

這里推薦一本書,Makefile手冊,寫的挺好的~

一、統(tǒng)一編譯所有子目錄的文件

直接上Makefile內(nèi)容了,

AR=ar
LD=ld
CC=gcc

CFLAGS = -O2 -Wall  -I./Test \
                -I./Test/Test1 \

#注:"\"后面不能有空格,并且該句寫完后最好有個換行

#注釋部分推薦在單獨的一行編寫

 

#動態(tài)庫需要 -fPIC  -shared

SOFLAGS = -O2 -fPIC -shared
TARGET = ./libSDK.so

#這里遞歸遍歷3級子目錄
DIRS := $(shell find . -maxdepth 3 -type d)

#這里循環(huán)遍歷目錄的cpp文件

FILES = $(foreach dir,$(DIRS),$(wildcard $(dir)/*.cpp))

#定義宏

DEF = -DLINUX -DENABLE_EPOLL

#替換文件后綴 ,也可以寫成 OBJS = $(FILES:%.cpp=%.o)

OBJS = $(patsubst %.cpp,%.o, $(FILES))
# Search paths
VPATH =

.cpp.o:
        $(CC) -c $(CFLAGS) $(DEF) -D__cplusplus $< -o $@
.c.o:
        $(CC) -c  $(CFLAGS)   $< -o $@


LIBS = -lpthread -lm -lstdc++
$(TARGET):$(OBJS)
        $(CXX) $(SOFLAGS) -o $@ $(OBJS) $(LIBS)
# $(OBJS):%.o:%.cpp
#       $(CXX) $(LINKFLAGS) $(CXXFLAGS) -o $(TARGET) $(OBJS) $(LIBS)
# $(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
        $(RM) $(TARGET)
        $(RM) $(OBJS)

 

 二、按目錄編譯

   參考:http://wdtxslqnn.blog.163.com/blog/static/4424648520132220651511/

要對子目錄執(zhí)行make,需要在當(dāng)前目錄制作一個Makefile,遍歷所有子目錄的Makefile,并運行相應(yīng)的make target. 以下是我用來編譯內(nèi)核模塊的一個Makefile

#

# Reference http://www.gnu.org/software/make/manual/make.html

#

需要排除的目錄

exclude_dirs := include bin

取得當(dāng)年子目錄深度為1的所有目錄名稱

dirs := $(shell find . -maxdepth 1 -type d)

dirs := $(basename $(patsubst ./%,%,$(dirs)))

dirs := $(filter-out $(exclude_dirs),$(dirs))

避免clean子目錄操作同名,加上_clean_前綴

SUBDIRS := $(dirs)

clean_dirs := $(addprefix _clean_,$(SUBDIRS) )

#

.PHONY: subdirs $(SUBDIRS) clean

執(zhí)行默認(rèn)make target

$(SUBDIRS):    

$(MAKE) -C $@

subdirs: $(SUBDIRS)

執(zhí)行clean

$(clean_dirs):    

$(MAKE) -C $(patsubst _clean_%,%,$@) clean

clean: $(clean_dirs)    

@find . /        

/( -name '*.[oas]' -o -name '*.ko' -o -name '.*.cmd' /

-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' /

-o -name '*.symtypes' /) /

-type f -print | xargs rm -f

 

三、編譯錯誤多的情況

當(dāng)我們執(zhí)行make之后,發(fā)現(xiàn)出現(xiàn)的錯誤和警告信息較多,無法從頭開始查看,這里提供一種shell的重定向輸出到文件的方法

make  2> make_print.txt 

數(shù)字    含義                   標(biāo)準(zhǔn)叫法
0      標(biāo)準(zhǔn)輸入        stdin = standard input
1      標(biāo)準(zhǔn)輸出        stdout = standard output
2      標(biāo)準(zhǔn)錯誤輸出   stderr = standard error

這樣終端就看不到錯誤輸出,而錯誤輸出都會保存在make_print.txt中

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI