濮阳杆衣贸易有限公司

主頁 > 知識庫 > Linux 下 Make 命令實例詳解

Linux 下 Make 命令實例詳解

熱門標簽:依蘭縣地圖標注app 百度中國地圖標注中心 山東企業(yè)電銷機器人價格 潮州地圖標注 安陽百應(yīng)電銷機器人加盟 公司400電話辦理價格 琿春市地圖標注app 百度地圖標注開鎖電話 pageadm實現(xiàn)地圖標注

Linux 下 make 命令是系統(tǒng)管理員和程序員用的最頻繁的命令之一。管理員用它通過命令行來編譯和安裝很多開源的工具,程序員用它來管理他們大型復(fù)雜的項目編譯問題。本文我們將用一些實例來討論 make 命令背后的工作機制。

Make 如何工作的

對于不知道背后機理的人來說,make 命令像命令行參數(shù)一樣接收目標。這些目標通常存放在以 “Makefile” 來命名的特殊文件中,同時文件也包含與目標相對應(yīng)的操作。更多信息,閱讀關(guān)于 Makefiles 如何工作的系列文章。

當 make 命令第一次執(zhí)行時,它掃描 Makefile 找到目標以及其依賴。如果這些依賴自身也是目標,繼續(xù)為這些依賴掃描 Makefile 建立其依賴關(guān)系,然后編譯它們。一旦主依賴編譯之后,然后就編譯主目標(這是通過 make 命令傳入的)。

現(xiàn)在,假設(shè)你對某個源文件進行了修改,你再次執(zhí)行 make 命令,它將只編譯與該源文件相關(guān)的目標文件,因此,編譯完最終的可執(zhí)行文件節(jié)省了大量的時間。

Make 命令實例

下面是本文所使用的測試環(huán)境:

OS —— Ubunut 13.04
Shell —— Bash 4.2.45
Application —— GNU Make 3.81

下面是工程的內(nèi)容:

$ ls 
anotherTest.c Makefile test.c test.h

下面是 Makefile 的內(nèi)容:

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

現(xiàn)在我們來看 Linux 下一些 make 命令應(yīng)用的實例:

1. 一個簡單的例子

為了編譯整個工程,你可以簡單的使用 make 或者在 make 命令后帶上目標 all。

$ make 
gcc -c -Wall test.c 
gcc -c -Wall anotherTest.c 
gcc -Wall test.o anotherTest.o -o test

你能看到 make 命令第一次創(chuàng)建的依賴以及實際的目標。

如果你再次查看目錄內(nèi)容,里面多了一些 .o 文件和執(zhí)行文件:

$ ls 
anotherTest.c anotherTest.o Makefile test test.c test.h test.o

現(xiàn)在,假設(shè)你對 test.c 文件做了一些修改,重新使用 make 編譯工程:

$ make 
gcc -c -Wall test.c 
gcc -Wall test.o anotherTest.o -o test

你可以看到只有 test.o 重新編譯了,然而另一個 Test.o 沒有重新編譯。

現(xiàn)在清理所有的目標文件和可執(zhí)行文件 test,你可以使用目標 clean:

$ make clean
rm -rf *.o test

$ ls
anotherTest.c Makefile test.c test.h

你可以看到所有的 .o 文件和執(zhí)行文件 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 會強制編譯所有的目標文件以及最終的執(zhí)行文件。

3. 使用 -d 選項打印調(diào)試信息

如果你想知道 make 執(zhí)行時實際做了什么,使用 -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 之前會切換目錄的。

這是一個目錄,假設(shè)你就在當前目錄下:

$ 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 命令首先切到特定的目錄下,在那執(zhí)行,然后再切換回來。

5. 通過 -f 選項將其它文件看作 Makefile

如果你想將重命名 Makefile 文件,比如取名為 my_makefile 或者其它的名字,我們想讓 make 將它也當成 Makefile,可以使用 -f 選項。

make -f my_makefile

通過這種方法,make 命令會選擇掃描 my_makefile 來代替 Makefile。

標簽:呼和浩特 公主嶺 常德 晉中 三明 林芝 香港 連云港

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Linux 下 Make 命令實例詳解》,本文關(guān)鍵詞  Linux,下,Make,命令,實例,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux 下 Make 命令實例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于Linux 下 Make 命令實例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    泾川县| 运城市| 华阴市| 如东县| 岳池县| 金华市| 静宁县| 琼海市| 镇平县| 珠海市| 芜湖县| 龙泉市| 故城县| 花垣县| 襄樊市| 修武县| 成都市| 五莲县| 高邑县| 磐安县| 内江市| 宁武县| 休宁县| 浦东新区| 且末县| 平潭县| 津市市| 鹤峰县| 南阳市| 常山县| 石柱| 灵武市| 吐鲁番市| 平山县| 苍梧县| 顺昌县| 兰西县| 黑水县| 扶风县| 崇仁县| 姚安县|