一、 Linux 原始碼包安裝過程
用於 linux 原始碼安裝站群軟體,一般下載原始碼包得到檔案:file.tar.gz 和 file.tar.bz2 格式
(1) 解壓縮
解壓命令為:
tar jxvf file.tar.bz2
tar zxvf file.tar.gz
(2) 配置檔案
cd …/file
./configure ……
(3) 編譯檔案
make
(4) 安裝檔案
make install
(5) 刪除一些臨時檔案
make clean
make clean:清除編譯產生的可執行檔案及目標檔案 (object file,*.o)
make distclean:除了清除可執行檔案和目標檔案外,把 configure 所產生的 Makefile 也清除掉
make dist:將程式和相關的檔案包裝成一個壓縮檔案以供釋出。執行完在目錄下會產生一個以 PACKAGE-VERSION.tar.gz 為名稱的檔案。 PACKAGE 和 VERSION 這兩個變數是根據 configure.in 檔案中 AM_INIT_AUTOMAKE(PACKAGE,VERSION) 的定義。在此範例中會產生 test-1.0.tar.gz 的檔案。
make distcheck:和 make dist 類似,但是加入檢查包裝後的壓縮檔案是否正常。這個目標除了把程式和相關檔案包裝成 tar.gz 檔案外,還會自動把這個壓縮檔案解開,執行 configure,並且進行 make all 的動作,確認編譯無誤後,會顯示這個 tar.gz 檔案可供釋出了。這個檢查非常有用,檢查過關的包,基本上可以給任何一個具備 GNU 開發環境-的人去重新編譯
(5) 解除安裝檔案
make uninstall
二、編譯和執行一個簡單的 main.c 檔案
編譯和執行一個簡單的 c 語言原始檔 main.c, 需要自動生成一個 makefile,
以下是步驟:
第一步:
建立一個檔案 main.c 在一定的目錄/home/hufeihu/project/main 下面:
————————————————
在 main.c 檔案中寫入一下的語句
#include
int main(int argc, char** argv)
{
printf(“Hello, Auto Makefile!n”);
return 0;
}
————————————————
此時狀態如下:
root@hufeihu:/home/hufeihu/project/main# pwd
/home/hufeihu/project/mainroot@hufeihu:/home/hufeihu/project/main# ls
main.c
root@hufeihu:/home/hufeihu/project/main#
第二步:
———-
執行 autoscan , 自動建立兩個檔案: autoscan.log configure.scan
此時狀態如下:
root@hufeihu:/home/hufeihu/project/main# autoscan
root@hufeihu:/home/hufeihu/project/main# ls
autoscan.log configure.scan main.c
root@hufeihu:/home/hufeihu/project/main#
第三步:
———-
修改 configure.scan 的檔名為 configure.in
檢視 configure.in 的內容:
————————————————
# -*-Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([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.69)
#
# 初始化, 定義站群軟體的基本資訊, 包括設定包的全稱, 版本號以及報告 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, hufeihu@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, hufeihu@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@hufeihu:/home/hufeihu/project/main# aclocal
aclocal: warning: autoconf input should be named ‘configure.ac’, not ‘configure.in’
root@hufeihu:/home/hufeihu/project/main# ls
aclocal.m4 autom4te.cache autoscan.log configure.in main.c
root@hufeihu:/home/hufeihu/project/main#
第五步:
執行 autoconf, 目的是生成 configure
此時的狀態是:
root@hufeihu:/home/hufeihu/project/main# autoconf
root@hufeihu:/home/hufeihu/project/main# ls
aclocal.m4 autom4te.cache autoscan.log configure configure.in main.c
root@hufeihu:/home/hufeihu/project/main#
第六步:
執行 autoheader,它負責生成 config.h.in 檔案。該工具通常會從 “acconfig.h” 檔案中複製使用者附加的符號定義,因此此處沒有附加符號定義,所以不需要建立 “acconfig.h” 檔案。
此時的狀態是:
root@hufeihu:/home/hufeihu/project/main# autoheader
root@hufeihu:/home/hufeihu/project/main# ls
aclocal.m4 autoscan.log configure main.c
autom4te.cache config.h.in configure.in
root@hufeihu:/home/hufeihu/project/main#
第七步:
下面即將執行 automake, 但在此之前應該做一下準備工作!
首先
建立一個 Makefile.am. 這一步是建立 Makefile 很重要的一步,automake 要用的指令碼配置檔案是 Makefile.am,使用者需要自己建立相應的檔案。之後,automake 工具轉換成 Makefile.in 。
這個 Makefile.am 的內容如下:
————————————————
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_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@hufeihu:/home/hufeihu/project/main# automake –add-missing
configure.in:8: installing `./missing’
configure.in:8: installing `./install-sh’
Makefile.am: installing `./depcomp’
root@hufeihu:/home/hufeihu/project/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@hufeihu:/home/hufeihu/project/main#
————————————————
第八步
執行 configure,在這一步中,透過執行自動配置設定檔案 configure,把 Makefile.in 變成了最終的 Makefile 。
執行的結果如下:
————————————————
root@hufeihu:/home/hufeihu/project/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 whether make supports nested variables… yes
checking for gcc… gcc
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
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 whether gcc understands -c and -o together… yes
checking for style of include used by make… GNU
checking dependency style of gcc… gcc3
checking that generated files are newer than configure… done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
root@hufeihu:/home/hufeihu/project/main#
root@hufeihu:/home/hufeihu/project/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@hufeihu:/home/hufeihu/project/main#
————————————————
第九步
執行 make,對配置檔案 Makefile 進行測試一下
此時的狀態如下:
————————————————
root@hufeihu:/home/hufeihu/project/main# make
make all-am
make[1]: Entering directory ‘/home/hufeihu/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
make[1]: Leaving directory ‘/home/hufeihu/project/main’
root@hufeihu:/home/hufeihu/project/main#
root@hufeihu:/home/hufeihu/project/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]#
————————————————
第十步
執行生成的檔案 main:
————————————————
root@hufeihu:/home/hufeihu/project/main# ./main
Hello, Auto Makefile!
總結一下,使用 automake 自動生成 makefile 的過程主要有
1 、建立好原始檔以後到原始檔所在目錄
2 、 autoscan 命令 將 configure.scan 檔案修改為 configure.in
修改 configure.in 檔案中的內容:
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS) 修改為 AC_INIT(main, 1.0,hufeihu@163.com)
在 AC_CONFIG_HEADER([config.h]) 後面新增 AM_INIT_AUTOMAKE(main,1.0)
在最後新增 AC_OUTPUT([Makefile])
3 、執行 aclocal
4 、執行 autoconf
5 、執行 autoheader
6 、建立 Makefile.am 檔案,內容為
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main 如果有多個用空格分開
main_SOURCES=main.c 定義 main 所需原始檔,多個可執行檔案分別定義
7 、執行 automake –add-missing
8 、執行./configure
9 、執行 make
在第六步中需要自己寫 Makefile.am 檔案,特別是其中的 main_SOURCES 需要把生成 main 所以來的檔案都包含進來。並且那些間接依賴的檔案也需要包含進來。比如說我有三個檔案:main.cpp Add.cpp Add.h Num.h Num.cpp 其中在 main.cpp 中包含了 Add.h 在 Add.cpp 中包含了 Num.h 這樣在完成 main 的依賴檔案時就需要包含以上所有的問個檔案 main.cpp Add.cpp Add.h Num.h Num.cpp 才可以。