前言
GNU make 的編寫(編程)非常類似于元編程(metaprogramming),整個MAKE 的運行分成2部分:
- 生成規(guī)則(rule):
- 執(zhí)行規(guī)則(rule) :
什么是規(guī)則(rule)
規(guī)則是MAKE 的核心,驅(qū)動MAKE執(zhí)行的pump。</p>
規(guī)則(rule) 范式<
Target: Prerequisites
Recipes
可以理解成
目標文件(target)的生成依賴于Prerequisites,并結(jié)合命令集(Recipes)生成。
- Prerequisites 可以為空
- Recipes 可以為空
簡單例子
out.text:in.txt
cp in.txt out.txt
如果out.txt 文件不存在,且in.txt文件存在,那么執(zhí)行recipe(cp)
out.txt 文件生成依賴in.txt,這里通過cp來生成out.txt
如果out.txt 文件已經(jīng)存在,且in.txt沒有發(fā)生變化,那么cp的命令就不會再執(zhí)行。
編譯流程
compile-diagram.jpg
調(diào)試工具
- remake
非常強大,類似GDB,支持單步,斷點。 - gmd: 侵入式,效果未知,因為remake已經(jīng)足夠好
官方文檔
Q&A
- GNU make will remove the intermediate files which generated by pattern rule
- assigning-variable-value-inside-recipe
- Get the Top dir of the makesystem
通常大型軟件系統(tǒng)會有復(fù)雜的makesystem,把make相關(guān)的腳本文件統(tǒng)一放置,便于其他模塊的引用。常見是分模塊include makesystem中的common makefile,這個時候,makesystem中的makefile需要知道自己的絕對路徑,便于資源路徑的定位,common-gnu-makefile-directory-path - phony target
A phony target should not be a prerequisite of a real target file; if it is, its recipe will be run every time make goes to update that file. As long as a phony target is never a prerequisite of a real target, the phony target recipe will be executed only when the phony target is a specified goal (see Arguments to Specify the Goals)
- 關(guān)于并行
以下的例子無法并行,因為只有一條rule。
用$(info) 來調(diào)試makefile
subdirs: for dir in $(SUBDIRS); do \ $(MAKE) -C $$dir; \ done