網絡資源:http://www.worldhello.net/2010/04/07/954.html
MakeFile文件簡介
Makefile是用于自動編譯和鏈接的,一個工程有很多文件組成,每一個文件的改變都會導致工程的重新鏈接,但是不是所有的文件都需要重新編譯,Makefile中紀錄有文件的信息,在make時會決定在鏈接的時候需要重新編譯哪些文件。
Makefile的宗旨就是:讓編譯器知道要編譯一個文件需要依賴其他的哪些文件。當那些依賴文件有了改變,編譯器會自動的發現最終的生成文件已經過時,而重新編譯相應的模塊。
Makefile的基本結構不是很復雜,但當一個程序開發人員開始寫Makefile時,經常會懷疑自己寫的是否符合慣例,而且自己寫的Makefile經常和自己的開發環境相關聯,當系統環境變量或路徑發生了變化后,Makefile可能還要跟著修改。這樣就造成了手工書寫Makefile的諸多問題,automake恰好能很好地幫助我們解決這些問題。
使用automake,程序開發人員只需要寫一些簡單的含有預定義宏的文件,由autoconf根據一個宏文件生成configure,由automake根據另一個宏文件生成Makefile.in,再使用configure依據Makefile.in來生成一個符合慣例的Makefile。下面我們將詳細介紹Makefile的automake生成方法。
-使用環境
本文所提到的程序是基于Linux發行版本:Ubuntu10.04,它包含了我們要用到的autoconf,automake。
-具體步驟
對于一個較大的項目而言,完全手動建立Makefile是一件費力而又容易出錯的工作。autotools系列工具只需用戶輸入簡單的目標文件、依賴文件、文件目錄等就可以比較輕松地生成Makefile了。現在Linux上的軟件開發一般都是用autotools來制作Makefile。
具體實例:
----------
在/root/project/main目錄下創建一個文件main.c,其內容如下:
------------------------------------------------
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Hello, Auto Makefile!\n");
return 0;
}
------------------------------------------------
具體步驟如下:
(1)autoscan
使用autoscan在給定目錄及其子目錄樹中檢查源文件,如果沒有給出目錄,就在當前目錄及其子目錄樹中進行檢查。最終生成兩個文件:configure.scan、autoscan.log
(2)修改configure.scan的文件名為configure.in
命令:mv configure.scan configure.in
(3)修改configure.in的內容:
configure.in的內容說明:
configure.in文件內容如下:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([main.cpp])
AC_CONFIG_HEADER([config.h])
# Checks for programs.
AC_PROG_CXX
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
說明:
1、以“#”號開始的是行為注釋。
2、AC_PREREQ宏聲明本文件要求的autoconf版本。
3、AC_INIT宏用來定義軟件的名稱和版本等信息,這里的BUG-REPORT- ADDRESS可以省略。
4、AC_CONFIG_SRCDIR宏用來偵測所指定的源碼文件是否存在,
來確定源碼目錄的有效性。這個參數一般不需要修改。
5、AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。
修改時需要增加一個宏AM_INIT_AUTOMAKE(PACKAGE,VERSION)
具體如下:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT(main, 1.0, wyylling@126.com)
AM_INIT_AUTOMAKE(main,1.0)
AC_CONFIG_SRCDIR([main.cpp])
#AC_CONFIG_HEADER([config.h])
AM_CONFIG_HEADER([config.h])
# Checks for programs.
AC_PROG_CXX
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
我的configure.in下面的真實內容:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65])
AC_INIT(lda, 1.0, 1527zhaobin@163.com)
AM_INIT_AUTOMAKE(lda,1.0)
AC_CONFIG_SRCDIR([lda.h])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([memset])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
(4)運行aclocal,生成一個“aclocal.m4”文件和一個緩沖文件夾autom4te.cache,該文件主要處理本地的宏定義。
此時的狀態是:
[root@localhost main]# aclocal
[root@localhost main]# ls
aclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ main.c
[root@localhost main]#
(5)運行 autoconf, 目的是生成 configure
此時的狀態是:
[root@localhost main]# autoconf
[root@localhost main]# ls
aclocal.m4 autoscan.log configure.in main.c
autom4te.cache configure configure.in~
[root@localhost main]#
(6)運行 autoheader,它負責生成config.h.in文件。該工具通常會從“acconfig.h”文件中復制用戶附加的符號定義,因此此處沒有附加符號定義,所以不需要創建“acconfig.h”文件。
此時的狀態是:
[root@localhost main]# autoheader
[root@localhost main]# ls
aclocal.m4 autoscan.log configure configure.in~
autom4te.cache config.h.in configure.in main.c
[root@localhost main]#
(7)配置腳本文件Makefile.am
新建Makefile.am文件,命令:
$ vi Makefile.am
內容如下:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c
automake會根據你寫的Makefile.am來自動生成Makefile.in。
下面對該腳本文件的對應項進行解釋。
其中的AUTOMAKE_OPTIONS為設置automake的選項。由于GNU(在第1章中已經有所介紹)對自己發布的軟件有嚴格的規范,比如必須附 帶許可證聲明文件COPYING等,否則automake執行時會報錯。automake提供了三種軟件等級:foreign、gnu和gnits,讓用 戶選擇采用,默認等級為gnu。在本例使用foreign等級,它只檢測必須的文件。
bin_PROGRAMS定義要產生的執行文件名。如果要產生多個執行文件,每個文件名用空格隔開。
main_SOURCES定義“main”這個執行程序所需要的原始文件。如果”main”這個程序是由多個原始文件所產生的,則必須把它所用到的所有原 始文件都列出來,并用空格隔開。例如:若目標體“main”需要“main.c”、“sunq.c”、“main.h”三個依賴文件,則定義 main_SOURCES=main.c sunq.c main.h。要注意的是,如果要定義多個執行文件,則對每個執行程序都要定義相應的file_SOURCES。例如,某個Makefile文件內容如下(編譯生成三個文件:hello,pthread,tty):
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello pthread tty
hello_SOURCES = hello.c
pthread_SOURCES = pthread.c
tty_SOURCES = tty.c
我的Makefile.am文件內容:
AUTO_OPTIONS=foreign
bin_PROGRAMS= lda
lda_SOURCES= lda.cc file_access.cc document.cc lda.h file_access.h document.h
(8)將運行 automake
[root@localhost main]# automake --add-missing
使用automake對其生成“configure.in”文件,在這里使用選項“—adding-missing”可以讓automake自動添加有一些必需的腳本文件。
運行后的狀態是:
configure.in:8: installing `./missing'
configure.in:8: installing `./install-sh'
Makefile.am: installing `./depcomp'
[root@localhost main]# ls
aclocal.m4 config.h.in configure.in~ main.c Makefile.in
autom4te.cache configure depcomp Makefile.am missing
autoscan.log configure.in install-sh Makefile.am~
[root@localhost main]#
在這一步中往往會產生一種錯誤如下:
configure.in: required file `./install-sh' not found
configure.in: required file `./mkinstalldirs' not found
configure.in: required file `./missing' not found
Makefile.am: required file `./COPYING' not found
Makefile.am: required file `./INSTALL' not found
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
Makefile.am: required file `./depcomp' not found
我們將進行如下處理:
Additionally, the INSTALL, NEWS, README, COPYING, AUTHORS and ./ChangeLog files need to be created:
touch INSTALL NEWS README AUTHORS ChangeLog
(9)運行configure,在這一步中,通過運行自動配置設置文件configure,把Makefile.in變成了最終的Makefile。
運行的結果如下:
------------------------------------------------
[root@localhost main]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... 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 ISO C89... 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: creating config.h
config.status: executing depfiles commands
[root@localhost main]# ls
aclocal.m4 config.h.in configure.in main.c Makefile.in
autom4te.cache config.log configure.in~ Makefile missing
autoscan.log config.status depcomp Makefile.am stamp-h1
config.h configure install-sh Makefile.am~
[root@localhost main]#
(10)運行 make,對配置文件Makefile進行測試一下
此時的狀態如下:
------------------------------------------------
[root@localhost main]# make
cd . && /bin/sh /root/project/main/missing --run aclocal-1.10
cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign
cd . && /bin/sh /root/project/main/missing --run autoconf
/bin/sh ./config.status --recheck
running CONFIG_SHELL=/bin/sh /bin/sh ./configure --no-create --no-recursion
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... 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 ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
configure: creating ./config.status
/bin/sh ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
cd . && /bin/sh /root/project/main/missing --run autoheader
rm -f stamp-h1
touch config.h.in
make all-am
make[1]: Entering directory `/root/project/main'
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -g -O2 -o main main.o
cd . && /bin/sh ./config.status config.h
config.status: creating config.h
config.status: config.h is unchanged
make[1]: Leaving directory `/root/project/main'
[root@localhost main]# ls
aclocal.m4 autoscan.log config.h.in config.status configure.in depcomp main main.o Makefile.am Makefile.in stamp-h1
autom4te.cache config.h config.log configure configure.in~ install-sh main.c Makefile Makefile.am~ missing
[root@localhost main]#
------------------------------------------------
(11)運行生成的文件 main:
------------------------------------------------
[root@localhost main]# ./main
Hello, Auto Makefile!
[root@localhost main]#
補充:
Makefile
在符合GNU Makefiel慣例的Makefile中,包含了一些基本的預先定義的操作:
make
根據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,來確認編譯不出現錯誤,最后提示你軟件包已經準備好,可以發布了。
===============================================
helloworld-1.0.tar.gz is ready for distribution
===============================================
make distclean
類似make clean,但同時也將configure生成的文件全部刪除掉,包括Makefile。
posted on 2012-04-24 20:33
憤怒的考拉 閱讀(694)
評論(0) 編輯 收藏