Makefile 相關知識

Makefile的用處

        通常用於編譯程式,尤其大型的專案 (沒有makefile一行一行打gcc 的話會瘋掉)make 有他的語言可以幫你叫shell做事,可以幫你檢查文件的相依關係,並執行你規定的命令。

Makefile 用法

再你要執行命令的目錄下,撰寫一個檔案檔案名取為"makefile"或"Makfil"。並依照makefile的語法,規定檔案的相依性和要執行的命令,之後再命令列輸入make,他就會依照你Makefile中規定的規則做事。

Makefile 語法

基本語法

target1 target2 target3 : prerequisite1 prerequisite2command1command2target:目標

目標可以是檔名或者是一個代表動作的識別符號,如果不是檔名的Target叫 phony target。make根據指定的target來做相關動作。

prerequisite:必要條件

檢查目標檔案之前目標檔案必須先檢查這些檔案

command: 命令

  • l 命令就是要完成一個目標所要做的動作, 有幾個比較重要的規定要清楚 command 前面一定要是個TAB鍵。不可以用空白鍵。

  • l 每一行的命令其實都是喚起一個sub shell來執行命令,做完了, 這個sub shell就沒有了。


EX:

prog: main.o Point.o Rectangle.og++ -o prog main.o Point.o Rectangle.omain.o: main.cpp Point.h Rectangle.hg++ -c main.cpp

Point.o: Point.cpp Point.h

g++ -c Point.cpp

Rectangle.o: Rectangle.cpp Rectangle.h

g++ -c Rectangle.cpp
參考資料

Makefile的撰寫 http://www.study-area.org/cyril/opentools/opentools/makefile.html

留言

這個網誌中的熱門文章

電腦網路project