最近再看專業(yè)嵌入式軟件設計這本書,總體感覺這本書對工具的介紹非常的詳細,看了一下基本的Makefile編程,將書上的幾個重點總結一下。
首先,Makefile就是一個管理器。
1、在Makefile中主要是由規(guī)則構成。
每一個規(guī)則都是由三個部分構成:
target : depending
<Tab> command
每一個規(guī)則都是這三部分組成,當然也不一定全部存在。
2、在規(guī)則中的每一條命令,make都是在一個新的shell上運行。如果希望多個命令在同一個shell中運行,可以用“;”將這些命令連接起來。當命令很長時,可以采用“\”將一個命令分成多行書寫。
基本的實例如下:
[gong@Gong-Computer Makefile]$ ls
complicated Makefile Makefile1 simple
[gong@Gong-Computer Makefile]$ vi Makefile
- 1 .PHONY : all
- 2
- 3 all:
- 4 @mkdir test ;\
- 5 cd test ;\
- 6 mkdir subtest
- ~
[gong@Gong-Computer Makefile]$ make
[gong@Gong-Computer Makefile]$ ls
complicated Makefile Makefile1 simple test
[gong@Gong-Computer Makefile]$ cd test/
[gong@Gong-Computer test]$ ls
subtest
[gong@Gong-Computer test]$
從上面的結果可以知道,所有的操作是在同一個shell中,而不是每一個命令一個新的shell。如果將Makefile改成下面的形式就會出現(xiàn)不一樣的結果.
- 1 .PHONY : all
- 2
- 3 all:
- 4 @mkdir test
- 5 @cd test
- 6 @mkdir subtest
- ~
[gong@Gong-Computer Makefile]$ ls
complicated Makefile Makefile1 simple
[gong@Gong-Computer Makefile]$ make
[gong@Gong-Computer Makefile]$ ls
complicated Makefile Makefile1 simple subtest test
從上面的實驗效果可以知道上面的三個命令并不是在同一個shell中執(zhí)行,而是分別不同的shell,導致了與我們期望的結果存在差別。
因此在實際的多個命令時一定要記得加上";"和“\”,不然得到的結果就會出現(xiàn)異樣。
3、適當?shù)倪\用函數(shù)可以簡化Makefile的設計。主要的集合函數(shù)主要是abspath,addprefix(加前綴), addsuffix(加后綴),eval ,filter(得到某一類文件名),filter-out(去除某一類文件名),notdir(找到路勁中的文件名),patsubst(常用的替代函數(shù)),realpath,wildcard(找到當前工作目錄下的文件名或者目錄名)。
4、Makfile的設計直接決定后期修改的復雜程度,需要加強練習。