歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> Linux 環境下 Makefile 文件制作淺談(一)

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

日期:2017/2/27 14:29:03   编辑:更多Linux
  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



4.RedHat 9.0下帶的程序文件及版本 autoconf-2.57-3.noarch.rpm automake-1.6.3-5.noarch.rpm gcc-3.2.2-5.i386.rpm



Copyright © Linux教程網 All Rights Reserved