三個概念:
- 目標(target):指要幹什麼
- 依賴關係(dependency):指目標所依賴的其他目標
- 命令(command):指告訴 make 如何生成目標
規則的語法:
targets:prerequisite
command
....
第一個小程序:
all :
echo “hello world”
- 目標:all
- 先決條件:無
- 命令:echo “hello world”
運行結果(終端):
$ make
echo “hello world”
hello world
$ make all
echo “hello world”
hello world
第一種運行方式是 make 不帶任何參數,可以看到,最終在終端上輸出兩行:
第一行:make 打印出來的將要運行的命令
第二行:命令的運行結果
第二種運行方式是 make 帶參數 all,告訴 make 我們希望構建 all 目標,結果同上;
改動一:
all :
echo “hello world”
test :
echo “just for test”
- 目標:all 、test
- 先決條件:無
- 命令:echo “hello world” 、echo “just for test”
運行結果(終端):
$ make
echo “hello world”
hello world
$ make test
echo “just for test”
just for test
從運行結果中:
- 一個 Makefile 中可以定義多個目標
- 調用 make 命令時,得告訴它 我們希望構建的目標是什麼,當沒有指定目標時,make 將以默認目標作爲生成目標(Makefile 中第一個目標爲默認目標)
- 當 make 得倒目標後,先找到構建目標的對應規則,然後運行規則中的命令來達到構建目標的目的,一個規則可以包含多條命令
改動二:
all : test
@echo “hello world”
test :
@echo “just for test”
- 目標:all 、test
- 先決條件:test
- 命令:@echo “hello world” 、@echo “just for test”
運行結果(終端):
$ make
just for test
hello world
$ make test
just for test
從運行結果中:
- 當 make 不帶參數時,構建 all 目標之前 test 目標也被構建了,all 目標後面的 test 告訴 make ,all 目標依賴於 test 目標,這個依賴目標被稱爲(all 目標的)先決條件
- 一個規則是由目標、先決條件及命令組成。目標和先決條件之間就是依賴關係(dependency),這種依賴關係指明:在構建目標之前,必須保證先決條件先滿足。
參考文獻:《專業嵌入式軟件開發》李雲·著