Linux編程基礎3 Make自動化編譯

來源:互聯網
上載者:User
一 簡介

        GNU make是一個工具程式(Utility software),經由讀取叫做“makefile”的檔案,自動化建構軟體。它是一種轉化檔案形式的工具,轉換的目標稱為“target”;與此同時,它也檢查檔案的依賴關係,如果需要的話,它會調用一些外部軟體來完成任務。它的依賴關係檢查系統非常簡單,主要根據依賴檔案的修改時間進行判斷。大多數情況下,它被用來編譯原始碼,產生結果代碼,然後把結果代碼串連起來產生可執行檔或者庫檔案。它使用叫做“makefile”的檔案來確定一個target檔案的依賴關係,然後把產生這個target的相關命令傳給shell去執行。

二 Make命令

        make: 預設使用makefile或Makefile進行編譯

        make -f [makefile_name]: 顯式指定makefile的名字

三 Makefile詳解        Makefile檔案由一個或多個規則群組成。

3.1 規則

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

        Makefile例子如下:

hello: main.o func1.o func2.o        gcc main.o func1.o func2.o -o hellomain.o: main.c        gcc -c main.cfunc1.o: func1.c        gcc -c func1.cfunc2.o: func2.c        gcc -c func2.c.PHONY: cleanclean:        rm -f hello main.o func1.o func2.o

        前兩行就是一個規則,基本形式如下:

目標:依賴        命令

         即:

targest : prerequisites        command

        註:命令需要以【TAB】鍵開始

3.2 變數

        變數的使用與shell指令碼類似:

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

        使用變數的好處在於可維護性。

3.3 預設變數

        預設變數如下:

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

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

        使用預設變數:

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

3.4 雜項

        1. #後為注釋
        2. 命令需要以【TAB】鍵開始

        3. 命令前加@,取消螢幕回顯

#makefile of helloobj=main.o func1.o func2.o func3.ohello:$(obj)        @gcc $^ -o $@

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.