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

Fedora Makefile編譯器怎么使用

145次閱讀
沒有評論

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

這篇文章主要講解了“Fedora Makefile 編譯器怎么使用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“Fedora Makefile 編譯器怎么使用”吧!

一、Fedora Makefile 介紹

Fedora Makefile 是用于自動編譯和鏈接的,一個工程有很多文件組成,每一個文件的改變都會導致工程的重新鏈接,但是不是所有的文件都需要重新編譯,Fedora Makefile 中紀錄有文件的信息,在 make 時會決定在鏈接的時候需要重新編譯哪些文件。

Fedora Makefile 的宗旨就是:讓編譯器知道要編譯一個文件需要依賴其他的哪些文件。當那些依賴文件有了改變,編譯器會自動的發現最終的生成文件已經過時,而重新編譯相應的模塊。

Fedora Makefile 的基本結構不是很復雜,但當一個程序開發人員開始寫 Fedora Makefile 時,經常會懷疑自己寫的是否符合慣例,而且自己寫的 Makefile 經常和自己的開發環境相關聯,當系統環境變量或路徑發生了變化后,Fedora Makefile 可能還要跟著修改。這樣就造成了手工書寫 Fedora Makefile 的諸多問題,automake 恰好能很好地幫助我們解決這些問題。

使用 automake,程序開發人員只需要寫一些簡單的含有預定義宏的文件,由 autoconf 根據一個宏文件生成 configure,由 automake 根據另一個宏文件生成 Makefile.in,再使用 configure 依據 Makefile.in 來生成一個符合慣例的 Fedora Makefile。下面我們將詳細介紹 Fedora Makefile 的 automake 生成方法。

二、使用的環境

本文所提到的程序是基于 Linux 發行版本:Fedora Core release 1,它包含了我們要用到的 autoconf,automake。

三、從 helloworld 入手

我們從大家最常使用的例子程序 helloworld 開始。下面的過程如果簡單地說來就是:新建三個文件:helloworld.cconfigure.inMakefile.am 然后執行:autoscan; aclocal; autoconf; automake –add-missing; ./configure; make; ./helloworld; 就可以看到 Fedora Makefile 被產生出來,而且可以將 helloworld.c 編譯通過。很簡單吧,幾條命令就可以做出一個符合慣例的 Fedora Makefile,感覺如何呀。現在開始介紹詳細的過程:

1、建目錄

在你的工作目錄下建一個 helloworld 目錄,我們用它來存放 helloworld 程序及相關文件,如在 /home/my/build 下:

$ mkdir helloword
$ cd helloworld

2、helloworld.c

然后用你自己最喜歡的編輯器寫一個 hellowrold.c 文件,如命令:vi helloworld.c。使用下面的代碼作為 helloworld.c 的內容。
int main(int argc, char** argv)
{printf( Hello, Linux World!
return 0;}

完成后保存退出。現在在 helloworld 目錄下就應該有一個你自己寫的 helloworld.c 了。

3、生成 configure

我們使用 autoscan 命令來幫助我們根據目錄下的源代碼生成一個 configure.in 的模板文件。

命令:

$ autoscan
$ ls
configure.scan helloworld.c 執行后在 hellowrold 目錄下會生成一個文件:configure.scan,我們可以拿它作為 configure.in 的藍本。

4,生成 configure.in

現在將 configure.scan 改名為 configure.in,并且編輯它,按下面的內容修改,去掉無關的語句:

Code

============================configure.in 內容開始 ========================================= # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_INIT(helloworld.c) AM_INIT_AUTOMAKE(helloworld, 1.0) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_OUTPUT(Makefile) ============================configure.in 內容結束 =========================================

5 執行 aclocal 和 autoconf

然后執行命令 aclocal 和 autoconf,分別會產生 aclocal.m4 及 configure 兩個文件:

Code $ aclocal $ls aclocal.m4 configure.in helloworld.c $ autoconf $ ls aclocal.m4 autom4te.cache configure configure.in helloworld.c

大家可以看到 configure.in 內容是一些宏定義,這些宏經 autoconf 處理后會變成檢查系統特性、環境變量、軟件必須的參數的 shell 腳本。autoconf 是用來生成自動配置軟件源代碼腳本(configure)的工具。configure 腳本能獨立于 autoconf 運行,且在運行的過程中,不需要用戶的干預。

要生成 configure 文件,你必須告訴 autoconf 如何找到你所用的宏。方式是使用 aclocal 程序來生成你的 aclocal.m4。aclocal 根據 configure.in 文件的內容,自動生成 aclocal.m4 文件。aclocal 是一個 perl 腳本程序,它的定義是:“aclocal – create aclocal.m4 by scanning configure.ac”。

autoconf 從 configure.in 這個列舉編譯軟件時所需要各種參數的模板文件中創建 configure。autoconf 需要 GNU m4 宏處理器來處理 aclocal.m4,生成 configure 腳本。

m4 是一個宏處理器。將輸入拷貝到輸出,同時將宏展開。宏可以是內嵌的,也可以是用戶定義的。除了可以展開宏,m4 還有一些內建的函數,用來引用文件,執行命令,整數運算,文本操作,循環等。m4 既可以作為編譯器的前端,也可以單獨作為一個宏處理器.

6、新建 Makefile.am

新建 Makefile.am 文件,命令:$ vi Makefile.am 內容如下:

Code AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=helloworld helloworldhelloworld_SOURCES=helloworld.c

automake 會根據你寫的 Makefile.am 來自動生成 Makefile.in。Makefile.am 中定義的宏和目標, 會指導 automake 生成指定的代碼。例如,宏 bin_PROGRAMS 將導致編譯和連接的目標被生成。

7、運行 automake

命令:

Code $ automake --add-missing configure.in: installing `./install-sh  configure.in: installing `./mkinstalldirs  configure.in: installing `./missing  Makefile.am: installing `./depcomp  automake 會根據 Makefile.am 文件產生一些文件,包含最重要的 Makefile.in。

8、執行 configure 生成 Fedora Makefile

Code $ ./configure checking for a BSD-compatible install /usr/bin/install -c checking whether build environment is sane yes checking for gawk gawk checking whether make sets $(MAKE) yes checking for gcc gcc checking for C compiler default output a.out checking whether the C compiler works yes checking whether we are cross compiling no checking for suffix of executables checking for suffix of object files o checking whether we are using the GNU C compiler yes checking whether gcc accepts -g yes checking for gcc option to accept ANSI C none needed checking for style of include used by make GNU checking dependency style of gcc gcc3 configure: creating ./config.status config.status: creating Makefile config.status: executing depfiles commands

$ ls -l Makefile-rw-rw-r– 1 yutao yutao 15035 Oct 15 10:40 Fedora Makefile 你可以看到,此時 Fedora Makefile 已經產生出來了。

9、使用 Fedora Makefile 編譯代碼

Code$ make if gcc -DPACKAGE_NAME=  -DPACKAGE_TARNAME=  -DPACKAGE_VERSION=  - DPACKAGE_STRING=  -DPACKAGE_BUGREPORT=  -DPACKAGE= helloworld  -DVERSION= 1.0  -I. -I. -g -O2 -MT helloworld.o -MD -MP -MF  .deps/helloworld.Tpo  -c -o helloworld.o `test -f  helloworld.c  || echo  ./ `helloworld.c; then mv -f  .deps/helloworld.Tpo   .deps/helloworld.Po  else rm -f  .deps/helloworld.Tpo  exit 1; fi gcc -g -O2 -o helloworld helloworld.o

10,運行 helloworld

Code
$ ./helloworld
Hello, Linux World!

這樣 helloworld 就編譯出來了,你如果按上面的步驟來做的話,應該也會很容易地編譯出正確的 helloworld 文件。你還可以試著使用一些其他的 make 命令,如 make clean,make install,make dist,看看它們會給你什么樣的效果。感覺如何?自己也能寫出這么專業的 Fedora Makefile,老板一定會對你刮目相看。

四、深入淺出

針對上面提到的各個命令,我們再做些詳細的介紹。

1、autoscan

autoscan 是用來掃描源代碼目錄生成 configure.scan 文件的。autoscan 可以用目錄名做為參數,但如果你不使用參數的話,那么 autoscan 將認為使用的是當前目錄。autoscan 將掃描你所指定目錄中的源文件,并創建 configure.scan 文件。

2、configure.scan

configure.scan 包含了系統配置的基本選項,里面都是一些宏定義。我們需要將它改名為 configure.in

3、aclocal

aclocal 是一個 perl 腳本程序。aclocal 根據 configure.in 文件的內容,自動生成 aclocal.m4 文件。aclocal 的定義是:“aclocal – create aclocal.m4 by scanning configure.ac”。

4、autoconf

使用 autoconf,根據 configure.in 和 aclocal.m4 來產生 configure 文件。configure 是一個腳本,它能設置源程序來適應各種不同的操作系統平臺,并且根據不同的系統來產生合適的 Fedora Makefile,從而可以使你的源代碼能在不同的操作系統平臺上被編譯出來。

configure.in 文件的內容是一些宏,這些宏經過 autoconf 處理后會變成檢查系統特性、環境變量、軟件必須的參數的 shell 腳本。configure.in 文件中的宏的順序并沒有規定,但是你必須在所有宏的最前面和 *** 面分別加上 AC_INIT 宏和 AC_OUTPUT 宏。

在 configure.ini 中:# 號表示注釋,這個宏后面的內容將被忽略。AC_INIT(FILE) 這個宏用來檢查源代碼所在的路徑。AM_INIT_AUTOMAKE(PACKAGE, VERSION) 這個宏是必須的,它描述了我們將要生成的軟件包的名字及其版本號:PACKAGE 是軟件包的名字,VERSION 是版本號。當你使用 make dist 命令時,它會給你生成一個類似 helloworld-1.0.tar.gz 的軟件發行包,其中就有對應的軟件包的名字和版本號。

AC_PROG_CC 這個宏將檢查系統所用的 C 編譯器。AC_OUTPUT(FILE) 這個宏是我們要輸出的 Fedora Makefile 的名字。我們在使用 automake 時,實際上還需要用到其他的一些宏,但我們可以用 aclocal 來幫我們自動產生。執行 aclocal 后我們會得到 aclocal.m4 文件。產生了 configure.in 和 aclocal.m4 兩個宏文件后,我們就可以使用 autoconf 來產生 configure 文件了。

5、Makefile.am

Makefile.am 是用來生成 Makefile.in 的,需要你手工書寫。Makefile.am 中定義了一些內容:AUTOMAKE_OPTIONS 這個是 automake 的選項。在執行 automake 時,它會檢查目錄下是否存在標準 GNU 軟件包中應具備的各種文件,例如 AUTHORS、ChangeLog、NEWS 等文件。我們將其設置成 foreign 時,automake 會改用一般軟件包的標準來檢查。

bin_PROGRAMS 這個是指定我們所要產生的可執行文件的文件名。如果你要產生多個可執行文件,那么在各個名字間用空格隔開。helloworld_SOURCES 這個是指定產生“helloworld”時所需要的源代碼。如果它用到了多個源文件,那么請使用空格符號將它們隔開。比如需要 helloworld.h,helloworld.c 那么請寫成 helloworld_SOURCES= helloworld.h helloworld.c。

如果你在 bin_PROGRAMS 定義了多個可執行文件,則對應每個可執行文件都要定義相對的 filename_SOURCES。

6、automake

我們使用 automake,根據 configure.in 和 Makefile.am 來產生 Makefile.in。選項 –add-missing 的定義是“add missing standard files to package”,它會讓 automake 加入一個標準的軟件包所必須的一些文件。

我們用 automake 產生出來的 Makefile.in 文件是符合 GNU Makefile 慣例的,接下來我們只要執行 configure 這個 shell 腳本就可以產生合適的 Fedora Makefile 文件了。

7、Fedora Makefile

在符合 GNU Makefiel 慣例的 Fedora Makefile 中,包含了一些基本的預先定義的操作:make 根據 Fedora Makefile 編譯源代碼,連接,生成目標文件,可執行文件。make clean 清除上次的 make 命令所產生的 object 文件(后綴為“.o”的文件)及可執行文件。

make install 將編譯成功的可執行文件安裝到系統目錄中,一般為 /usr/local/bin 目錄。make dist 產生發布軟件包文件(即 distribution package)。這個命令將會將可執行文件及相關文件打包成一個 tar.gz 壓縮的文件用來作為發布軟件的軟件包。它會在當前目錄下生成一個名字類似“PACKAGE-VERSION.tar.gz”的文件。PACKAGE 和 VERSION,是我們在 configure.in 中定義的 AM_INIT_AUTOMAKE(PACKAGE, VERSION)。

make distcheck 生成發布軟件包并對其進行測試檢查,以確定發布包的正確性。這個操作將自動把壓縮包文件解開,然后執行 configure 命令,并且執行 make,來確認編譯不出現錯誤,*** 提示你軟件包已經準備好,可以發布了。make distclean 類似 make clean,但同時也將 configure 生成的文件全部刪除掉,包括 Fedora Makefile。

感謝各位的閱讀,以上就是“Fedora Makefile 編譯器怎么使用”的內容了,經過本文的學習后,相信大家對 Fedora Makefile 編譯器怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-16發表,共計7497字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 三台县| 福安市| 阳信县| 平原县| 东乡| 竹溪县| 三穗县| 罗平县| 花垣县| 清远市| 海安县| 长治市| 酉阳| 惠来县| 托克逊县| 拉孜县| 大庆市| 平南县| 江达县| 岳西县| 武宣县| 六枝特区| 永春县| 平南县| 富民县| 年辖:市辖区| 富裕县| 宁乡县| 光山县| 石棉县| 修文县| 红河县| 万山特区| 三台县| 溧阳市| 通化县| 深泽县| 文山县| 即墨市| 肇源县| 凤凰县|