讀書心得 跟我一起寫 makefile (4-1) makefile content 和 include

本文出自 跟我一起寫 makefile-陳皓(2005)

Chp4. makefile 總述


makefile 文件包含下列內容:

內容或規則描述
顯式規則如何生成一個或多個目標文件。由 makefile 內容明顯指出
要生成的文件,文件的相依性和生成的命令。
隱式規則make 有自動推導的功能,所以隱晦的規則可以讓我們比較粗糙
地編寫 makefile
變數定義類似 C 的 #define 會替換相對應的字串
文件指示詞make 中也有屬於 make 的指示詞,例如 include
註解"#" 號可以註解 makefile 的內容使其不會被執行

引用其它 makefile 的例子:

假設我們有個 a.mk 和我們的 makefile 放在同個路徑,其內容定義了 CC 和 OBJECTS

C = gcc
OBJECTS = Hello.o

在我們的 makefile 中 include a.mk

include a.mk

.PHONY: Hello
Hello: Hello.o
 $(CC) -o Hello $(OBJECTS)

Hello.o:

.PHONY: clean
clean:
 -rm Hello *.o

其他要注意的事是:

1. 如果執行 make 指令時,有 "-I" 或 "--include-dir" 參數,那 make 就會在这个參數所指定的路徑下去尋找。

2. 可以在 include 前面加上減號使其變成 -include 意思是 include 失敗也沒關係,繼續執行接下來的動作

留言

熱門文章