Makefile教程
1. Makefile 簡(jiǎn)介
Makefile 是和 make 命令一起配合使用的.
很多大型項(xiàng)目的編譯都是通過(guò) Makefile 來(lái)組織的, 如果沒(méi)有 Makefile, 那很多項(xiàng)目中各種庫(kù)和代碼之間的依賴(lài)關(guān)系不知會(huì)多復(fù)雜.
Makefile的組織流程的能力如此之強(qiáng), 不僅可以用來(lái)編譯項(xiàng)目, 還可以用來(lái)組織我們平時(shí)的一些日常操作. 這個(gè)需要大家發(fā)揮自己的想象力.
非常感謝 gunguymadman_cu 提供如此詳盡的Makefile介紹, 這正是我一直尋找的Makefile中文文檔.
1.1 Makefile 主要的 5個(gè)部分 (顯示規(guī)則, 隱晦規(guī)則, 變量定義, 文件指示, 注釋)
顯示規(guī)則 :: 說(shuō)明如何生成一個(gè)或多個(gè)目標(biāo)文件(包括 生成的文件, 文件的依賴(lài)文件, 生成的命令)
隱晦規(guī)則 :: make的自動(dòng)推導(dǎo)功能所執(zhí)行的規(guī)則
變量定義 :: Makefile中定義的變量
文件指示 :: Makefile中引用其他Makefile; 指定Makefile中有效部分; 定義一個(gè)多行命令
注釋 :: Makefile只有行注釋 “#”, 如果要使用或者輸出"#"字符, 需要進(jìn)行轉(zhuǎn)義, “#”
Makefile基本格式如下:
target ... : prerequisites ...command......其中,
- target - 目標(biāo)文件, 可以是 Object File, 也可以是可執(zhí)行文件
- prerequisites - 生成 target 所需要的文件或者目標(biāo)
- command - make需要執(zhí)行的命令 (任意的shell命令), Makefile中的命令必須以 [tab] 開(kāi)頭
1.2 GNU make 的工作方式
1.讀入主Makefile (主Makefile中可以引用其他Makefile)
2.讀入被include的其他Makefile
3.初始化文件中的變量
4.推導(dǎo)隱晦規(guī)則, 并分析所有規(guī)則
5.為所有的目標(biāo)文件創(chuàng)建依賴(lài)關(guān)系鏈
6.根據(jù)依賴(lài)關(guān)系, 決定哪些目標(biāo)要重新生成
7.執(zhí)行生成命令
2. Makefile 初級(jí)語(yǔ)法
2.1 Makefile 規(guī)則
2.1.1 規(guī)則語(yǔ)法
規(guī)則主要有2部分: 依賴(lài)關(guān)系 和 生成目標(biāo)的方法.
語(yǔ)法有以下2種:
target ... : prerequisites ...command...或者
target ... : prerequisites ; commandcommand...*注* command太長(zhǎng), 可以用 “\” 作為換行符
2.1.2 規(guī)則中的通配符
- * :: 表示任意一個(gè)或多個(gè)字符
- ? :: 表示任意一個(gè)字符
- […] :: ex. [abcd] 表示a,b,c,d中任意一個(gè)字符, [^abcd]表示除a,b,c,d以外的字符, [0-9]表示 0~9中任意一個(gè)數(shù)字
- ~ :: 表示用戶(hù)的home目錄
2.1.3 路徑搜索
當(dāng)一個(gè)Makefile中涉及到大量源文件時(shí)(這些源文件和Makefile極有可能不在同一個(gè)目錄中),
這時(shí), 最好將源文件的路徑明確在Makefile中, 便于編譯時(shí)查找. Makefile中有個(gè)特殊的變量 VPATH 就是完成這個(gè)功能的.
指定了 VPATH 之后, 如果當(dāng)前目錄中沒(méi)有找到相應(yīng)文件或依賴(lài)的文件, Makefile 回到 VPATH 指定的路徑中再去查找…
VPATH 使用方法:
- vpath <directories> :: 當(dāng)前目錄中找不到文件時(shí), 就從<directories>中搜索
- vpath <pattern> <directories> :: 符合<pattern>格式的文件, 就從<directories>中搜索
- vpath <pattern> :: 清除符合<pattern>格式的文件搜索路徑
- vpath :: 清除所有已經(jīng)設(shè)置好的文件路徑
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其中 = 和 := 的區(qū)別在于, := 只能使用前面定義好的變量, = 可以使用后面定義的變量
測(cè)試 =
# Makefile內(nèi)容 OBJS2 = $(OBJS1) programC.o OBJS1 = programA.o programB.oall:@echo $(OBJS2)# bash中執(zhí)行 make, 可以看出雖然 OBJS1 是在 OBJS2 之后定義的, 但在 OBJS2中可以提前使用 $ make programA.o programB.o programC.o測(cè)試 :=
# Makefile內(nèi)容 OBJS2 := $(OBJS1) programC.o OBJS1 := programA.o programB.oall:@echo $(OBJS2)# bash中執(zhí)行 make, 可以看出 OBJS2 中的 $(OBJS1) 為空 $ make programC.o2.2.2 變量替換
# Makefile內(nèi)容 SRCS := programA.c programB.c programC.c OBJS := $(SRCS:%.c=%.o)all:@echo "SRCS: " $(SRCS)@echo "OBJS: " $(OBJS)# bash中運(yùn)行make $ make SRCS: programA.c programB.c programC.c OBJS: programA.o programB.o programC.o2.2.3 變量追加值 +=
# Makefile內(nèi)容 SRCS := programA.c programB.c programC.c SRCS += programD.call:@echo "SRCS: " $(SRCS)# bash中運(yùn)行make $ make SRCS: programA.c programB.c programC.c programD.c2.2.4 變量覆蓋 override
作用是使 Makefile中定義的變量能夠覆蓋 make 命令參數(shù)中指定的變量
語(yǔ)法:
- override <variable> = <value>
- override <variable> := <value>
- override <variable> += <value>
下面通過(guò)一個(gè)例子體會(huì) override 的作用:
# Makefile內(nèi)容 (沒(méi)有用override) SRCS := programA.c programB.c programC.call:@echo "SRCS: " $(SRCS)# bash中運(yùn)行make $ make SRCS=nothing SRCS: nothing################################################## Makefile內(nèi)容 (用override) override SRCS := programA.c programB.c programC.call:@echo "SRCS: " $(SRCS)# bash中運(yùn)行make $ make SRCS=nothing SRCS: programA.c programB.c programC.c2.2.5 目標(biāo)變量
作用是使變量的作用域僅限于這個(gè)目標(biāo)(target), 而不像之前例子中定義的變量, 對(duì)整個(gè)Makefile都有效.
語(yǔ)法:
- <target …> :: <variable-assignment>
- <target …> :: override <variable-assignment> (override作用參見(jiàn) 變量覆蓋的介紹)
示例:
# Makefile 內(nèi)容 SRCS := programA.c programB.c programC.ctarget1: TARGET1-SRCS := programD.c target1:@echo "SRCS: " $(SRCS)@echo "SRCS: " $(TARGET1-SRCS)target2:@echo "SRCS: " $(SRCS)@echo "SRCS: " $(TARGET1-SRCS)# bash中執(zhí)行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 中書(shū)寫(xiě)shell命令時(shí)可以加2種前綴 @ 和 -, 或者不用前綴.
3種格式的shell命令區(qū)別如下:
- 不用前綴 :: 輸出執(zhí)行的命令以及命令執(zhí)行的結(jié)果, 出錯(cuò)的話(huà)停止執(zhí)行
- 前綴 @ :: 只輸出命令執(zhí)行的結(jié)果, 出錯(cuò)的話(huà)停止執(zhí)行
- 前綴 - :: 命令執(zhí)行有錯(cuò)的話(huà), 忽略錯(cuò)誤, 繼續(xù)執(zhí)行
示例:
# Makefile 內(nèi)容 (不用前綴) all:echo "沒(méi)有前綴"cat this_file_not_existecho "錯(cuò)誤之后的命令" <-- 這條命令不會(huì)被執(zhí)行# bash中執(zhí)行 make $ make echo "沒(méi)有前綴" <-- 命令本身顯示出來(lái) 沒(méi)有前綴 <-- 命令執(zhí)行結(jié)果顯示出來(lái) cat this_file_not_exist cat: this_file_not_exist: No such file or directory make: *** [all] Error 1############################################################ Makefile 內(nèi)容 (前綴 @) all:@echo "有前綴@"@cat this_file_not_exist@echo "錯(cuò)誤之后的命令" <-- 這條命令不會(huì)被執(zhí)行# bash中執(zhí)行 make $ make 有前綴@ <-- 只有命令執(zhí)行的結(jié)果, 不顯示命令本身 cat: this_file_not_exist: No such file or directory make: *** [all] Error 1############################################################ Makefile 內(nèi)容 (前綴 -) all:-echo "有前綴-"-cat this_file_not_exist-echo "錯(cuò)誤之后的命令" <-- 這條命令會(huì)被執(zhí)行# bash中執(zhí)行 make $ make echo "有前綴-" <-- 命令本身顯示出來(lái) 有前綴- <-- 命令執(zhí)行結(jié)果顯示出來(lái) cat this_file_not_exist cat: this_file_not_exist: No such file or directory make: [all] Error 1 (ignored) echo "錯(cuò)誤之后的命令" <-- 出錯(cuò)之后的命令也會(huì)顯示 錯(cuò)誤之后的命令 <-- 出錯(cuò)之后的命令也會(huì)執(zhí)行2.4 偽目標(biāo)
偽目標(biāo)并不是一個(gè)"目標(biāo)(target)", 不像真正的目標(biāo)那樣會(huì)生成一個(gè)目標(biāo)文件.
典型的偽目標(biāo)是 Makefile 中用來(lái)清理編譯過(guò)程中中間文件的 clean 偽目標(biāo), 一般格式如下:
.PHONY: clean <-- 這句沒(méi)有也行, 但是最好加上 clean:-rm -f *.o2.5 引用其他的 Makefile
語(yǔ)法: include <filename> (filename 可以包含通配符和路徑)
示例:
# Makefile 內(nèi)容 all:@echo "主 Makefile begin"@make other-all@echo "主 Makefile end"include ./other/Makefile# ./other/Makefile 內(nèi)容 other-all:@echo "other makefile begin"@echo "other makefile end"# bash中執(zhí)行 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 <-- 這個(gè)文件不用管 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 end2.6 查看C文件的依賴(lài)關(guān)系
寫(xiě) Makefile 的時(shí)候, 需要確定每個(gè)目標(biāo)的依賴(lài)關(guān)系.
GNU提供一個(gè)機(jī)制可以查看C代碼文件依賴(lài)那些文件, 這樣我們?cè)趯?xiě) Makefile 目標(biāo)的時(shí)候就不用打開(kāi)C源碼來(lái)看其依賴(lài)那些文件了.
比如, 下面命令顯示內(nèi)核源碼中 virt/kvm/kvm_main.c 中的依賴(lài)關(guān)系
$ 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 的依賴(lài)關(guān)系2.7 make 退出碼
Makefile的退出碼有以下3種:
- 0 :: 表示成功執(zhí)行
- 1 :: 表示make命令出現(xiàn)了錯(cuò)誤
- 2 :: 使用了 “-q” 選項(xiàng), 并且make使得一些目標(biāo)不需要更新
2.8 指定 Makefile, 指定特定目標(biāo)
默認(rèn)執(zhí)行 make 命令時(shí), GNU make在當(dāng)前目錄下依次搜索下面3個(gè)文件 “GNUmakefile”, “makefile”, “Makefile”,
找到對(duì)應(yīng)文件之后, 就開(kāi)始執(zhí)行此文件中的第一個(gè)目標(biāo)(target). 如果找不到這3個(gè)文件就報(bào)錯(cuò).
非默認(rèn)情況下, 可以在 make 命令中指定特定的 Makefile 和特定的目標(biāo).
示例:
# Makefile文件名改為 MyMake, 內(nèi)容 target1:@echo "target [1] begin"@echo "target [1] end"target2:@echo "target [2] begin"@echo "target [2] end"# bash 中執(zhí)行 make $ ls Makefile $ mv Makefile MyMake $ ls MyMake $ make <-- 找不到默認(rèn)的 Makefile make: *** No targets specified and no makefile found. Stop. $ make -f MyMake <-- 指定特定的Makefile target [1] begin target [1] end $ make -f MyMake target2 <-- 指定特定的目標(biāo)(target) target [2] begin target [2] end2.9 make 參數(shù)介紹
make 的參數(shù)有很多, 可以通過(guò) make -h 去查看, 下面只介紹幾個(gè)我認(rèn)為比較有用的.
| --debug[=<options>] | 輸出make的調(diào)試信息, options 可以是 a, b, v |
| -j --jobs | 同時(shí)運(yùn)行的命令的個(gè)數(shù), 也就是多線(xiàn)程執(zhí)行 Makefile |
| -r --no-builtin-rules | 禁止使用任何隱含規(guī)則 |
| -R --no-builtin-variabes | 禁止使用任何作用于變量上的隱含規(guī)則 |
| -B --always-make | 假設(shè)所有目標(biāo)都有更新, 即強(qiáng)制重編譯 |
2.10 Makefile 隱含規(guī)則
這里只列一個(gè)和編譯C相關(guān)的.
編譯C時(shí),<n>.o 的目標(biāo)會(huì)自動(dòng)推導(dǎo)為 <n>.c
# Makefile 中 main : main.ogcc -o main main.o#會(huì)自動(dòng)變?yōu)?span id="ze8trgl8bvbq" class="token operator">: main : main.ogcc -o main main.omain.o: main.c <-- main.o 這個(gè)目標(biāo)是隱含生成的gcc -c main.c2.11 隱含規(guī)則中的 命令變量 和 命令參數(shù)變量
2.11.1 命令變量, 書(shū)寫(xiě)Makefile可以直接寫(xiě) shell時(shí)用這些變量.
下面只列出一些C相關(guān)的
| RM | rm -f |
| AR | ar |
| CC | cc |
| CXX | g++ |
示例:
# Makefile 內(nèi)容 all:@echo $(RM)@echo $(AR)@echo $(CC)@echo $(CXX)# bash 中執(zhí)行make, 顯示各個(gè)變量的值 $ make rm -f ar cc g++2.11.2 命令參數(shù)變量
| ARFLAGS | AR命令的參數(shù) |
| CFLAGS | C語(yǔ)言編譯器的參數(shù) |
| CXXFLAGS | C++語(yǔ)言編譯器的參數(shù) |
示例: 下面以 CFLAGS 為例演示
# test.c 內(nèi)容 #include <stdio.h>int main(int argc, char *argv[]) {printf ("Hello Makefile\n");return 0; }# Makefile 內(nèi)容 test: test.o$(CC) -o test test.o# bash 中用 make 來(lái)測(cè)試 $ 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 <-- 請(qǐng)忽略這個(gè)文件 -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 <-- 這個(gè)是自動(dòng)推導(dǎo)的$ rm -f test test.o$ make CFLAGS=-Wall <-- 命令中加的編譯器參數(shù)自動(dòng)追加入下面的編譯中了 cc -Wall -c -o test.o test.c cc -o test test.o2.12 自動(dòng)變量
Makefile 中很多時(shí)候通過(guò)自動(dòng)變量來(lái)簡(jiǎn)化書(shū)寫(xiě), 各個(gè)自動(dòng)變量的含義如下:
| $@ | 目標(biāo)集合 |
| $% | 當(dāng)目標(biāo)是函數(shù)庫(kù)文件時(shí), 表示其中的目標(biāo)文件名 |
| $< | 第一個(gè)依賴(lài)目標(biāo). 如果依賴(lài)目標(biāo)是多個(gè), 逐個(gè)表示依賴(lài)目標(biāo) |
| $? | 比目標(biāo)新的依賴(lài)目標(biāo)的集合 |
| $^ | 所有依賴(lài)目標(biāo)的集合, 會(huì)去除重復(fù)的依賴(lài)目標(biāo) |
| $+ | 所有依賴(lài)目標(biāo)的集合, 不會(huì)去除重復(fù)的依賴(lài)目標(biāo) |
| $* | 這個(gè)是GNU make特有的, 其它的make不一定支持 |
3. Makefile 高級(jí)語(yǔ)法
3.1 嵌套Makefile
在 Makefile 初級(jí)語(yǔ)法中已經(jīng)提到過(guò)引用其它 Makefile的方法. 這里有另一種寫(xiě)法, 并且可以向引用的其它 Makefile 傳遞參數(shù).
示例: (不傳遞參數(shù), 只是調(diào)用子文件夾 other 中的Makefile)
# Makefile 內(nèi)容 all:@echo "主 Makefile begin"@cd ./other && make@echo "主 Makefile end"# ./other/Makefile 內(nèi)容 other-all:@echo "other makefile begin"@echo "other makefile end"# bash中執(zhí)行 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 <-- 這個(gè)文件不用管 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傳遞參數(shù))
# Makefile 內(nèi)容 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 內(nèi)容 other-all:@echo "other makefile begin"@echo "VALUE1: " $(VALUE1)@echo "VALUE2: " $(VALUE2)@echo "other makefile end"# bash中執(zhí)行 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*補(bǔ)充* export 語(yǔ)法格式如下:
- export variable = value
- export variable := value
- export variable += value
3.2 定義命令包
命令包有點(diǎn)像是個(gè)函數(shù), 將連續(xù)的相同的命令合成一條, 減少 Makefile 中的代碼量, 便于以后維護(hù).
語(yǔ)法:
define <command-name> command ... endef示例:
# Makefile 內(nèi)容 define run-hello-makefile @echo -n "Hello" @echo " Makefile!" @echo "這里可以執(zhí)行多條 Shell 命令!" endefall:$(run-hello-makefile)# bash 中運(yùn)行make $ make Hello Makefile! 這里可以執(zhí)行多條 Shell 命令!3.3 條件判斷
條件判斷的關(guān)鍵字主要有 ifeq ifneq ifdef ifndef
語(yǔ)法:
<conditional-directive> <text-if-true> endif# 或者 <conditional-directive> <text-if-true> else <text-if-false> endif示例: ifeq的例子, ifneq和ifeq的使用方法類(lèi)似, 就是取反
# Makefile 內(nèi)容 all: ifeq ("aa", "bb")@echo "equal" else@echo "not equal" endif# bash 中執(zhí)行 make $ make not equal示例: ifdef的例子, ifndef和ifdef的使用方法類(lèi)似, 就是取反
# Makefile 內(nèi)容 SRCS := program.call: ifdef SRCS@echo $(SRCS) else@echo "no SRCS" endif# bash 中執(zhí)行 make $ make program.c3.4 Makefile 中的函數(shù)
Makefile 中自帶了一些函數(shù), 利用這些函數(shù)可以簡(jiǎn)化 Makefile 的編寫(xiě).
函數(shù)調(diào)用語(yǔ)法如下:
$(<function> <arguments>) # 或者 ${<function> <arguments>}- <function> 是函數(shù)名
- <arguments> 是函數(shù)參數(shù)
3.4.1 字符串函數(shù)
字符串替換函數(shù): $(subst <from>,<to>,<text>)
功能: 把字符串<text> 中的 <from> 替換為 <to>
返回: 替換過(guò)的字符串
# Makefile 內(nèi)容 all:@echo $(subst t,e,maktfilt) <-- 將t替換為e# bash 中執(zhí)行 make $ make makefile模式字符串替換函數(shù): $(patsubst <pattern>,<replacement>,<text>)
功能: 查找<text>中的單詞(單詞以"空格", “tab”, "換行"來(lái)分割) 是否符合 <pattern>, 符合的話(huà), 用 <replacement> 替代.
返回: 替換過(guò)的字符串
# Makefile 內(nèi)容 all:@echo $(patsubst %.c,%.o,programA.c programB.c)# bash 中執(zhí)行 make $ make programA.o programB.o去空格函數(shù): $(strip <string>)
功能: 去掉 <string> 字符串中開(kāi)頭和結(jié)尾的空字符
返回: 被去掉空格的字符串值
# Makefile 內(nèi)容 VAL := " aa bb cc "all:@echo "去除空格前: " $(VAL)@echo "去除空格后: " $(strip $(VAL))# bash 中執(zhí)行 make $ make 去除空格前: aa bb cc 去除空格后: aa bb cc查找字符串函數(shù): $(findstring <find>,<in>)
功能: 在字符串 <in> 中查找 <find> 字符串
返回: 如果找到, 返回 <find> 字符串, 否則返回空字符串
# Makefile 內(nèi)容 VAL := " aa bb cc "all:@echo $(findstring aa,$(VAL))@echo $(findstring ab,$(VAL))# bash 中執(zhí)行 make $ make aa過(guò)濾函數(shù): $(filter <pattern…>,<text>)
功能: 以 <pattern> 模式過(guò)濾字符串 <text>, 保留符合模式 <pattern> 的單詞, 可以有多個(gè)模式
返回: 符合模式 <pattern> 的字符串
# Makefile 內(nèi)容 all:@echo $(filter %.o %.a,program.c program.o program.a)# bash 中執(zhí)行 make $ make program.o program.a反過(guò)濾函數(shù): $(filter-out <pattern…>,<text>)
功能: 以 <pattern> 模式過(guò)濾字符串<text>, 去除符合模式 <pattern> 的單詞, 可以有多個(gè)模式
返回: 不符合模式 <pattern> 的字符串
# Makefile 內(nèi)容 all:@echo $(filter-out %.o %.a,program.c program.o program.a)# bash 中執(zhí)行 make $ make program.c排序函數(shù): $(sort <list>)
功能: 給字符串 <list> 中的單詞排序 (升序)
返回: 排序后的字符串
# Makefile 內(nèi)容 all:@echo $(sort bac abc acb cab)# bash 中執(zhí)行 make $ make abc acb bac cab取單詞函數(shù): $(word <n>,<text>)
功能: 取字符串 <text> 中的 第<n>個(gè)單詞 (n從1開(kāi)始)
返回: <text> 中的第<n>個(gè)單詞, 如果<n> 比 <text> 中單詞個(gè)數(shù)要大, 則返回空字符串
# Makefile 內(nèi)容 all:@echo $(word 1,aa bb cc dd)@echo $(word 5,aa bb cc dd)@echo $(word 4,aa bb cc dd)# bash 中執(zhí)行 make $ make aadd取單詞串函數(shù): $(wordlist <s>,<e>,<text>)
功能: 從字符串<text>中取從<s>開(kāi)始到<e>的單詞串. <s>和<e>是一個(gè)數(shù)字.
返回: 從<s>到<e>的字符串
# Makefile 內(nèi)容 all:@echo $(wordlist 1,3,aa bb cc dd)@echo $(word 1,4,aa bb cc dd)@echo $(word 2,5,aa bb cc dd)# bash 中執(zhí)行 make $ make aa bb cc 4,aa bb單詞個(gè)數(shù)統(tǒng)計(jì)函數(shù): $(words <text>)
功能: 統(tǒng)計(jì)字符串 <text> 中單詞的個(gè)數(shù)
返回: 單詞個(gè)數(shù)
# Makefile 內(nèi)容all:@echo $(words aa bb cc dd)@echo $(words aabbccdd)@echo $(words )# bash 中執(zhí)行 make $ make 4 1 0首單詞函數(shù): $(firstword <text>)
功能: 取字符串 <text> 中的第一個(gè)單詞
返回: 字符串 <text> 中的第一個(gè)單詞
# Makefile 內(nèi)容 all:@echo $(firstword aa bb cc dd)@echo $(firstword aabbccdd)@echo $(firstword )# bash 中執(zhí)行 make $ make aa aabbccdd3.4.2 文件名函數(shù)
取目錄函數(shù): $(dir <names…>)
功能: 從文件名序列 <names> 中取出目錄部分
返回: 文件名序列 <names> 中的目錄部分
# Makefile 內(nèi)容 all:@echo $(dir /home/a.c ./bb.c ../c.c d.c)# bash 中執(zhí)行 make $ make /home/ ./ ../ ./取文件函數(shù): $(notdir <names…>)
功能: 從文件名序列 <names> 中取出非目錄部分
返回: 文件名序列 <names> 中的非目錄部分
# Makefile 內(nèi)容 all:@echo $(notdir /home/a.c ./bb.c ../c.c d.c)# bash 中執(zhí)行 make $ make a.c bb.c c.c d.c取后綴函數(shù): $(suffix <names…>)
功能: 從文件名序列 <names> 中取出各個(gè)文件名的后綴
返回: 文件名序列 <names> 中各個(gè)文件名的后綴, 沒(méi)有后綴則返回空字符串
# Makefile 內(nèi)容 all:@echo $(suffix /home/a.c ./b.o ../c.a d)# bash 中執(zhí)行 make $ make .c .o .a取前綴函數(shù): $(basename <names…>)
功能: 從文件名序列 <names> 中取出各個(gè)文件名的前綴
返回: 文件名序列 <names> 中各個(gè)文件名的前綴, 沒(méi)有前綴則返回空字符串
# Makefile 內(nèi)容 all:@echo $(basename /home/a.c ./b.o ../c.a .e /home/.d)# bash 中執(zhí)行 make $ make /home/a ./b ../c /home/加后綴函數(shù): $(addsuffix <suffix>,<names…>)
功能: 把后綴 <suffix> 加到 <names> 中的每個(gè)單詞后面
返回: 加過(guò)后綴的文件名序列
# Makefile 內(nèi)容 all:@echo $(addsuffix .c,/home/a b ./c.o ../d.c)# bash 中執(zhí)行 make $ make /home/a.c b.c ./c.o.c ../d.c.c加前綴函數(shù): $(addprefix <prefix>,<names…>)
功能: 把前綴 <prefix> 加到 <names> 中的每個(gè)單詞前面
返回: 加過(guò)前綴的文件名序列
# Makefile 內(nèi)容 all:@echo $(addprefix test_,/home/a.c b.c ./d.c)# bash 中執(zhí)行 make $ make test_/home/a.c test_b.c test_./d.c連接函數(shù): $(join <list1>,<list2>)
功能: <list2> 中對(duì)應(yīng)的單詞加到 <list1> 后面
返回: 連接后的字符串
# Makefile 內(nèi)容 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 中執(zhí)行 make $ make a1 b2 c3 d4 a1 b2 c3 d4 5 a1 b2 c3 d4 e3.4.3 foreach
語(yǔ)法:
$(foreach <var>,<list>,<text>)
示例:
# Makefile 內(nèi)容 targets := a b c d objects := $(foreach i,$(targets),$(i).o)all:@echo $(targets)@echo $(objects)# bash 中執(zhí)行 make $ make a b c d a.o b.o c.o d.o3.4.4 if
這里的if是個(gè)函數(shù), 和前面的條件判斷不一樣, 前面的條件判斷屬于Makefile的關(guān)鍵字
語(yǔ)法:
$(if <condition>,<then-part>)
$(if <condition>,<then-part>,<else-part>)
示例:
# Makefile 內(nèi)容 val := a objects := $(if $(val),$(val).o,nothing) no-objects := $(if $(no-val),$(val).o,nothing)all:@echo $(objects)@echo $(no-objects)# bash 中執(zhí)行 make $ make a.o nothing3.4.5 call - 創(chuàng)建新的參數(shù)化函數(shù)
語(yǔ)法:
$(call <expression>,<parm1>,<parm2>,<parm3>…)
示例:
# Makefile 內(nèi)容 log = "====debug====" $(1) "====end===="all:@echo $(call log,"正在 Make")# bash 中執(zhí)行 make $ make ====debug==== 正在 Make ====end====3.4.6 origin - 判斷變量的來(lái)源
語(yǔ)法:
$(origin <variable>)
返回值有如下類(lèi)型:
| undefined | <variable> 沒(méi)有定義過(guò) |
| default | <variable> 是個(gè)默認(rèn)的定義, 比如 CC 變量 |
| environment | <variable> 是個(gè)環(huán)境變量, 并且 make時(shí)沒(méi)有使用 -e 參數(shù) |
| file | <variable> 定義在Makefile中 |
| command line | <variable> 定義在命令行中 |
| override | <variable> 被 override 重新定義過(guò) |
| automatic | <variable> 是自動(dòng)化變量 |
示例:
# Makefile 內(nèi)容 val-in-file := test-file override val-override := test-overrideall:@echo $(origin not-define) # not-define 沒(méi)有定義@echo $(origin CC) # CC 是Makefile默認(rèn)定義的變量@echo $(origin PATH) # PATH 是 bash 環(huán)境變量@echo $(origin val-in-file) # 此Makefile中定義的變量@echo $(origin val-in-cmd) # 這個(gè)變量會(huì)加在 make 的參數(shù)中@echo $(origin val-override) # 此Makefile中定義的override變量@echo $(origin @) # 自動(dòng)變量, 具體前面的介紹# bash 中執(zhí)行 make $ make val-in-cmd=val-cmd undefined default environment file command line override automatic3.4.7 shell
語(yǔ)法:
$(shell <shell command>)
它的作用就是執(zhí)行一個(gè)shell命令, 并將shell命令的結(jié)果作為函數(shù)的返回.
作用和 `<shell command>` 一樣, ` 是反引號(hào)
3.4.8 make 控制函數(shù)
產(chǎn)生一個(gè)致命錯(cuò)誤: $(error <text …>)
功能: 輸出錯(cuò)誤信息, 停止Makefile的運(yùn)行
# Makefile 內(nèi)容 all:$(error there is an error!)@echo "這里不會(huì)執(zhí)行!"# bash 中執(zhí)行 make $ make Makefile:2: *** there is an error!. Stop.輸出警告: $(warning <text …>)
功能: 輸出警告信息, Makefile繼續(xù)運(yùn)行
# Makefile 內(nèi)容 all:$(warning there is an warning!)@echo "這里會(huì)執(zhí)行!"# bash 中執(zhí)行 make $ make Makefile:2: there is an warning! 這里會(huì)執(zhí)行!原創(chuàng)參見(jiàn)https://blog.csdn.net/mxss343314287/article/details/112004808
總結(jié)
以上是生活随笔為你收集整理的Makefile教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: tiny4412 linux+qtopi
- 下一篇: 移动端阻止body左右偏移