1、編譯所有子目錄的makefile
經常有人需要順序編譯一個一個的模塊,最後才連接生成可執行程序,但是如果一個模塊一個模塊地執行make,比較馬法,下面是一個經過驗證的makefile;
可以根據自己的需要修改SUBDIRS宏的值,設定自己需要編譯的目錄的順序.就可以了.
#編譯所有子目錄
#SUBDIRS=`ls -d */ | grep -v 'bin' | grep -v 'lib' | grep -v 'include'`
#編譯指定子目錄
SUBDIRS=dir1 dir2 dir3
define make_subdir
@for subdir in $(SUBDIRS) ; do \
( cd $$subdir && make $1) \
done;
endef
all:
$(call make_subdir , all)
install :
$(call make_subdir , install)
debug:
$(call make_subdir , debug)
clean:
$(call make_subdir , clean)
2、gcc -I -L -l 的區別
gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld
上面這句表示在編譯hello.c時-I /home/hello/include表示將/home/hello/include目錄作爲第一個尋找頭文件的目錄,
尋找的順序是:/home/hello/include-->/usr/include-->/usr/local/include
-L /home/hello/lib表示將/home/hello/lib目錄作爲第一個尋找庫文件的目錄,
尋找的順序是:/home/hello/lib-->/lib-->/usr/lib-->/usr/local/lib
-lworld表示在上面的lib的路徑中尋找libworld.so動態庫文件(如果gcc編譯選項中加入了“-static”表示尋找libworld.a靜態庫文件)
3、Makefile中的 符號 $@, $^, $<
$@ 表示目標文件
$^ 表示所有的依賴文件
$< 表示第一個依賴文件
$? 表示比目標還要新的依賴文件列表
4、wildcard、notdir、patsubst的意思
wildcard : 擴展通配符
notdir : 去除路徑
patsubst :替換通配符