共計(jì) 3215 個字符,預(yù)計(jì)需要花費(fèi) 9 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“l(fā)inux 下怎么使用 make 命令”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
make 如何工作的
對于不知道背后機(jī)理的人來說,make 命令像命令行參數(shù)一樣接收目標(biāo)。這些目標(biāo)通常存放在以“makefile”來命名的特殊文件中,同時文件也包含與目標(biāo)相對應(yīng)的操作。更多信息,閱讀關(guān)于 makefiles 如何工作的系列文章。
當(dāng) make 命令第一次執(zhí)行時,它掃描 makefile 找到目標(biāo)以及其依賴。如果這些依賴自身也是目標(biāo),繼續(xù)為這些依賴掃描 makefile 建立其依賴關(guān)系,然后編譯它們。一旦主依賴編譯之后,然后就編譯主目標(biāo)(這是通過 make 命令傳入的)。
現(xiàn)在,假設(shè)你對某個源文件進(jìn)行了修改,你再次執(zhí)行 make 命令,它將只編譯與該源文件相關(guān)的目標(biāo)文件,因此,編譯完最終的可執(zhí)行文件節(jié)省了大量的時間。
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. 一個簡單的例子
為了編譯整個工程,你可以簡單的使用 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 重新編譯了,然而另一個 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)。
下面是個例子:
$ 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í)際做了什么,使用 -d 選項(xiàng)。
這是一個例子:
$ 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 之前會切換目錄的。
這是一個目錄,假設(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。
“l(fā)inux 下怎么使用 make 命令”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!