Linux環境下Makefile文件製作淺談

Linux 環境下 Makefile 文件製作淺談(一)

  編寫:Leaf Zhou

  EMAIL:[email protected]

  可自由複製但禁止刪改

  2003-10-12

   無論對於一個初學者還是一個資深的Linux程序員,編寫Makefile文件都是一件很麻煩的事;再者,開發人員應該把主要的精力放在程序代碼的編寫 上,而在Makefile文件花費太多的精力顯然是不明智的;還有,對於不同的處理器架構,往往編譯器不同,環境不同,特別是一些嵌入式系統中的各種程序 的編譯,於是移植問題也使Makefile文件編寫趨於複雜,也顯得這個問題很重要。對於這些問題Linux的高手們早已想到了,所以他們開發了一些能夠 自動生成Makefile文件的工具。他們就是下面這些工具:

  〉GNU Automake

  〉GNU Autoconf

  〉GNU m4

  〉perl

  〉GNU Libtool

   因此您的操作系統必須安裝以上軟件,否則就不能夠自動生成Makefile文件或者在生成的過程中出現各種各樣的問題。用 autoconf/automake/autoheader工具來處理各種移植性的問題,用這些工具完成系統配置信息的收集,製作Makefile文件。 然後在打算編譯源碼時只需要通過 "./configure; make"這樣簡單的命令就可以得到乾淨利落的編譯。

  製作Makefile文件需要以下幾步:

  1〉建立編譯目錄和源代碼目錄及源代碼文件(即要編譯的源文件)

  [root@localhost leaf]#mkdir testmk

  [root@localhost leaf]#cd testmk

  [root@localhost testmk]#vi hello.c

  編輯hello.c文件如下內容:

  /*filename:hello.c*/

  #include <stdio.h>

  int main(int argc,char **argv)

  {

  printf("%s/n","Hello, World!")

  return 0;

  }

  2〉利用autoscan工具產生一個configure.in文件的模板文件configure.scan文件:

  [root@localhost testmk]#autoscan

  [root@localhost testmk]#ls

  configure.scan hello.c

  3〉把configure.scan文件更名爲configure.in文件,並編譯其內容如下:

  [root@localhost testmk]#mv configure.scan configure.in

  [root@localhost testmk]#vi configure.in

  dnl Process this file with autoconf to produce a configure script.

  AC_INIT(hello.c)

  dnl Add the file by leaf

  AM_INIT_AUTOMAKE(hello,1.0)

  dnl Checks for programs.

  AC_PROG_CC

  dnl Checks for libraries.

  dnl Checks for header files.

  dnl Checks for typedefs, structures, and compiler characteristics.

  dnl Checks for library functions.

  AC_OUTPUT(Makefile)

  4〉執行aclocal,會產生aclocal.m4文件

  [root@localhost testmk]#aclocal

  [root@localhost testmk]#ls

  aclocal.m4 configure.in hello.c

  5〉執行autoconf,會產生confiure文件

  [root@localhost testmk]#autoconf

  [root@localhost testmk]#ls

  aclocal.m4 [autom4te.cache] configure configure.in hello.c

  6〉創建文件Makefile.am並編輯其內容如下:

  AUTOMAKE_OPTIONS=foreign

  bin_PROGRAMS=hello

  hello_SOURCES=hello.c

  其中,hello爲編譯後產生的可執行文件名稱,而第三行等號後面爲源文件列表

  7〉執行automake程序,automake程序會根據Makefile.am產生一些文件,其中最重要的是Makefile.in文件:

  [root@localhost testmk]#automake --add-missing

  configure.in: installing `./install-sh'

  configure.in: installing `./mkinstalldirs'

  configure.in: installing `./missing'

  Makefile.am: installing `./depcomp'

  [root@localhost testmk]#ls

  aclocal.m4 [autom4te.cache] configure configure.in depcomp

  hello.c install-sh Makefile.am Makefile.in missing

  mkinstalldirs

  8〉執行configure腳本,生成我們需要的Makefile文件。

  [root@localhost testmk]#./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

  9〉最後只執行make就大功告成了:

  [root@localhost testmk]#make

  source='hello.c' object='hello.o' libtool=no /

  depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo' /

  depmode=gcc3 /bin/sh ./depcomp /

   gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c `test -f 'hello.c' || echo './'`hello.c

  gcc -g -O2 -o hello hello.o

  備註:

  1.以上內容均在RedHat Linux 9.0環境下測試通過。

  2.參考書目《Linux 程序設計權威指南》於明儉、陳向陽、方漢編著

  3.其它國內外網站資料

  4.RedHat 9.0下帶的程序文件及版本

  autoconf-2.57-3.noarch.rpm

  automake-1.6.3-5.noarch.rpm

  gcc-3.2.2-5.i386.rpm

   
  在介紹了簡單的如何產生Makefile文件之後,相信已經能夠編寫Makefile文件了,但那還遠

  遠不夠,因爲編寫那麼簡單的Makefile文件還要如此繁瑣和興師動衆,那未免小題大做了。因此,

  我們有必要進一步瞭解如何製作Makefile文件。在製作Makefile文件的過程中,編寫configure.in

  文件是關鍵,因此這一部分將重點介紹configure.in文件的編寫的相關問題。對於Makefile.am文

  件的編寫在編譯多文件和多目錄中要用到,所以下一篇將會介紹。

   在打開生成的Makefile文件,才發現能夠編譯的目標(target)有很多,因此有必要介紹一下

  其中比較重要而且常用的目標(target)的含義,也就是說,如何使用這個Makefile文件。

   1〉make 或 make all

   開始執行編譯過程,產生我們設定的目標。這時會開始編譯必要的源代碼文件,然後進

  行連結,並且最終生成可執行文件或我們想要生成庫文件。 具體命令行如下:

   [root@localhost testmk]#make

   或

   [root@localhost testmk]#make all

   2〉make dist

   將程序的源代碼和相關文檔打包成一個壓縮文件,以用來備份源代碼文件。命令完成後

  會在目錄下會產生一個以 PACKAGE-VERSION.tar.gz 爲名稱的打包文件。PACKAGE 和 VERSION

  這兩個變量是在configure.in文件中定義的。在我們的例子中定義如下:

   AM_INIT_AUTOMAKE(hello,1.0)

   因此會在目錄中生成一個名爲hello-1.0.tar.gz的文件。具體命令行如下:

   [root@localhost testmk]#make dist

   3〉make install

   將正確編譯生成的可執行文件或庫文件安裝到系統中,通常是/usr/local/bin這個目錄。

  如果沒有需要安裝的可執行文件或庫文件,將會自動執行make命令進行編譯,然後再進行安裝操

  作。具體命令行如下:

   [root@localhost testmk]#make install

   4〉make clean

   清除之前所編譯的可執行文件以及目標文件(Object Files, *.o)。 具體命令行如下:

   [root@localhost testmk]#make clean

   5〉make distclean

   清除之前所編譯的可執行文件、目標文件(Object Files, *.o)以及由執行./configure

  所產生的 Makefile文件。 具體命令行如下:

   [root@localhost testmk]#make distclean

   在弄清Makefile文件如何使用之後,我們來進一步瞭解生成Makefile文件的有關問題。先看

  一下源文件的結構和內容:

   /hello-1.0

   /hello-1.0/pubfun.h

   /hello-1.0/pubfun.c

   /hello-1.0/hello.c

   -------------------------------------------------

   /*filename:pubfun.h */

   #include <stdio.h>

   

   void *printA(void *pdata);

   -------------------------------------------------

   /*filename:pubfun.c */

   

   #include <stdio.h>

   #include <math.h>

   #include <pubfun.h>

   

   void *printA(void *pdata)

   {

   printf("%f/n",sin(2));

   printf("Hello,World -->%d/n",getpid());

   } 

   -------------------------------------------------

   /*filename:hello.c */

   

   #include <stdio.h>

   #include <pthread.h>

   #include "pubfun.h"

   

   int main(int argc,char **argv)

   {

   int ret;

   pthread_t ptid;

   int index;

   

   // create a thread

   ret = pthread_create(&ptid,NULL,printA,(void *)&index);

   if(ret)

   return -1;

   printA(NULL);

   

   return 0;

   } 

   ---------------------------------------------------

   對於我們的源文件中用到了數學庫和線程庫,還有我們自己寫的頭文件,我們修改configure.in 文件如下:

   01:dnl Process this file with autoconf to produce a configure script.

   02:AC_INIT(hello.c)

   03:

   04:dnl Add the file by leaf

   05:AM_INIT_AUTOMAKE(hello,1.0)

   06:

   07:dnl 檢查C編譯器.如果在環境中沒有設定CC,就查找gcc,如果沒有找到,就使用cc.

   08:AC_PROG_CC

   09:

   10: dnl 爲C編譯器提供的調試和優化選項.

   11: CFLAGS=" -O2"

   12:

   13: dnl 爲C預處理器和編譯器提供頭文件搜索目錄選項('-Idir')以及其他各種選項.

   14: CPPFLAGS=" -I."

   15:

   16: dnl 自定義輸出的檢查信息

   17:AC_MSG_CHECKING([for architecture type])

   18:

   19:dnl 輸出檢查結果

   20: AC_MSG_RESULT([ok])

   21:

   22: dnl 傳遞給連接器的'-l'和'-L'選項.

   23: LIBS=" -L."

   24:

   25:dnl Checks for libraries,Might abort.

   26:AC_CHECK_LIB(m,sin,[LIBS="$LIBS -lm"],exit 1)

   27: AC_CHECK_LIB(pthread,pthread_create,[LIBS="$LIBS -pthread"],exit 1)

   28: dnl AC_CHECK_LIB(socket, connect)

   29:

   30:dnl Checks for header files.(檢查頭文件是否存在)

   31:AC_CHECK_HEADERS(sys/socket.h)

   32:

   33:dnl Checks for typedefs, structures, and compiler characteristics.

   34:

   35:dnl Checks for library functions.

   36:

   37:AC_OUTPUT(Makefile) 

   

   這個configure.in文件中使用了幾個常用的宏,還有一些如AC_ARG_ENABLE宏、AC_MSG_ERROR

  宏、AC_MSG_RESULT宏、AM_PATH_GTK宏等有用的宏,你可以在附帶的附件文件裏查找,以便使用。

  這裏我要重點提一下第26到28行,對於程序中需要用到一些特定的庫,需要在編譯時進行指定,

  否則會出現連結錯誤。例如第26行就對數學庫進行了檢查,如果沒有,將退出Makefile文件的生

  成,因爲找不到數學庫生成也編譯不過去;同理第27行對線程庫進行了檢查,第28行對socket庫

  進行了檢查(被註釋掉的原因是引用的例子中沒用到此庫)。這裏面用到了AC_CHECK_LIB宏。具體

  用法如下:

   AC_CHECK_LIB(庫名稱,需要庫中的函數,[如果找到,[如果沒找到]])

  在這個宏中的[庫名稱]實在編譯時 -l 選項後面的名稱,如數學庫 -lm 就用 m 就行了。在例子

  中,如果找到了庫,就在編譯選項中添加 -lm 選項。

   還是如上一篇中所述的那樣,執行aclocal和autoconf命令,然後再創建並編輯Makefile.am

  文件內容如下:

   AUTOMAKE_OPTIONS=foreign

   bin_PROGRAMS=hello

   hello_SOURCES=hello.c pubfun.c pubfun.h 

   Makefile.am文件和上一篇中相比就增加了源代碼的數量,其他都沒有改變。 

   然後再執行automake --add-missing即可。信息如下:

   [root@leaf hello-1.0]# ./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

   checking for sin in -lm... yes

   checking for pthread_create in -lpthread... yes

   configure: creating ./config.status

   config.status: creating Makefile

   config.status: executing depfiles commands

   [root@leaf hello-1.0]# make

   source='hello.c' object='hello.o' libtool=no /

   depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo' /

   depmode=gcc3 /bin/sh ./depcomp /

   gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING=""

  -DPACKAGE_BUGREPORT="" -DPACKAGE="hello" -DVERSION="1.0" -I.

   -I. -I. -O2 -c `test -f 'hello.c' || echo './'`hello.c

   source='pubfun.c' object='pubfun.o' libtool=no /

   depfile='.deps/pubfun.Po' tmpdepfile='.deps/pubfun.TPo' /

   depmode=gcc3 /bin/sh ./depcomp /

   gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING=""

  -DPACKAGE_BUGREPORT="" -DPACKAGE="hello" -DVERSION="1.0" -I.

   -I. -I. -O2 -c `test -f 'pubfun.c' || echo './'`pubfun.c

   gcc -O2 -o hello hello.o pubfun.o -L. -lm -pthread 

   

   

  備註: 

   1.以上內容均在RedHat Linux 9.0環境下測試通過。

   2.詳細的選項可參考由王立翻譯的文檔http://www.cngnu.org/technology/1657/297.html。

   3.其它國內外網站資料

   4.RedHat 9.0下帶的程序文件及版本

   autoconf-2.57-3.noarch.rpm

   automake-1.6.3-5.noarch.rpm

   gcc-3.2.2-5.i386.rpm

   5.附件源文件用如下命令打開即可:

   tar xvzf hello-1.0.tar.gz

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章