makefile 工程管理

來源:互聯網
上載者:User

標籤:linux   之一   包括   說明   efi   soft   系統預設   mil   -o   

 

GNU make

Linux程式員必須學會使用GNU make來構建和管理自己的軟體工程。

GNU 的 make 能夠使整個軟體工程的編譯、連結只需要一個命令就可以完成。

 

 

Makefile

make在執行時,需要一個名為Makefile的檔案。

Makefile檔案描述了整個工程的編譯,連結等規則。

  其中包括哪些源檔案需要編譯以及如何編譯;

  需要建立哪些庫檔案以及如何建立這些庫檔案;

  如何最後產生我們想要的可執行檔。

 

 

Makefile(例子)
hello:main.o func1.o func2.o    gcc main.o func1.o func2.o -o hellomain.o:main.c    gcc -c main.cfunc1.0:func1.c    gcc -c func1.cfunc2.o:func2.c    gcc -c func2.c.PYONY:cleanclean:    rm -f hello main.o func1.o func2.o

由main.c  func1.c  func2.c檔案產生一個hello的可執行檔。

 

 

Makefile(術語)

規則:用於說明如何產生一個或多個目標檔案。

規則格式

targets : prerequisites  command
目標 : 依賴  命令
main.o : main.c  gcc -c main.c  

注意:命令需要以 [TAB] 鍵 開始。

 

 

目標

在Makefile中,規則的順序是很重要的。

Makefile中只應該有一個最終目標,其它的目標都是被整個目標所連帶出來的。

所以,一定要讓 make 知道最終目標是什麼。

一般來說,定義在Makefile 中的目標可能會有很多,但是,

第一條規則中的目標將被確立為最終目標。

 

 

檔案名稱

make 命令預設在目前的目錄下尋找名字為 makefile 或 Makefile 的工程檔案。

當名字不為這兩者之一時,可以使用 make -f 檔案名稱 來指定。

 

偽目標:

makefile 中把那些沒有任何依賴只有執行動作的目標稱為"偽目標"(phony targets)。

.PHONY : cleanclean :    rm -f hello main.o func1.o func2.o

".PHONY" 將 “clean” 目標聲明為偽目標。

 當執行make後會產生很多 *.o檔案,如果加上上面的clean,再執行  make clean  會按照上面的命令刪除指定的目標檔案。

 

變數
hello : main.o func1.o func2.o    gcc main.o func1.o func2.o -o hello

思考:如果要為 hello 目標添加一個依賴,如:func3.o,該如何修改?

答案1:

 

hello : main.o func1.o func2.o func3.o    gcc main.o func1.o func2.o func3.o -o hello

答案2:

obj = main.o func1.o func2.o func3.ohello : $(obj)    gcc $(obj) -o hello

 

 

在 makefile 中,存在系統預設的自動化變數

$^ : 代表所有的依賴檔案

[email protected] : 代表目標

$< : 代表第一個依賴檔案

例:

hello : main.o func1.o func2.o    gcc main.o func1.o func2.o -o hello

改寫成==>

hello : main.o func1.o func2.o    gcc $^ -o [email protected]

 

注釋和取消回顯

Makefile 中 “#”字元後的內容被視作注釋。

 

@:取消回顯

hello : hello.c    @gcc hello.c -o hello

 

makefile 工程管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.