1、概述1.1、Makefile檔案
make在執行時,需要命名一個Makefile檔案。Makefile檔案描述了整個工程的編譯和串連等規則。
make命令預設在目前的目錄下尋找makefile或者Makefile檔案,當自訂檔案時用 :make -f 檔案名稱
1.2、用法1.2.1、規則
目標列表:依賴列表
<Tab>
命令列表
如:
hello.o:hello.c
gcc -c hello.c
命令必須用tab鍵開始
1.2.2、目標
第一條規則中確定最終目標
1.2.3、偽目標
沒有任何依賴,只有執行動作的目標(phony targets)
用法:
.PHONY:目標名
如:clean目標聲明為偽目標,然後執行動作
.PHONY:clean
clean:
rm -f hello hello1.o hello2.o
1.2.4、變數
為了動態修改參數的變化依賴,方便管理
1.2.4.1、系統預設變數$...
$^ : 代表所有依賴檔案
$@ : 代表目標
$< :代表第一個依賴檔案
$? :代表所有已修改的依賴檔案
如:
hello:hello1.o hello2.0
gcc hello1.o hello2.o -o hello
==>
hello:hello1.o hello2.o
gcc $^ -o $@
1.2.4.2、普通變數=
變數名 := 文本
變數名 = 文本
變數名 += 文本
1.2.4.3、引用變數$
$變數名
如:
C=gcc
$C
1.2.5、雜項#、@1.2.5.1、#
#注釋
1.2.5.2、@
@:取消回顯(螢幕不列印編譯目標訊息)
1.2.6、執行個體
hello: hello1.o hello2.o gcc hello1.o hello2.o -o hellohello1.o:hello1.c gcc -c hello1.chello2.o:hello2.c gcc - c hello2.c.PHONY:cleanclean: rm -f hello hello1.o hello2.o