為了編譯一個簡單的源文件main.c,需要自動生成一個makefile,以下是步驟:第一步:----------在/root/project/main目錄下創建一個文件main.c,其內容如下:------------------------------------------------#include <stdio.h> int main(int argc, char** argv) { printf("Hello, Auto Makefile!\n"); return 0; } ------------------------------------------------此時狀態如下:[root@localhost main]# pwd/root/project/main[root@localhost main]# lsmain.c[root@localhost main]# 第二步:----------運行 autoscan , 自動創建兩個文件: autoscan.log configure.scan此時狀態如下:[root@localhost main]# autoscan[root@localhost main]# lsautoscan.log configure.scan main.c[root@localhost main]# 第三步:----------修改configure.scan的文件名為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.c])AC_CONFIG_HEADER([config.h])# 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------------------------------------------------解讀以上的文件:------------------------------------------------# -*- Autoconf -*-# Process this file with autoconf to produce a configure script.# AC_PREREQ:# 確保使用的是足夠新的Autoconf版本。如果用于創建configure的Autoconf的版# 本比version 要早,就在標準錯誤輸出打印一條錯誤消息并不會創建configure。AC_PREREQ(2.61)## 初始化,定義軟件的基本信息,包括設置包的全稱,版本號以及報告BUG時需要用的郵箱地址#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)## 用來偵測所指定的源碼文件是否存在,來確定源碼目錄的有效性#AC_CONFIG_SRCDIR([main.c])## 用于生成config.h文件,以便autoheader使用#AC_CONFIG_HEADER([config.h])# Checks for programs.AC_PROG_CC# Checks for libraries.# Checks for header files.# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.## 創建輸出文件。在`configure.in'的末尾調用本宏一次。#AC_OUTPUT------------------------------------------------修改動作: 1.修改AC_INIT里面的參數: AC_INIT(main,1.0, pgpxc@163.com) 2.添加宏AM_INIT_AUTOMAKE, 它是automake所必備的宏,也同前面一樣,PACKAGE是所要產生軟件套件的名稱,VERSION是版本編號。 3.在AC_OUTPUT后添加輸出文件Makefile修改后的結果:------------------------------------------------# -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_PREREQ(2.61)AC_INIT(main, 1.0, pgpxc@163.com)AC_CONFIG_SRCDIR([main.c])AC_CONFIG_HEADER([config.h])AM_INIT_AUTOMAKE(main,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])------------------------------------------------第四步:運行 aclocal, 生成一個“aclocal.m4”文件和一個緩沖文件夾autom4te.cache,該文件主要處理本地的宏定義。此時的狀態是:[root@localhost main]# aclocal[root@localhost main]# lsaclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ main.c[root@localhost main]# 第五步:運行 autoconf, 目的是生成 configure 此時的狀態是:[root@localhost main]# autoconf[root@localhost main]# lsaclocal.m4 autoscan.log configure.in main.cautom4te.cache configure configure.in~[root@localhost main]# 第六步:運行 autoheader,它負責生成config.h.in文件。該工具通常會從“acconfig.h”文件中復制用戶附加的符號定義,因此此處沒有附加符號定義,所以不需要創建“acconfig.h”文件。此時的狀態是:[root@localhost main]# autoheader[root@localhost main]# lsaclocal.m4 autoscan.log configure configure.in~autom4te.cache config.h.in configure.in main.c[root@localhost main]# 第七步:下面即將運行 automake, 但在此之前應該做一下準備工作!首先創建一個 Makefile.am.這一步是創建Makefile很重要的一步,automake要用的腳本配置文件是Makefile.am,用戶需要自己創建相應的文件。之后,automake工具轉換成Makefile.in。這個Makefile.am的內容如下:------------------------------------------------AUTOMAKE_OPTIONS=foreignbin_PROGRAMS=mainmain_SOURCES=main.c------------------------------------------------下面對該腳本文件的對應項進行解釋。 其中的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。其次使用automake對其生成“configure.in”文件,在這里使用選項“—adding-missing”可以讓automake自動添加有一些必需的腳本文件。運行后的狀態是:------------------------------------------------[root@localhost main]# automake --add-missingconfigure.in:8: installing `./missing'configure.in:8: installing `./install-sh'Makefile.am: installing `./depcomp'[root@localhost main]# lsaclocal.m4 config.h.in configure.in~ main.c Makefile.inautom4te.cache configure depcomp Makefile.am missingautoscan.log configure.in install-sh Makefile.am~[root@localhost main]# ------------------------------------------------第八步運行configure,在這一步中,通過運行自動配置設置文件configure,把Makefile.in變成了最終的Makefile。運行的結果如下:------------------------------------------------[root@localhost main]# ./configure checking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables... checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3configure: creating ./config.statusconfig.status: creating Makefile
config.status: creating config.hconfig.status: executing depfiles commands[root@localhost main]# lsaclocal.m4 config.h.in configure.in main.c Makefile.inautom4te.cache config.log configure.in~ Makefile missingautoscan.log config.status depcomp Makefile.am stamp-h1config.h configure install-sh Makefile.am~[root@localhost main]# ------------------------------------------------第九步運行 make,對配置文件Makefile進行測試一下此時的狀態如下:------------------------------------------------[root@localhost main]# makecd . && /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 --recheckrunning CONFIG_SHELL=/bin/sh /bin/sh ./configure --no-create --no-recursionchecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables... checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3configure: creating ./config.status/bin/sh ./config.statusconfig.status: creating Makefileconfig.status: creating config.hconfig.status: config.h is unchangedconfig.status: executing depfiles commandscd . && /bin/sh /root/project/main/missing --run autoheaderrm -f stamp-h1touch config.h.inmake all-ammake[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.cmv -f .deps/main.Tpo .deps/main.Pogcc -g -O2 -o main main.o cd . && /bin/sh ./config.status config.hconfig.status: creating config.hconfig.status: config.h is unchangedmake[1]: Leaving directory `/root/project/main'[root@localhost main]# lsaclocal.m4 autoscan.log config.h.in config.status configure.in depcomp main main.o Makefile.am Makefile.in stamp-h1autom4te.cache config.h config.log configure configure.in~ install-sh main.c Makefile Makefile.am~ missing[root@localhost main]# ------------------------------------------------第十步運行生成的文件 main:------------------------------------------------[root@localhost main]# ./mainHello, Auto Makefile![root@localhost main]# ------------------------------------------------我用的是ubuntu以上就是全文了.但有一處要改:用aclocal全報有一個m4文件有錯.找到報錯的那一行.把變量加個中括號就可以了.下面來分別解釋一下上面的文件和命令: autoscan : 用來掃描源代碼的目錄生成configure.scan,如果不指定掃描目錄,則他會掃描當前工作目錄, configure.scan : 包含了系統配置的基本選項,一般用來制作configure.in文件。 configure.in : 該文件的內容都是一些宏,其中的順序沒有硬性的規定,不過建議的順序是: AC_INIT 測試程序 測試函數 測試頭文件 測試類型定義 測試結構 測試編譯器 測試庫函數 測試系統調用 AC_OUTPOUT 更詳細的請參閱《GNU/Linux編程指南(第二版)》
aclocal: 根據configure.in的內容自動生成aclocal.m4文件,其定義為:alocal : creat alocal.m4 by scanning configure.ac
alocal.m4 : 在執行automake的時候還需要其他的一些宏,這就由alocal產生。當有了 configure.in和alocal.m4兩個宏文件后,就可以用automake來產生Makefile.in文件了。
autoconf: 用來產生configure腳本程序。
configure: 他能根據不同的系統,產生不同的Makefile,從而是我們的程序具有可移植性。他還有一些參數:
--cache-file=FILE 測試系統的特性,并將結果放到FILE中
--help 輸出幫助信息
--no-create 阻止其生成輸出文件
--quiet 執行是不做輸出
--silent 同上,若設置則不會有任何輸出到屏幕
--version 輸出automake的版本號
--prefix=PEWFIX 安裝位置設置(常用)
--exec-prefix=EPREFIX 設置結構倚賴的文件的安裝位置
--bindir=DIR 指定可執行文件的安裝位置.
--sbindir=DIR 指定超級用戶可執行的安裝位置.
--libexecdir=DIR 指定可執行支持文件的安裝位置.
--datadir=DIR 指定通用數據文件的安裝位置.
--sysconfdir=DIR 指定只讀數據的安裝位置.
--sharedstatedir=DIR 指定共享的可寫數據的安裝位置.
--localstatedir=DIR 指定(非共享)可寫數據的安裝位置.
--libdir=DIR 指定庫文件的安裝位置.
--includedir=DIR 指定C頭文件的安裝位置.
--oldincludedir=DIR 指定為除GCC外編譯器安裝的C頭文件的安裝位置.
--infodir=DIR 指定Info格式文檔的安裝位置.
--mandir=DIR 指定手冊頁的安裝位置.
--srcdir=DIR 源碼的位置
--program-prefix=PREFIX 增加安裝程序名字前綴.
--program-suffix=SUFFIX 增加安裝程序名字后綴.
--program-transform-name=PROGRAM 產生安裝名
--build=BUILD 指定軟件包安裝的系統平臺.
--host=HOST 指定軟件運行的系統平臺.
--target=GARGET 指定軟件面向的系統平臺
--disable-FEATURE 提供為大型選項的編譯時配置
--enable-FEATURE[=ARG] 提供了一些默認被禁止的特性
--enable-FEATURE=no 同--disable-FEATURE
--with-PACKAGE[=ARG] 使用已有軟件包和庫
--with-PACKAGE=no --without-PACKAGE同義
--without-PACKAGE 禁止軟件包與系統已有的軟件包交互
--x-includes=DIR --with-PACKAGE的一個特例
--x-libraries=DIR 向configure腳本指明包含X11庫的目錄
makefile.am : 根據他生成makefile.in文件。
AUTOMAKE_OPTIONS = foreign 設置automake的選項,設置成foreign表示按一般軟件處理。
bin_PROGRAMS = filename1 [...] 產生的可執行文件名
filename1_SOURCES = f1.c f2.c 指明生成filename可執行文件的源碼
[...]
filename_LDADD =
filename_LDFLAGS =
filename_DEPENDENCIES =
靜態庫lib_LIBRARIES = libfilename.a
filename_a_SOURCES =
filename_a_LDADD =
filename_a_LIBADD =
filename_a_LDFALGS =
頭文件include_HEADERS = filename.h
數據文件data_DATA = data1 data2
(對于可執行文件和靜態庫類型如果只想編譯,不想安裝到系統中,可以用:noinst_PROGRAMS代替bin_PROGRAMS
和noinst_LIBRARIES代替lib_LIBRARIES)
automake : 使用automake --add-missing來生成Makefile.in文件,后面的選項使我們制作出來的Makfile符合GNU的習慣,
Makefile : GNU的習慣:
make 編譯,連接,生成可執行文件;
make clean 清除編譯產生的obj文件,以及可執行文件;
make install 安裝到系統中,一般是/usr/local/bin
make dist 將源碼打包,以便發布
make diskcheck 生成包,并對其進行檢查,以確保其正確性;
以上相關鏈接:http://www.cnblogs.com/Safe3/archive/2009/02/10/1387460.html
http://blog.csdn.net/hadesgin/article/details/6704371
posted on 2012-11-08 15:37
王海光 閱讀(1489)
評論(0) 編輯 收藏 引用 所屬分類:
Linux