Makefile學習
千里之行,始於足下
隨筆 - 243文章 - 0評論 - 308Makefile 使用總結
1. Makefile 簡介
Makefile 是和 make 命令一起配合使用的.
很多大型專案的編譯都是通過 Makefile 來組織的, 如果沒有 Makefile, 那很多專案中各種庫和程式碼之間的依賴關係不知會多複雜.
Makefile的組織流程的能力如此之強, 不僅可以用來編譯專案, 還可以用來組織我們平時的一些日常操作. 這個需要大家發揮自己的想象力.
本篇部落格是基於{精華} 跟我一起寫 Makefile而整理的, 有些刪減, 追加了一些示例.
非常感謝 gunguymadman_cu 提供如此詳盡的Makefile介紹, 這正是我一直尋找的Makefile中文文件.
1.1 Makefile 主要的 5個部分 (顯示規則, 隱晦規則, 變數定義, 檔案指示, 註釋)
Makefile基本格式如下:
target ... : prerequisites ... command ... ...
其中,
- target - 目標檔案, 可以是 Object File, 也可以是可執行檔案
- prerequisites - 生成 target 所需要的檔案或者目標
- command - make需要執行的命令 (任意的shell命令), Makefile中的命令必須以 [tab] 開頭
- 顯示規則 :: 說明如何生成一個或多個目標檔案(包括 生成的檔案, 檔案的依賴檔案, 生成的命令)
- 隱晦規則 :: make的自動推導功能所執行的規則
- 變數定義 :: Makefile中定義的變數
- 檔案指示 :: Makefile中引用其他Makefile; 指定Makefile中有效部分; 定義一個多行命令
- 註釋 :: Makefile只有行註釋 "#", 如果要使用或者輸出"#"字元, 需要進行轉義, "\#"
1.2 GNU make 的工作方式
- 讀入主Makefile (主Makefile中可以引用其他Makefile)
- 讀入被include的其他Makefile
- 初始化檔案中的變數
- 推導隱晦規則, 並分析所有規則
- 為所有的目標檔案建立依賴關係鏈
- 根據依賴關係, 決定哪些目標要重新生成
- 執行生成命令
2. Makefile 初級語法
2.1 Makefile 規則
2.1.1 規則語法
規則主要有2部分: 依賴關係 和 生成目標的方法.
語法有以下2種:
target ... : prerequisites ... command ...
或者
target ... : prerequisites ; command command ...
*注*command太長, 可以用 "\" 作為換行符
2.1.2 規則中的萬用字元
- * :: 表示任意一個或多個字元
- ? :: 表示任意一個字元
- [...] :: ex. [abcd] 表示a,b,c,d中任意一個字元, [^abcd]表示除a,b,c,d以外的字元, [0-9]表示 0~9中任意一個數字
- ~ :: 表示使用者的home目錄
2.1.3 路徑搜尋
當一個Makefile中涉及到大量原始檔時(這些原始檔和Makefile極有可能不在同一個目錄中),
這時, 最好將原始檔的路徑明確在Makefile中, 便於編譯時查詢. Makefile中有個特殊的變數VPATH就是完成這個功能的.
指定了VPATH之後, 如果當前目錄中沒有找到相應檔案或依賴的檔案, Makefile 回到VPATH指定的路徑中再去查詢..
VPATH使用方法:
- vpath <directories> :: 當前目錄中找不到檔案時, 就從<directories>中搜索
- vpath <pattern> <directories> :: 符合<pattern>格式的檔案, 就從<directories>中搜索
- vpath <pattern> :: 清除符合<pattern>格式的檔案搜尋路徑
- vpath :: 清除所有已經設定好的檔案路徑
# 示例1 - 當前目錄中找不到檔案時, 按順序從 src目錄 ../parent-dir目錄中查詢檔案 VPATH src:../parent-dir # 示例2 - .h結尾的檔案都從 ./header 目錄中查詢 VPATH %.h ./header # 示例3 - 清除示例2中設定的規則 VPATH %.h # 示例4 - 清除所有VPATH的設定 VPATH
2.2 Makefile 中的變數
2.2.1 變數定義 ( = or := )
OBJS = programA.o programB.o OBJS-ADD = $(OBJS) programC.o # 或者 OBJS := programA.o programB.o OBJS-ADD := $(OBJS) programC.o
其中 = 和 := 的區別在於, := 只能使用前面定義好的變數, = 可以使用後面定義的變數
測試 =
# Makefile內容 OBJS2 = $(OBJS1) programC.o OBJS1 = programA.o programB.o all: @echo $(OBJS2) # bash中執行 make, 可以看出雖然 OBJS1 是在 OBJS2 之後定義的, 但在 OBJS2中可以提前使用 $ make programA.o programB.o programC.o
測試 :=
# Makefile內容 OBJS2 := $(OBJS1) programC.o OBJS1 := programA.o programB.o all: @echo $(OBJS2) # bash中執行 make, 可以看出 OBJS2 中的 $(OBJS1) 為空 $ make programC.o
2.2.2 變數替換
# Makefile內容 SRCS := programA.c programB.c programC.c OBJS := $(SRCS:%.c=%.o) all: @echo "SRCS: " $(SRCS) @echo "OBJS: " $(OBJS) # bash中執行make $ make SRCS: programA.c programB.c programC.c OBJS: programA.o programB.o programC.o
2.2.3 變數追加值 +=
# Makefile內容 SRCS := programA.c programB.c programC.c SRCS += programD.c all: @echo "SRCS: " $(SRCS) # bash中執行make $ make SRCS: programA.c programB.c programC.c programD.c
2.2.4 變數覆蓋 override
作用是使 Makefile中定義的變數能夠覆蓋 make 命令引數中指定的變數
語法:
- override <variable> = <value>
- override <variable> := <value>
- override <variable> += <value>
下面通過一個例子體會 override 的作用:
# Makefile內容 (沒有用override) SRCS := programA.c programB.c programC.c all: @echo "SRCS: " $(SRCS) # bash中執行make $ make SRCS=nothing SRCS: nothing ################################################# # Makefile內容 (用override) override SRCS := programA.c programB.c programC.c all: @echo "SRCS: " $(SRCS) # bash中執行make $ make SRCS=nothing SRCS: programA.c programB.c programC.c
2.2.5 目標變數
作用是使變數的作用域僅限於這個目標(target), 而不像之前例子中定義的變數, 對整個Makefile都有效.
語法:
- <target ...> :: <variable-assignment>
- <target ...> :: override <variable-assignment> (override作用參見 變數覆蓋的介紹)
示例:
# Makefile 內容 SRCS := programA.c programB.c programC.c target1: TARGET1-SRCS := programD.c target1: @echo "SRCS: " $(SRCS) @echo "SRCS: " $(TARGET1-SRCS) target2: @echo "SRCS: " $(SRCS) @echo "SRCS: " $(TARGET1-SRCS) # bash中執行make $ make target1 SRCS: programA.c programB.c programC.c SRCS: programD.c $ make target2 <-- target2中顯示不了 $(TARGET1-SRCS) SRCS: programA.c programB.c programC.c SRCS:
2.3 Makefile 命令字首
Makefile 中書寫shell命令時可以加2種字首 @ 和 -, 或者不用字首.
3種格式的shell命令區別如下:
- 不用字首 :: 輸出執行的命令以及命令執行的結果, 出錯的話停止執行
- 字首 @ :: 只輸出命令執行的結果, 出錯的話停止執行
- 字首 - :: 命令執行有錯的話, 忽略錯誤, 繼續執行
示例:
# Makefile 內容 (不用字首) all: echo "沒有字首" cat this_file_not_exist echo "錯誤之後的命令" <-- 這條命令不會被執行 # bash中執行 make $ make echo "沒有字首" <-- 命令本身顯示出來 沒有字首 <-- 命令執行結果顯示出來 cat this_file_not_exist cat: this_file_not_exist: No such file or directory make: *** [all] Error 1 ########################################################### # Makefile 內容 (字首 @) all: @echo "沒有字首" @cat this_file_not_exist @echo "錯誤之後的命令" <-- 這條命令不會被執行 # bash中執行 make $ make 沒有字首 <-- 只有命令執行的結果, 不顯示命令本身 cat: this_file_not_exist: No such file or directory make: *** [all] Error 1 ########################################################### # Makefile 內容 (字首 -) all: -echo "沒有字首" -cat this_file_not_exist -echo "錯誤之後的命令" <-- 這條命令會被執行 # bash中執行 make $ make echo "沒有字首" <-- 命令本身顯示出來 沒有字首 <-- 命令執行結果顯示出來 cat this_file_not_exist cat: this_file_not_exist: No such file or directory make: [all] Error 1 (ignored) echo "錯誤之後的命令" <-- 出錯之後的命令也會顯示 錯誤之後的命令 <-- 出錯之後的命令也會執行
2.4 偽目標
偽目標並不是一個"目標(target)", 不像真正的目標那樣會生成一個目標檔案.
典型的偽目標是 Makefile 中用來清理編譯過程中中間檔案的 clean 偽目標, 一般格式如下:
.PHONY: clean <-- 這句沒有也行, 但是最好加上 clean: -rm -f *.o
2.5 引用其他的 Makefile
語法: include <filename> (filename 可以包含萬用字元和路徑)
示例:
# Makefile 內容 all: @echo "主 Makefile begin" @make other-all @echo "主 Makefile end" include ./other/Makefile # ./other/Makefile 內容 other-all: @echo "other makefile begin" @echo "other makefile end" # bash中執行 make $ ll total 20K -rw-r--r-- 1 wangyubin wangyubin 125 Sep 23 16:13 Makefile -rw-r--r-- 1 wangyubin wangyubin 11K Sep 23 16:15 makefile.org <-- 這個檔案不用管 drwxr-xr-x 2 wangyubin wangyubin 4.0K Sep 23 16:11 other $ ll other/ total 4.0K -rw-r--r-- 1 wangyubin wangyubin 71 Sep 23 16:11 Makefile $ make 主 Makefile begin make[1]: Entering directory `/path/to/test/makefile' other makefile begin other makefile end make[1]: Leaving directory `/path/to/test/makefile' 主 Makefile end
2.6 檢視C檔案的依賴關係
寫 Makefile 的時候, 需要確定每個目標的依賴關係.
GNU提供一個機制可以檢視C程式碼檔案依賴那些檔案, 這樣我們在寫 Makefile 目標的時候就不用開啟C原始碼來看其依賴那些檔案了.
比如, 下面命令顯示核心原始碼中 virt/kvm/kvm_main.c 中的依賴關係
$ cd virt/kvm/ $ gcc -MM kvm_main.c kvm_main.o: kvm_main.c iodev.h coalesced_mmio.h async_pf.h <-- 這句就可以加到 Makefile 中作為編譯 kvm_main.o 的依賴關係
2.7 make 退出碼
Makefile的退出碼有以下3種:
- 0 :: 表示成功執行
- 1 :: 表示make命令出現了錯誤
- 2 :: 使用了 "-q" 選項, 並且make使得一些目標不需要更新
2.8 指定 Makefile, 指定特定目標
預設執行 make 命令時, GNU make在當前目錄下依次搜尋下面3個檔案 "GNUmakefile", "makefile", "Makefile",
找到對應檔案之後, 就開始執行此檔案中的第一個目標(target). 如果找不到這3個檔案就報錯.
非預設情況下, 可以在 make 命令中指定特定的 Makefile 和特定的 目標.
示例:
# Makefile檔名改為 MyMake, 內容 target1: @echo "target [1] begin" @echo "target [1] end" target2: @echo "target [2] begin" @echo "target [2] end" # bash 中執行 make $ ls Makefile $ mv Makefile MyMake $ ls MyMake $ make <-- 找不到預設的 Makefile make: *** No targets specified and no makefile found. Stop. $ make -f MyMake <-- 指定特定的Makefile target [1] begin target [1] end $ make -f MyMake target2 <-- 指定特定的目標(target) target [2] begin target [2] end
2.9 make 引數介紹
make 的引數有很多, 可以通過 make -h 去檢視, 下面只介紹幾個我認為比較有用的.
引數 |
含義 |
--debug[=<options>] | 輸出make的除錯資訊, options 可以是 a, b, v |
-j --jobs | 同時執行的命令的個數, 也就是多執行緒執行 Makefile |
-r --no-builtin-rules | 禁止使用任何隱含規則 |
-R --no-builtin-variabes | 禁止使用任何作用於變數上的隱含規則 |
-B --always-make | 假設所有目標都有更新, 即強制重編譯 |
2.10 Makefile 隱含規則
這裡只列一個和編譯C相關的.
編譯C時,<n>.o 的目標會自動推導為 <n>.c
# Makefile 中 main : main.o gcc -o main main.o #會自動變為: main : main.o gcc -o main main.o main.o: main.c <-- main.o 這個目標是隱含生成的 gcc -c main.c
2.11 隱含規則中的 命令變數 和 命令引數變數
2.11.1 命令變數, 書寫Makefile可以直接寫 shell時用這些變數.
下面只列出一些C相關的
變數名 |
含義 |
RM | rm -f |
AR | ar |
CC | cc |
CXX | g++ |
示例:
# Makefile 內容 all: @echo $(RM) @echo $(AR) @echo $(CC) @echo $(CXX) # bash 中執行make, 顯示各個變數的值 $ make rm -f ar cc g++
2.11.2 命令引數變數
變數名 |
含義 |
ARFLAGS | AR命令的引數 |
CFLAGS | C語言編譯器的引數 |
CXXFLAGS | C++語言編譯器的引數 |
示例: 下面以 CFLAGS 為例演示
# test.c 內容 #include <stdio.h> int main(int argc, char *argv[]) { printf ("Hello Makefile\n"); return 0; } # Makefile 內容 test: test.o $(CC) -o test test.o # bash 中用 make 來測試 $ ll total 24K -rw-r--r-- 1 wangyubin wangyubin 69 Sep 23 17:31 Makefile -rw-r--r-- 1 wangyubin wangyubin 14K Sep 23 19:51 makefile.org <-- 請忽略這個檔案 -rw-r--r-- 1 wangyubin wangyubin 392 Sep 23 17:31 test.c $ make cc -c -o test.o test.c cc -o test test.o <-- 這個是自動推導的 $ rm -f test test.o $ make CFLAGS=-Wall <-- 命令中加的編譯器引數自動追加入下面的編譯中了 cc -Wall -c -o test.o test.c cc -o test test.o
2.12 自動變數
Makefile 中很多時候通過自動變數來簡化書寫, 各個自動變數的含義如下:
自動變數 |
含義 |
$@ | 目標集合 |
$% | 當目標是函式庫檔案時, 表示其中的目標檔名 |
$< | 第一個依賴目標. 如果依賴目標是多個, 逐個表示依賴目標 |
$? | 比目標新的依賴目標的集合 |
$^ | 所有依賴目標的集合, 會去除重複的依賴目標 |
$+ | 所有依賴目標的集合, 不會去除重複的依賴目標 |
$* | 這個是GNU make特有的, 其它的make不一定支援 |
3. Makefile 高階語法
3.1 巢狀Makefile
在 Makefile 初級語法中已經提到過引用其它 Makefile的方法. 這裡有另一種寫法, 並且可以向引用的其它 Makefile 傳遞引數.
示例: (不傳遞引數, 只是呼叫子資料夾 other 中的Makefile)
# Makefile 內容 all: @echo "主 Makefile begin" @cd ./other && make @echo "主 Makefile end" # ./other/Makefile 內容 other-all: @echo "other makefile begin" @echo "other makefile end" # bash中執行 make $ ll total 28K -rw-r--r-- 1 wangyubin wangyubin 104 Sep 23 20:43 Makefile -rw-r--r-- 1 wangyubin wangyubin 17K Sep 23 20:44 makefile.org <-- 這個檔案不用管 drwxr-xr-x 2 wangyubin wangyubin 4.0K Sep 23 20:42 other $ ll other/ total 4.0K -rw-r--r-- 1 wangyubin wangyubin 71 Sep 23 16:11 Makefile $ make 主 Makefile begin make[1]: Entering directory `/path/to/test/makefile/other' other makefile begin other makefile end make[1]: Leaving directory `/path/to/test/makefile/other' 主 Makefile end
示例: (用export傳遞引數)
# Makefile 內容 export VALUE1 := export.c <-- 用了 export, 此變數能夠傳遞到 ./other/Makefile 中 VALUE2 := no-export.c <-- 此變數不能傳遞到 ./other/Makefile 中 all: @echo "主 Makefile begin" @cd ./other && make @echo "主 Makefile end" # ./other/Makefile 內容 other-all: @echo "other makefile begin" @echo "VALUE1: " $(VALUE1) @echo "VALUE2: " $(VALUE2) @echo "other makefile end" # bash中執行 make $ make 主 Makefile begin make[1]: Entering directory `/path/to/test/makefile/other' other makefile begin VALUE1: export.c <-- VALUE1 傳遞成功 VALUE2: <-- VALUE2 傳遞失敗 other makefile end make[1]: Leaving directory `/path/to/test/makefile/other' 主 Makefile end
*補充*export 語法格式如下:
- export variable = value
- export variable := value
- export variable += value
3.2 定義命令包
命令包有點像是個函式, 將連續的相同的命令合成一條, 減少 Makefile 中的程式碼量, 便於以後維護.
語法:
define <command-name> command ... endef
示例:
# Makefile 內容 define run-hello-makefile @echo -n "Hello" @echo " Makefile!" @echo "這裡可以執行多條 Shell 命令!" endef all: $(run-hello-makefile) # bash 中執行make $ make Hello Makefile! 這裡可以執行多條 Shell 命令!
3.3 條件判斷
條件判斷的關鍵字主要有ifeq ifneq ifdef ifndef
語法:
<conditional-directive> <text-if-true> endif # 或者 <conditional-directive> <text-if-true> else <text-if-false> endif
示例:ifeq的例子, ifneq和ifeq的使用方法類似, 就是取反
# Makefile 內容 all: ifeq ("aa", "bb") @echo "equal" else @echo "not equal" endif # bash 中執行 make $ make not equal
示例:ifdef的例子, ifndef和ifdef的使用方法類似, 就是取反
# Makefile 內容 SRCS := program.c all: ifdef SRCS @echo $(SRCS) else @echo "no SRCS" endif # bash 中執行 make $ make program.c
3.4 Makefile 中的函式
Makefile 中自帶了一些函式, 利用這些函式可以簡化 Makefile 的編寫.
函式呼叫語法如下:
$(<function> <arguments>) # 或者 ${<function> <arguments>}
- <function> 是函式名
- <arguments> 是函式引數
3.4.1 字串函式
字串替換函式: $(subst <from>,<to>,<text>)
功能: 把字串<text> 中的 <from> 替換為 <to>
返回: 替換過的字串
# Makefile 內容 all: @echo $(subst t,e,maktfilt) <-- 將t替換為e # bash 中執行 make $ make makefile
模式字串替換函式: $(patsubst <pattern>,<replacement>,<text>)
功能: 查詢<text>中的單詞(單詞以"空格", "tab", "換行"來分割) 是否符合 <pattern>, 符合的話, 用 <replacement> 替代.
返回: 替換過的字串
# Makefile 內容 all: @echo $(patsubst %.c,%.o,programA.c programB.c) # bash 中執行 make $ make programA.o programB.o
去空格函式: $(strip <string>)
功能: 去掉 <string> 字串中開頭和結尾的空字元
返回: 被去掉空格的字串值
# Makefile 內容 VAL := " aa bb cc " all: @echo "去除空格前: " $(VAL) @echo "去除空格後: " $(strip $(VAL)) # bash 中執行 make $ make 去除空格前: aa bb cc 去除空格後: aa bb cc
查詢字串函式: $(findstring <find>,<in>)
功能: 在字串 <in> 中查詢 <find> 字串
返回: 如果找到, 返回 <find> 字串, 否則返回空字串
# Makefile 內容 VAL := " aa bb cc " all: @echo $(findstring aa,$(VAL)) @echo $(findstring ab,$(VAL)) # bash 中執行 make $ make aa
過濾函式: $(filter <pattern...>,<text>)
功能: 以 <pattern> 模式過濾字串 <text>, *保留* 符合模式 <pattern> 的單詞, 可以有多個模式
返回: 符合模式 <pattern> 的字串
# Makefile 內容 all: @echo $(filter %.o %.a,program.c program.o program.a) # bash 中執行 make $ make program.o program.a
反過濾函式: $(filter-out <pattern...>,<text>)
功能: 以 <pattern> 模式過濾字串 <text>, *去除* 符合模式 <pattern> 的單詞, 可以有多個模式
返回: 不符合模式 <pattern> 的字串
# Makefile 內容 all: @echo $(filter-out %.o %.a,program.c program.o program.a) # bash 中執行 make $ make program.c
排序函式: $(sort <list>)
功能: 給字串 <list> 中的單詞排序 (升序)
返回: 排序後的字串
# Makefile 內容 all: @echo $(sort bac abc acb cab) # bash 中執行 make $ make abc acb bac cab
取單詞函式: $(word <n>,<text>)
功能: 取字串 <text> 中的 第<n>個單詞 (n從1開始)
返回: <text> 中的第<n>個單詞, 如果<n> 比 <text> 中單詞個數要大, 則返回空字串
# Makefile 內容 all: @echo $(word 1,aa bb cc dd) @echo $(word 5,aa bb cc dd) @echo $(word 4,aa bb cc dd) # bash 中執行 make $ make aa dd
取單詞串函式: $(wordlist <s>,<e>,<text>)
功能: 從字串<text>中取從<s>開始到<e>的單詞串. <s>和<e>是一個數字.
返回: 從<s>到<e>的字串
# Makefile 內容 all: @echo $(wordlist 1,3,aa bb cc dd) @echo $(word 5,6,aa bb cc dd) @echo $(word 2,5,aa bb cc dd) # bash 中執行 make $ make aa bb cc bb
單詞個數統計函式: $(words <text>)
功能: 統計字串 <text> 中單詞的個數
返回: 單詞個數
# Makefile 內容 all: @echo $(words aa bb cc dd) @echo $(words aabbccdd) @echo $(words ) # bash 中執行 make $ make 4 1 0
首單詞函式: $(firstword <text>)
功能: 取字串 <text> 中的第一個單詞
返回: 字串 <text> 中的第一個單詞
# Makefile 內容 all: @echo $(firstword aa bb cc dd) @echo $(firstword aabbccdd) @echo $(firstword ) # bash 中執行 make $ make aa aabbccdd
3.4.2 檔名函式
取目錄函式: $(dir <names...>)
功能: 從檔名序列 <names> 中取出目錄部分
返回: 檔名序列 <names> 中的目錄部分
# Makefile 內容 all: @echo $(dir /home/a.c ./bb.c ../c.c d.c) # bash 中執行 make $ make /home/ ./ ../ ./
取檔案函式: $(notdir <names...>)
功能: 從檔名序列 <names> 中取出非目錄部分
返回: 檔名序列 <names> 中的非目錄部分
# Makefile 內容 all: @echo $(notdir /home/a.c ./bb.c ../c.c d.c) # bash 中執行 make $ make a.c bb.c c.c d.c
取字尾函式: $(suffix <names...>)
功能: 從檔名序列 <names> 中取出各個檔名的字尾
返回: 檔名序列 <names> 中各個檔名的字尾, 沒有後綴則返回空字串
# Makefile 內容 all: @echo $(suffix /home/a.c ./b.o ../c.a d) # bash 中執行 make $ make .c .o .a
取字首函式: $(basename <names...>)
功能: 從檔名序列 <names> 中取出各個檔名的字首
返回: 檔名序列 <names> 中各個檔名的字首, 沒有字首則返回空字串
# Makefile 內容 all: @echo $(basename /home/a.c ./b.o ../c.a /home/.d .e) # bash 中執行 make $ make /home/a ./b ../c /home/
加字尾函式: $(addsuffix <suffix>,<names...>)
功能: 把字尾 <suffix> 加到 <names> 中的每個單詞後面
返回: 加過後綴的檔名序列
# Makefile 內容 all: @echo $(addsuffix .c,/home/a b ./c.o ../d.c) # bash 中執行 make $ make /home/a.c b.c ./c.o.c ../d.c.c
加字首函式: $(addprefix <prefix>,<names...>)
功能: 把字首 <prefix> 加到 <names> 中的每個單詞前面
返回: 加過字首的檔名序列
# Makefile 內容 all: @echo $(addprefix test_,/home/a.c b.c ./d.c) # bash 中執行 make $ make test_/home/a.c test_b.c test_./d.c
連線函式: $(join <list1>,<list2>)
功能: <list2> 中對應的單詞加到 <list1> 後面
返回: 連線後的字串
# Makefile 內容 all: @echo $(join a b c d,1 2 3 4) @echo $(join a b c d,1 2 3 4 5) @echo $(join a b c d e,1 2 3 4) # bash 中執行 make $ make a1 b2 c3 d4 a1 b2 c3 d4 5 a1 b2 c3 d4 e
3.4.3 foreach
語法:
$(foreach <var>,<list>,<text>)
示例:
# Makefile 內容 targets := a b c d objects := $(foreach i,$(targets),$(i).o) all: @echo $(targets) @echo $(objects) # bash 中執行 make $ make a b c d a.o b.o c.o d.o
3.4.4 if
這裡的if是個函式, 和前面的條件判斷不一樣, 前面的條件判斷屬於Makefile的關鍵字
語法:
$(if <condition>,<then-part>)
$(if <condition>,<then-part>,<else-part>)
示例:
# Makefile 內容 val := a objects := $(if $(val),$(val).o,nothing) no-objects := $(if $(no-val),$(val).o,nothing) all: @echo $(objects) @echo $(no-objects) # bash 中執行 make $ make a.o nothing
3.4.5 call - 建立新的引數化函式
語法:
$(call <expression>,<parm1>,<parm2>,<parm3>...)
示例:
# Makefile 內容 log = "====debug====" $(1) "====end====" all: @echo $(call log,"正在 Make") # bash 中執行 make $ make ====debug==== 正在 Make ====end====
3.4.6 origin - 判斷變數的來源
語法:
$(origin <variable>)
返回值有如下型別:
型別 |
含義 |
undefined | <variable> 沒有定義過 |
default | <variable> 是個預設的定義, 比如 CC 變數 |
environment | <variable> 是個環境變數, 並且 make時沒有使用 -e 引數 |
file | <variable> 定義在Makefile中 |
command line | <variable> 定義在命令列中 |
override | <variable> 被 override 重新定義過 |
automatic | <variable> 是自動化變數 |
示例:
# Makefile 內容 val-in-file := test-file override val-override := test-override all: @echo $(origin not-define) # not-define 沒有定義 @echo $(origin CC) # CC 是Makefile預設定義的變數 @echo $(origin PATH) # PATH 是 bash 環境變數 @echo $(origin val-in-file) # 此Makefile中定義的變數 @echo $(origin val-in-cmd) # 這個變數會加在 make 的引數中 @echo $(origin val-override) # 此Makefile中定義的override變數 @echo $(origin @) # 自動變數, 具體前面的介紹 # bash 中執行 make $ make val-in-cmd=val-cmd undefined default environment file command line override automatic
3.4.7 shell
語法:
$(shell <shell command>)
它的作用就是執行一個shell命令, 並將shell命令的結果作為函式的返回.
作用和 `<shell command>` 一樣,`是反引號
3.4.8 make 控制函式
產生一個致命錯誤: $(error <text ...>)
功能: 輸出錯誤資訊, 停止Makefile的執行
# Makefile 內容 all: $(error there is an error!) @echo "這裡不會執行!" # bash 中執行 make $ make Makefile:2: *** there is an error!. Stop.
輸出警告: $(warning <text ...>)
功能: 輸出警告資訊, Makefile繼續執行
# Makefile 內容 all: $(warning there is an warning!) @echo "這裡會執行!" # bash 中執行 make $ make Makefile:2: there is an warning! 這裡會執行!
3.5 Makefile中一些GNU約定俗成的偽目標
如果有過在Linux上, 從原始碼安裝軟體的經歷的話, 就會對 make clean, make install 比較熟悉.
像 clean, install 這些偽目標, 廣為人知, 不用解釋就大家知道是什麼意思了.
下面列舉一些常用的偽目標, 如果在自己專案的Makefile合理使用這些偽目標的話, 可以讓我們自己的Makefile看起來更專業, 呵呵 :)
偽目標 |
含義 |
all | 所有目標的目標,其功能一般是編譯所有的目標 |
clean | 刪除所有被make建立的檔案 |
install | 安裝已編譯好的程式,其實就是把目標可執行檔案拷貝到指定的目錄中去 |
列出改變過的原始檔 | |
tar | 把源程式打包備份. 也就是一個tar檔案 |
dist | 建立一個壓縮檔案, 一般是把tar檔案壓成Z檔案. 或是gz檔案 |
TAGS | 更新所有的目標, 以備完整地重編譯使用 |
check 或 test | 一般用來測試makefile的流程 |
關注 - 6
粉絲 - 379 關注成功 86 0 «上一篇:掛載KVM Guest作業系統磁碟
»下一篇:CPPUTest 單元測試框架(針對 C 單元測試的使用說明) posted @2014-09-24 16:10wang_yb 閱讀(273548) 評論(33)編輯收藏
評論列表 回覆引用 #1樓2015-04-24 10:55StevensFollower 此文 格式排版漂亮 內容條理清晰 收藏了 支援(3)反對(0) 回覆引用 #2樓2015-07-05 18:42ying_feng 好文! 支援(0)反對(0) 回覆引用 #3樓2015-07-14 09:52rlandjon perfect! 支援(0)反對(0) 回覆引用 #4樓2015-09-27 01:13flfwzgl 好文, 收藏了 支援(0)反對(0) 回覆引用 #5樓2015-10-13 09:55thinkinglife 說得很清楚,讓看的人一目瞭然,贊一個! 支援(0)反對(0) 回覆引用 #6樓2015-10-15 19:05huaishen 相當不錯 支援(0)反對(0) 回覆引用 #7樓2015-12-04 22:08牧 天 mark 見過的最好的了 支援(0)反對(0) 回覆引用 #8樓2016-01-28 10:51退隱江湖 寫的很好的,謝謝博主了! 支援(0)反對(0) 回覆引用 #9樓2016-03-04 16:46octocat 收藏,對makefile很詳細的介紹,可以作為跟我一起學Makefile入門 支援(0)反對(0) 回覆引用 #10樓2016-03-29 22:12冰靈天堂 整理的非常好啊! 支援(0)反對(0) 回覆引用 #11樓2016-03-30 17:56Ace8793 mark 支援(0)反對(0) 回覆引用 #12樓2016-08-12 20:49亞煞極 簡單明瞭 支援(0)反對(0) 回覆引用 #13樓2016-08-25 23:38longci 2.11.1 命令變數, 書寫Makefile可以直接寫 shell時用這些變數.
裡面,CC 的預設值(樓主應該是粗心),應該是 gcc 支援(0)反對(0) 回覆引用 #14樓[樓主]2016-08-26 14:27wang_yb @longci
cc 也是 c語言的編譯器 支援(0)反對(0) 回覆引用 #15樓2016-09-24 08:54雪兒的天空 非常好的文章 支援(0)反對(0) 回覆引用 #16樓2017-03-09 22:37ashen~ 不多說好東西 支援(0)反對(0) 回覆引用 #17樓2017-04-08 16:13wzbaxmt 筆記做了4張,學到了~ 支援(0)反對(0) 回覆引用 #18樓2017-04-19 07:27超哥萬歲 有例項的博文才是好博文 支援(0)反對(0) 回覆引用 #19樓2017-06-27 20:42城闕 好好拜讀下^__^ 支援(0)反對(0) 回覆引用 #20樓2017-07-21 09:20立志做一個好的程式設計師 makefile 的好文啊,謝謝分享 支援(0)反對(0) 回覆引用 #21樓2017-08-22 10:59LittleKu 必須回覆點贊並收藏 支援(0)反對(0) 回覆引用 #22樓2017-10-20 21:15longwind09 樓主文章漂亮,部落格主題簡潔優美,人應該也很nice,求交往啊 支援(0)反對(0) 回覆引用 #23樓2017-12-01 09:43Kernel001 博主,真用心--收藏了 支援(0)反對(0) 回覆引用 #24樓2017-12-20 11:43Lrack 類 好文,感謝 支援(0)反對(0) 回覆引用 #25樓2018-10-26 15:24luoyesiqiu 好文!謝謝 支援(0)反對(0) 回覆引用 #26樓2018-12-28 23:09fhlx 必須贊 支援(0)反對(0) 回覆引用 #27樓2019-04-23 17:23Smah 清晰 支援(0)反對(0) 回覆引用 #28樓2019-06-04 17:03mythbuster 請教一下怎麼通過make後面加上引數向makefile中的變數傳遞引數呢? 支援(0)反對(0) 回覆引用 #29樓[樓主]2019-06-06 11:33wang_yb @mythbuster
1 |
make val-in-cmd=xxx
|
makefile 中獲取這個變數:
1 2 |
all:
@echo $(val- in -cmd)
|
你好, makefile中基本都是shell命令, 一般的makefile通過列印一些資訊來除錯.
如果是很複雜的makefile, 建議可以分成多個部分分別除錯之後再合併 支援(0)反對(0) 回覆引用 #33樓2020-03-14 19:04不告訴你我是誰
正在學習Makefile,剛好看到,太有用了。
支援(0)反對(0) 重新整理評論重新整理頁面返回頂部 發表評論 編輯預覽退出訂閱評論
[Ctrl+Enter快捷鍵提交]
【推薦】超50萬行VC++原始碼: 大型組態工控、電力模擬CAD與GIS原始碼庫【推薦】1200件T恤+6萬獎金,阿里雲程式設計大賽報名開啟
【推薦】未知數的距離,毫秒間的傳遞,聲網與你實時互動
【推薦】了不起的開發者,擋不住的華為,園子裡的品牌專區
【推薦】Java實戰200例(附原始碼)-助你提升實戰能力 相關博文:
·Makefile 中:= ?= += =的區別
·Makefile使用總結
·Makefile中的函式
·Makefile小結
·Makefile總結(轉帖)
»更多推薦... 最新 IT 新聞:
·騰訊申請自動駕駛、車輛碰撞預警等多項涉車專利
·業績大好,順豐股價週五再漲停,市值近4000億元
·小米股價週五再漲10%,今年股價已經翻番
·微軟曾持有的蘋果股票現在價值8816億元 可惜賣早了
·聚划算推出小鵬汽車百億補貼 單車直補5萬元
»更多新聞...
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 31 | 1 | |||
2 | 3 | 4 | 5 | 6 | 7 | 8 | |||
9 | 10 | 11 | 12 | 13 | 14 | 15 | |||
16 | 17 | 18 | 19 | 20 | 21 | 22 | |||
23 | 24 | 25 | 26 | 27 | 28 | 29 | |||
30 | 31 | 1 | 2 | 3 | 4 | 5 |
搜尋
常用連結
我的標籤
- linux(70)
- linux-kernel(30)
- CLR via C#筆記(24)
- golang(23)
- elixir(19)
- C#學習(9)
- puppet(8)
- 前端開發(8)
- 開發工具(7)
- C#疑難雜症(7)
- 更多
積分與排名
- 積分 - 300576
- 排名 - 1469
隨筆檔案(243)
- 2020年8月(1)
- 2020年7月(1)
- 2020年6月(2)
- 2020年5月(1)
- 2020年4月(5)
- 2020年3月(1)
- 2020年2月(1)
- 2020年1月(1)
- 2019年12月(1)
- 2019年11月(1)
- 2019年10月(1)
- 2019年9月(5)
- 2019年8月(1)
- 2019年7月(1)
- 2019年6月(1)
- 2019年5月(4)
- 2019年4月(3)
- 2019年3月(1)
- 2019年2月(3)
- 2019年1月(1)
- 2018年12月(1)
- 2018年11月(1)
- 2018年10月(2)
- 2018年8月(1)
- 2018年6月(2)
- 2018年5月(1)
- 2018年4月(2)
- 2018年3月(2)
- 2017年12月(1)
- 2017年11月(1)
- 2017年9月(1)
- 2017年5月(1)
- 2017年1月(1)
- 2016年12月(3)
- 2016年11月(2)
- 2016年10月(1)
- 2016年9月(3)
- 2016年8月(2)
- 2016年7月(1)
- 2016年6月(3)
- 2016年5月(3)
- 2016年4月(1)
- 2016年3月(1)
- 2016年2月(1)
- 2015年10月(2)
- 2015年3月(2)
- 2015年2月(2)
- 2015年1月(7)
- 2014年11月(1)
- 2014年10月(1)
- 2014年9月(4)
- 2014年8月(3)
- 2014年7月(11)
- 2014年6月(40)
- 2014年5月(7)
- 2014年4月(1)
- 2014年3月(1)
- 2014年1月(6)
- 2013年12月(1)
- 2013年11月(1)
- 2013年10月(1)
- 2013年9月(1)
- 2013年6月(1)
- 2013年5月(3)
- 2013年4月(6)
- 2012年9月(2)
- 2012年8月(4)
- 2012年7月(3)
- 2012年6月(1)
- 2012年1月(1)
- 2011年12月(2)
- 2011年11月(4)
- 2011年10月(2)
- 2011年9月(1)
- 2011年8月(2)
- 2011年7月(9)
- 2011年6月(5)
- 2011年5月(3)
- 2011年4月(9)
- 2010年6月(7)
- 2010年5月(9)
- 2010年4月(3)
最新評論
- 1. Re:《CLR Via C# 第3版》筆記之(三) - 程式集和模組
-
收藏
- 2. Re:強大的table元件-antd pro table
- @hujiayu-147 太慘了,我也遇到了...
- 3. Re:強大的table元件-antd pro table
- @皮卡丘賊皮 我也遇到這個問題了,您這個問題解決了嗎,...
- 4. Re:強大的table元件-antd pro table
- @wang_yb 什麼也沒改,別的專案裡用就沒問題,就這個專案用就是這情況...
- 5. Re:強大的table元件-antd pro table
- @hujiayu-147 改了啥東西啊?...
閱讀排行榜
- 1. Makefile 使用總結(273548)
- 2. C#操作Excel總結(96004)
- 3. GIT的使用(44744)
- 4. IIS 403錯誤詳細原因(40706)
- 5. Linux資源控制-CPU和記憶體(37574)
評論排行榜
- 1. Makefile 使用總結(33)
- 2. emacs org-mode檔案轉html檔案(24)
- 3. 《Linux核心設計與實現》讀書筆記(十二)- 記憶體管理(15)
- 4. 在Linux上部署aspnet mvc3(15)
- 5. C# 連線Oracle(利用ODP.net,不安裝oracle客戶端)(15)
Powered by .NET Core on Kubernetes