自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux Makefile介紹自動編譯和鏈接

運維 系統(tǒng)運維
而且自己寫的Linux Makefile經(jīng)常和自己的開發(fā)環(huán)境相關(guān)聯(lián),當系統(tǒng)環(huán)境變量或路徑發(fā)生了變化后,Linux Makefile可能還要跟著修改。這樣就造成了手工書寫Linux Makefile的諸多問題,automake恰好能很好地幫助我們解決這些問題。

Linux Makefile還是比較常用的,于是我研究了一下Linux Makefile,在這里拿出來和大家分享一下,希望對大家有用。作為Linux下的程序開發(fā)人員,一定都遇到過Linux Makefile,用make命令來編譯自己寫的程序確實是很方便。一般情況下,大家都是手工寫一個簡單Linux Makefile,如果要想寫出一個符合自由軟件慣例的Linux Makefile就不那么容易了。

在本文中,將介紹如何使用autoconf和automake兩個工具來幫助我們自動地生成符合自由軟件慣例的Linux Makefile,這樣就可以象常見的GNU程序一樣,只要使用“./configure”,“make”,“make instal”就可以把程序安裝到Linux系統(tǒng)中去了。這將特別適合想做開放源代碼軟件的程序開發(fā)人員,又或如果你只是自己寫些小的Toy程序,那么這個文章對你也會有很大的幫助。

一、Linux Makefile介紹

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

Linux Makefile的宗旨就是:讓編譯器知道要編譯一個文件需要依賴其他的哪些文件。當那些依賴文件有了改變,編譯器會自動的發(fā)現(xiàn)最終的生成文件已經(jīng)過時,而重新編譯相應的模塊。 Linux Makefile的基本結(jié)構(gòu)不是很復雜,但當一個程序開發(fā)人員開始寫Linux Makefile時,經(jīng)常會懷疑自己寫的是否符合慣例.

而且自己寫的Linux Makefile經(jīng)常和自己的開發(fā)環(huán)境相關(guān)聯(lián),當系統(tǒng)環(huán)境變量或路徑發(fā)生了變化后,Linux Makefile可能還要跟著修改。這樣就造成了手工書寫Linux Makefile的諸多問題,automake恰好能很好地幫助我們解決這些問題。

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

二、使用的環(huán)境

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

三、從helloworld入手

我們從大家最常使用的例子程序helloworld開始。 下面的過程如果簡單地說來就是: 新建三個文件:
helloworld.c
configure.in
Linux Makefile.am

然后執(zhí)行: aclocal; autoconf; automake --add-missing; ./configure; make; ./helloworld  就可以看到Linux Makefile被產(chǎn)生出來,而且可以將helloworld.c編譯通過。
很簡單吧,幾條命令就可以做出一個符合慣例的Linux Makefile,感覺如何呀。 現(xiàn)在開始介紹詳細的過程:

1、建目錄 在你的工作目錄下建一個helloworld目錄,我們用它來存放helloworld程序及相關(guān)文件,如在/home/my/build下:
$ mkdir helloword
$ cd helloworld 

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

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

3、生成configure

我們使用autoscan命令來幫助我們根據(jù)目錄下的源代碼生成一個configure.in的模板文件。 命令:
$ autoscan
$ ls
configure.scan helloworld.c 

執(zhí)行后在hellowrold目錄下會生成一個文件:configure.scan,我們可以拿它作為configure.in的藍本。 現(xiàn)在將configure.scan改名為configure.in,并且編輯它,按下面的內(nèi)容修改,去掉無關(guān)的語句:

  1. ============================configure.in內(nèi)容開始=========================================   
  2. # -*- Autoconf -*-   
  3. # Process this file with autoconf to produce a configure script.   
  4. AC_INIT(helloworld.c)   
  5. AM_INIT_AUTOMAKE(helloworld, 1.0)   
  6. # Checks for programs.   
  7. AC_PROG_CC   
  8. # Checks for libraries.   
  9. # Checks for header files.   
  10. # Checks for typedefs, structures, and compiler characteristics.   
  11. # Checks for library functions.   
  12. AC_OUTPUT(Linux Makefile)   
  13. ============================configure.in內(nèi)容結(jié)束=========================================   


然后執(zhí)行命令aclocal和autoconf,分別會產(chǎn)生aclocal.m4及configure兩個文件:

  1. $ aclocal    
  2. $ls    
  3. aclocal.m4 configure.in helloworld.c    
  4. $ autoconf    
  5. $ ls    
  6. aclocal.m4 autom4te.cache configure configure.in helloworld.c   

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

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

autoconf從configure.in這個列舉編譯軟件時所需要各種參數(shù)的模板文件中創(chuàng)建configure。 autoconf需要GNU m4宏處理器來處理aclocal.m4,生成configure腳本。 m4是一個宏處理器。將輸入拷貝到輸出,同時將宏展開。

宏可以是內(nèi)嵌的,也可以是用戶定義的。除了可以展開宏,m4還有一些內(nèi)建的函數(shù),用來引用文件,執(zhí)行命令,整數(shù)運算,文本操作,循環(huán)等。m4既可以作為編譯器的前端,也可以單獨作為一個宏處理器。

4、新建Linux Makefile.am 新建Linux Makefile.am文件,命令: $ vi Linux Makefile.am 內(nèi)容如下:

  1. AUTOMAKE_OPTIONS=foreign   
  2. bin_PROGRAMS=helloworld   
  3. helloworldhelloworld_SOURCES=helloworld.c    
  4. automake會根據(jù)你寫的Linux Makefile.am來自動生成Linux Makefile.in。 

Linux Makefile.am中定義的宏和目標,會指導automake生成指定的代碼。例如,宏bin_PROGRAMS將導致編譯和連接的目標被生成。

5、運行automake
命令:

  1. $ automake --add-missing   
  2. configure.in: installing `./install-sh'   
  3. configure.in: installing `./mkinstalldirs'   
  4. configure.in: installing `./missing'   
  5. Linux Makefile.am: installing `./depcomp'  


automake會根據(jù)Linux Makefile.am文件產(chǎn)生一些文件,包含最重要的Linux Makefile.in。

6、執(zhí)行configure生成Linux Makefile

  1. $ ./configure    
  2. checking for a BSD-compatible install... /usr/bin/install -c   
  3. checking whether build environment is sane... yes   
  4. checking for gawk... gawk   
  5. checking whether make sets $(MAKE)... yes   
  6. checking for gcc... gcc   
  7. checking for C compiler default output... a.out   
  8. checking whether the C compiler works... yes   
  9. checking whether we are cross compiling... no   
  10. checking for suffix of executables...    
  11. checking for suffix of object files... o   
  12. checking whether we are using the GNU C compiler... yes   
  13. checking whether gcc accepts -g... yes   
  14. checking for gcc option to accept ANSI C... none needed   
  15. checking for style of include used by make... GNU   
  16. checking dependency style of gcc... gcc3   
  17. configure: creating ./config.status   
  18. config.status: creating Linux Makefile   
  19. config.status: executing depfiles commands   
  20. $ ls -l Linux Makefile   
  21. -rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Linux Makefile   

你可以看到,此時Linux Makefile已經(jīng)產(chǎn)生出來了。

7、使用Linux Makefile編譯代碼

  1. $ make   
  2. if gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -   
  3. DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="helloworld" -DVERSION="1.0"   
  4. -I. -I. -g -O2 -MT helloworld.o -MD -MP -MF ".deps/helloworld.Tpo"   
  5. -c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c;    
  6. then mv -f ".deps/helloworld.Tpo" ".deps/helloworld.Po";    
  7. else rm -f ".deps/helloworld.Tpo"; exit 1;    
  8. fi   
  9. gcc -g -O2 -o helloworld helloworld.o    
  10. 運行helloworld   
  11. $ ./helloworld    
  12. Hello, Linux World!   

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

四、深入淺出

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

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

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

3、 aclocal
aclocal是一個perl 腳本程序。aclocal根據(jù)configure.in文件的內(nèi)容,自動生成aclocal.m4文件。aclocal的定義是:“aclocal - create aclocal.m4 by scanning configure.ac”。

4、 autoconf
autoconf是用來產(chǎn)生configure文件的。configure是一個腳本,它能設置源程序來適應各種不同的操作系統(tǒng)平臺,并且根據(jù)不同的系統(tǒng)來產(chǎn)生合適的Linux Makefile,從而可以使你的源代碼能在不同的操作系統(tǒng)平臺上被編譯出來。

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

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

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

5、 Linux Makefile.am

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

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

6、 automake

我們使用automake --add-missing來產(chǎn)生Linux Makefile.in。 選項--add-missing的定義是“add missing standard files to package”,它會讓automake加入一個標準的軟件包所必須的一些文件。 我們用automake產(chǎn)生出來的Linux Makefile.in文件是符合GNU Linux Makefile慣例的,接下來我們只要執(zhí)行configure這個shell 腳本就可以產(chǎn)生合適的 Linux Makefile 文件了。

7、 Linux Makefile

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

make install 將編譯成功的可執(zhí)行文件安裝到系統(tǒng)目錄中,一般為/usr/local/bin目錄。 make dist 產(chǎn)生發(fā)布軟件包文件(即distribution package)。這個命令將會將可執(zhí)行文件及相關(guān)文件打包成一個tar.gz壓縮的文件用來作為發(fā)布軟件的軟件包。

它會在當前目錄下生成一個名字類似“PACKAGE-VERSION.tar.gz”的文件。PACKAGE和VERSION,是我們在configure.in中定義的AM_INIT_AUTOMAKE(PACKAGE, VERSION)。
make distcheck 生成發(fā)布軟件包并對其進行測試檢查,以確定發(fā)布包的正確性。這個操作將自動把壓縮包文件解開,然后執(zhí)行configure命令,并且執(zhí)行make,來確認編譯不出現(xiàn)錯誤,最后提示你軟件包已經(jīng)準備好,可以發(fā)布了。

helloworld-1.0.tar.gz is ready for distribution make distclean  類似make clean,但同時也將configure生成的文件全部刪除掉,包括Linux Makefile。

五、結(jié)束語

通過上面的介紹,你應該可以很容易地生成一個你自己的符合GNU慣例的Linux Makefile文件及對應的項目文件。 如果你想寫出更復雜的且符合慣例的Linux Makefile,你可以參考一些開放代碼的項目中的configure.in和Linux Makefile.am文件,比如:嵌入式數(shù)據(jù)庫sqlite,單元測試cppunit。

【編輯推薦】

  1. Linux Makefile系統(tǒng)的真相符合自由軟件慣例
  2. Linux Makefile自動生成的運行步驟
  3. Linux Makefile介紹自動編譯和鏈接
  4. Linux Makefile介紹使用的環(huán)境深入淺出
  5. 闡述Linux Makefile文件概念
責任編輯:佚名 來源: CSDN
相關(guān)推薦

2010-02-25 15:11:48

Linux Makef

2010-02-24 16:01:39

Linux Makef

2010-06-22 17:09:52

Linux Autom

2010-03-01 09:57:55

Linux Makef

2010-02-26 10:47:30

Linux Makef

2010-06-22 15:24:11

autoconf安裝

2010-02-24 14:55:35

Linux Makef

2010-06-22 15:26:58

autoconf安裝

2010-06-22 15:13:32

autoconf安裝

2013-11-14 11:38:20

Linux Kerne編譯

2009-12-23 16:28:13

Linux GCC

2009-12-23 10:33:52

Linux操作系統(tǒng)

2009-12-24 10:04:38

Linux進行C編譯

2017-09-12 09:22:51

LinuxMakefileautoconf工具

2017-01-15 15:27:51

Linux軟連接和硬鏈接

2022-02-28 09:44:09

Linux硬鏈接軟鏈接

2009-10-26 13:45:39

linux Makef

2023-11-15 08:27:46

Linux系統(tǒng)

2009-12-08 12:22:05

內(nèi)核Makefile軟鏈接

2010-05-28 14:55:17

Linux編程工具
點贊
收藏

51CTO技術(shù)棧公眾號