linux下如何使用make指令
make 如何運作的
對於不知道背後機理的人來說,make 指令像命令列參數一樣接收目標。這些目標通常存放在以 “makefile” 來命名的特殊文件中,同時文件也包含與目標相對應的操作。更多信息,閱讀關於 makefiles 如何工作的系列文章。
當 make 指令第一次執行時,它會掃描 makefile 找到目標以及其依賴。如果這些依賴本身也是目標,請繼續為這些依賴掃描 makefile 建立其依賴關係,然後編譯它們。一旦主依賴編譯之後,然後就編譯主目標(這是透過 make 指令傳入的)。
現在,假設你對某個原始檔進行了修改,你再次執行make 命令,它將只編譯與該原始檔相關的目標文件,因此,編譯最終的可執行檔節省了大量的時間。
make 指令實例
以下是本文所使用的測試環境:
os —— ubunut 13.04 shell —— bash 4.2.45 application —— gnu make 3.81
以下是工程的內容:
$ ls anothertest.c makefile test.c test.h
下面是makefile的內容:
all: test test: test.o anothertest.o gcc -wall test.o anothertest.o -o test test.o: test.c gcc -c -wall test.c anothertest.o: anothertest.c gcc -c -wall anothertest.c clean: rm -rf *.o test
現在我們來看linux 下一些make 指令應用的實例:
1. 一個簡單的例子
為了編譯整個工程,你可以簡單的使用make 或在make 指令後帶上目標all。
$ make gcc -c -wall test.c gcc -c -wall anothertest.c gcc -wall test.o anothertest.o -o test
你能看到 make 指令第一次建立的依賴以及實際的目標。
如果你再查看目錄內容,裡面多了一些.o 檔案和執行檔:
$ ls anothertest.c anothertest.o makefile test test.c test.h test.o
現在,假設你對test.c 檔案做了一些修改,重新使用make 編譯工程:
$ make gcc -c -wall test.c gcc -wall test.o anothertest.o -o test
你可以看到只有test.o 重新編譯了,然而另一個test.o 沒有重新編譯。
現在清理所有的目標檔案和執行檔 test,你可以使用目標 clean:
$ make clean rm -rf *.o test $ ls anothertest.c makefile test.c test.h
你可以看到所有的 .o 檔案和執行檔 test 都被刪除了。
2. 透過-b 選項讓所有目標總是重新建立
#到目前為止,你可能注意到make 指令不會編譯那些自從上次編譯之後就沒有更改的文件,但是,如果你想覆蓋make 這種預設的行為,你可以使用-b 選項。
下面是個例子:
$ make make: nothing to be done for `all'. $ make -b gcc -c -wall test.c gcc -c -wall anothertest.c gcc -wall test.o anothertest.o -o test
你可以看到儘管 make 命令不會編譯任何文件,然而 make -b 會強制編譯所有的目標文件以及最終的執行文件。
3. 使用 -d 選項列印偵錯資訊
如果你想知道 make 執行時實際做了什麼,請使用 -d 選項。
這是一個例子:
$ make -d | more gnu make 3.81 copyright (c) 2006 free software foundation, inc. this is free software; see the source for copying conditions. there is no warranty; not even for merchantability or fitness for a particular purpose. this program built for x86_64-pc-linux-gnu reading makefiles… reading makefile `makefile'… updating makefiles…. considering target file `makefile'. looking for an implicit rule for `makefile'. trying pattern rule with stem `makefile'. trying implicit prerequisite `makefile.o'. trying pattern rule with stem `makefile'. trying implicit prerequisite `makefile.c'. trying pattern rule with stem `makefile'. trying implicit prerequisite `makefile.cc'. trying pattern rule with stem `makefile'. trying implicit prerequisite `makefile.c'. trying pattern rule with stem `makefile'. trying implicit prerequisite `makefile.cpp'. trying pattern rule with stem `makefile'. --more--
這是很長的輸出,你也看到我使用了 more 指令來一頁一頁顯示輸出。
4. 使用 -c 選項改變目錄
你可以提供 make 指令不同的目錄路徑,在尋找 makefile 之前會切換目錄的。
這是一個目錄,假設你就在目前目錄下:
$ ls file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt
但是你想執行的make 指令的makefile 檔案保存在../make-dir/ 目錄下,你可以這樣做:
$ make -c ../make-dir/ make: entering directory `/home/himanshu/practice/make-dir' make: nothing to be done for `all'. make: leaving directory `/home/himanshu/practice/make-dir
你能看到make 指令先切到特定的目錄下,在那執行,然後再切換回來。
5. 透過-f 選項將其它文件視為makefile
如果你想將重命名makefile 文件,例如取名為my_makefile 或其它的名字,我們想讓make 將它也當成makefile,可以使用-f 選項。
make -f my_makefile
透過這個方法,make 指令會選擇掃描 my_makefile 來取代 makefile。
以上是linux下如何使用make指令的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

在 Sublime 中運行代碼的方法有六種:通過熱鍵、菜單、構建系統、命令行、設置默認構建系統和自定義構建命令,並可通過右鍵單擊項目/文件運行單個文件/項目,構建系統可用性取決於 Sublime Text 的安裝情況。

Linux的主要用途包括:1.服務器操作系統,2.嵌入式系統,3.桌面操作系統,4.開發和測試環境。 Linux在這些領域表現出色,提供了穩定性、安全性和高效的開發工具。

要安裝 Laravel,需依序進行以下步驟:安裝 Composer(適用於 macOS/Linux 和 Windows)安裝 Laravel 安裝器創建新項目啟動服務訪問應用程序(網址:http://127.0.0.1:8000)設置數據庫連接(如果需要)

自定義開發環境的方法有很多種,但全局 Git 配置文件是最有可能用於自定義設置(例如用戶名、電子郵件、首選文本編輯器和遠程分支)的一種。以下是您需要了解的有關全局 Git 配置文件的關鍵事項。
