linux驅動學習(一) Makefile基礎__linux

來源:互聯網
上載者:User

“大計劃”需要,現要全面學習linux驅動。記錄下完整的學習過程,方便日後需要。

 

linux與windows 的C編程差異之一就是Makefile,對於學習C語言來說,起初最好是在linux環境下。

一般的程式都是由多個源檔案編譯連結而成,除了極其簡單的hello word,這些源檔案的處理步驟通常用Makefile來管理。Makefile有什麼作用呢,先編寫一個簡單的程式。

程式有三個源檔案main.c  hello.c word.c 以及各自的標頭檔main.h hello.h word.h組成

//main.c
#include"main.h"#include"hello.h"#include"word.h"void main_say(){printf("I am main!\n");}int main(void){main_say();hello_say();word_say();}

//main.h
#ifndef MAIN_H#define MAIN_Hvoid main_say();#endif



//hello.c
#include"hello.h"void hello_say(){printf("I am hello!\n");}

//hello.h
#ifndef HELLO_H#define HELLO_Hextern void hello_say();#endif


//word.c
#include"word.h"void word_say(){printf("I am word!\n");        }

//word.h
#ifndef WORD_H#define WORD_Hextern void word_say();#endif

在沒有Makefile的情況下可以這樣編譯

gcc main.c hello.c word.c -o main

但是,其中的一個源檔案如hello.c改動了,這些源檔案都得重新編譯,所以可以改成這樣編譯

$gcc  -c main.c$gcc  -c hello.c$gcc  -c word.c$gcc main.o hello.o word.o -o main

如果修改了hello.c 只要重新執行

$gcc  -c hello.c$gcc main.o hello.o word.o -o main


這樣雖然簡化了程式的編譯,但是每次都得重新輸入命令,效率低,而且很容易出錯,所以Makefile就是來解決上列問題,但不止於此。

將Makefile與原始碼放在同一目錄下

main: main.o hello.o word.ogcc main.o hello.o word.o -o mainmain.o: main.c main.h hello.h word.hgcc -c main.chello.o: hello.c hello.hgcc -c hello.cword.o: word.c word.hgcc -c word.c

make後,在終端列印了執行的命令

make命令會自動讀取目前的目錄下的Makefile


<localhost.localdomain:/data/ghostyu/linuxc> make
gcc -c main.c
gcc -c hello.c
gcc -c word.c
gcc main.o hello.o word.o -o main
<localhost.localdomain:/data/ghostyu/linuxc>
這樣每次修改源檔案後 只要make 下就可以了。
Makefile的編寫規規則為一個或多個下列命令組構成:
target ... : prerequisites ... 
cmd1
cmd2
...


如:
main: main.o hello.o word.o
gcc main.o hello.o word.o -o main


main為規則的目標(target),main.o hello.o word.o 為這條規則的條件(prerequisites)
要更新目標,必須先更新所有條件,若條件被更新,目標必備更新,更新的依據為檔案的修改時間。
所以上列Makefile中,會先執行
main.o: main.c main.h hello.h word.h
gcc -c main.c


hello.o: hello.c hello.h
gcc -c hello.c


word.o: word.c word.h
gcc -c word.c
而後才連結目標main
main: main.o hello.o word.o
gcc main.o hello.o word.o -o main


通常Makefile中會有一條clean規則,用於清除編譯過程中產生的二進位檔案,保留源檔案
在Makefile中加入clean
clean:
@echo "cleanning project"
-rm main *.o
@echo "clean completed"


命令rm前加了‘-’號,表示即使make執行這條命令出錯了,也會繼續運行,像rm,mkdir等一般會加上‘-’,因為rm的檔案可能已經被刪除,或者mkdir的目錄已經存在。


<localhost.localdomain:/data/ghostyu/linuxc> make clean
cleanning project
rm main *.o
clean completed
<localhost.localdomain:/data/ghostyu/linuxc> 


大家試試吧echo前的@符號去掉試試,結果如下
<localhost.localdomain:/data/ghostyu/linuxc> make clean
echo "cleanning project"
cleanning project
rm main *.o
echo "clean completed"
clean completed
<localhost.localdomain:/data/ghostyu/linuxc>
如果make執行的命令前面加了@字元,則不顯示命令本身而只顯示它的結果。


假如源檔案中有個叫clean.c的源檔案,然後make的時候就直接 make到這個clean源檔案了,所以Makefile中的clean部分要特殊說明下,將clean聲明為一個偽目標:
clean:
@echo "cleanning project"
-rm main *.o
@echo "clean completed"
.PHONY: clean


clean目標是一個約定俗成的名字,類似這樣約定成俗的名字還有:


install,執行編譯後的安裝工作,把可執行檔、設定檔、文檔等分別拷到不同的安裝目錄。
clean,刪除編譯產生的二進位檔案。


distclean,不僅刪除編譯產生的二進位檔案,也刪除其它產生的檔案,例如設定檔和格式轉換後的文檔,執行make distclean之後應該清除所有這些檔案,只留下源檔案。


注,Makefile檔案名稱也可以是小寫makefile,執行make時,按照 makefile,Makefile的順序找到的第一個為預設的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.