久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

linux下如何使用make命令

150次閱讀
沒有評論

共計 3247 個字符,預計需要花費 9 分鐘才能閱讀完成。

這篇“linux 下如何使用 make 命令”文章的知識點大部分人都不太理解,所以丸趣 TV 小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“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 命令”這篇文章的內容,相信大家都有了一定的了解,希望丸趣 TV 小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注丸趣 TV 行業資訊頻道。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-15發表,共計3247字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 沈丘县| 玉田县| 尉氏县| 中西区| 江达县| 额济纳旗| 盘锦市| 瑞金市| 芜湖市| 江山市| 夏邑县| 修水县| 瑞丽市| 鄄城县| 贵德县| 思南县| 承德县| 济阳县| 四子王旗| 乐昌市| 和田市| 永春县| 怀化市| 宜昌市| 德阳市| 营山县| 洛南县| 尼勒克县| 长阳| 尉犁县| 阿克| 石河子市| 锡林浩特市| 红桥区| 韩城市| 武城县| 华安县| 资阳市| 屯留县| 和田县| 鹤岗市|