首頁  >  文章  >  運維  >  linux系統的編譯指令怎麼用

linux系統的編譯指令怎麼用

PHPz
PHPz轉載
2023-05-23 17:20:092215瀏覽

linux系統的編譯指令是「Make」。在linux系統中,make是一個非常重要的編譯命令,管理員用它透過命令列來編譯和安裝很多開源的工具,程式設計師用它來管理他們大型複雜的專案編譯問題。 Make被用來自動化編譯大型程式的任務,它可以自動偵測程式中需要重新編譯的部分,並下發對應的編譯指令。

Make簡介

make 是 linux 系統的實用程式。它被用來管理大型程式的自動編譯任務,自動判斷程式的哪個部分需要重新編譯,並且傳送編譯指令。雖然,我們最常見於 C 語言程式的編譯。但是,make 不限於某一特定語言,凡是可以透過 shell 指令來執行編譯器的語言都可以使用 make 。除此之外,你甚至可以用 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 testtest.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系統的編譯指令怎麼用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除