2015年4月6日 星期一

Makefile

ref: http://tetralet.luna.com.tw/?op=ViewArticle&articleId=185

Makefile語法:

● 語法:

target: dependencies
<tab>Commands

target:  要建立的檔案
  make在編譯時,會比較檔案時間,決定是否重新建立target
  若該項目並非檔案,則為fake項目,而不會建立target檔案。(利用 .PHONY來指定fake項目)

dependencies: make根據這些項目決定是否重新編譯
  建立target前,必定先檢查的項目。可以不指定。

commands: 建立的指令
  必定以 \Tab 開頭(Tab開頭的都會被視為Shell script)
  每條command會啟動一個新的Shell,可以用; 將指令寫在同一行,或是 &&

● PHONY:

.PHONY: clean
clean:
<tab>rm *.o

  不論檔案時間必定執行,不會建立target

● 特別字元:

  @: 不顯示執行的命令
  -:   即使指令出錯,也不中斷make

.PHONY: clean
clean:
    @echo "Clean..."
    -rm *.o

● 隱性法則:

foo.o: common.h
    gcc -c foo.c

由於產生foo.o的指令就是foo.c,因此在make中可以簡化為:

foo.o: common.h

也可以用空白指令避免make利用隱性法則編譯
foo.o: common.h
<tab>

● 註解:

以#開頭即為註解

● 變數宣告:(macro)

利用=指定
target = foo
$(target): command
<tab>gcc -o $(target) foo.c

●Make參數:

可以用參數蓋過makefile裡的參數
make CFLAGS="-g -O2"

可以在make後接要重新建立的target
make clean



沒有留言:

張貼留言