怎么在Linux中使用Make命令-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)怎么在Linux中使用Make 命令,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

在許昌等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需求定制設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,全網(wǎng)整合營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,許昌網(wǎng)站建設(shè)費(fèi)用合理。

Linux 下 make 命令是系統(tǒng)管理員和程序員用的最頻繁的命令之一。管理員用它通過命令行來編譯和安裝很多開源的工具,程序員用它來管理他們大型復(fù)雜的項(xiàng)目編譯問題。

Make 如何工作的

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

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


Make 命令實(shí)例

下面是本文所使用的測試環(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)用的實(shí)例:

1. 一個(gè)簡單的例子

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

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

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

如果你再次查看目錄內(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 重新編譯了,然而另一個(gè) Test.o 沒有重新編譯。

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

$ make clean
rm -rf *.o test

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

你可以看到所有的 .o 文件和執(zhí)行文件 test 都被刪除了。

2. 通過 -B 選項(xiàng)讓所有目標(biāo)總是重新建立

到目前為止,你可能注意到 make 命令不會編譯那些自從上次編譯之后就沒有更改的文件,但是,如果你想覆蓋 make 這種默認(rèn)的行為,你可以使用 -B 選項(xiàng)。

下面是個(gè)例子:

$ 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 會強(qiáng)制編譯所有的目標(biāo)文件以及最終的執(zhí)行文件。

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

如果你想知道 make 執(zhí)行時(shí)實(shí)際做了什么,使用 -d 選項(xiàng)。

這是一個(gè)例子:

$ 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 選項(xiàng)改變目錄

你可以為 make 命令提供不同的目錄路徑,在尋找 Makefile 之前會切換目錄的。

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

$ ls 
file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt
file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt

但是你想運(yùn)行的 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 選項(xiàng)將其它文件看作 Makefile

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

make -f my_makefile

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


以上就是怎么在Linux中使用Make 命令,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享文章:怎么在Linux中使用Make命令-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://www.muchs.cn/article2/dhcgic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、定制開發(fā)、品牌網(wǎng)站制作自適應(yīng)網(wǎng)站、營銷型網(wǎng)站建設(shè)、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)