版權聲明:本文為 cdeveloper 原創文章,可以隨意轉載,但必須在明確位置注明出處!
Autoconf 簡介
Autoconf 是一種用于生成 shell
腳本的工具,可自動配置軟件源代碼包,以適應多種類型的類似 Posix
的系統。你可以簡單地把它看為打包源碼的工具,例如你在 GNU 官網下載的那些 xxx.tar.gz
格式的軟件包,我們使用這個工具最終目的就是將源代碼打成一個包來提供給別人使用。本次就跟大家分享下如何打包一個基本的 Hello World
給別人使用,掌握這個步驟以后就可以類比來打包別的軟件包了,首先我們來看看 Autoconf 的基本打包流程。
Autoconf 打包流程
我們使用 Autoconf
打包一個軟件包主要依靠下面這張圖:
這張圖中主要使用了 5 個與 autoconf 相關的工具,我們分別來了解即可,不需要深入學習。
1. autoscan
autoscan
用來掃描源代碼目錄并生成 configure.scan
文件,這個文件包含了系統配置的基本選項,里面都是一些宏定義,在使用的時候需要將這個文件改名為 configure.ac
,并修改相關的配置,我們后面在實際例子中介紹。
2. aclocal
aclocal
是一個 prel
腳本程序,aclocal
根據 configure.ac
文件的內容自動生成 aclocal.m4
文件,這個文件內容是 configure.ac
中的宏展開。
3. autoconf
autoconf
用來產生 configure
文件,這個文件就是我們在手動編譯一個軟件是要做的第一步:./configure
。
4. autoheader
autoheader
自動生成相關的文件,這個功能在源碼需要頭文件時才使用。
5. automake
automake
可以將 Makefile.am
生成 Makfile.in
,但是 Makefil.am
需要我們手動書寫。
了解了這 5 個工具,下面我們就來打包一個實際的 Hello World !
實踐:Autoconf 打包 Hello World
我們來以一個實際打包 hello.c
的例子來介紹 Autoconf 的基本用法。
安裝 autoconf
首先確定你的系統有沒有安裝 autoconf
,在命令行鍵入 autoconf
,如果提示沒有安裝,則需要先安裝:
sudo apt-get install autoconf
編輯 hello.c
我們編寫一個 hello.c
作為測試:
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
return 0;
}
下面就開始正式打包流程。
1. autoscan
從上面的圖中可以看到,第一步我們需要使用 autoscan
來生成 configure.scan
文件:
autoscan
執行的結果除了 hello.c
還有另外 2 個文件:
2. 修改 configure.ac
之后我們還需要將 configure.scan
改名為 configure.ac
,并修改以下的 3 點內容:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
# 1. 修改:可執行文件名稱,版本號,bug 郵箱
AC_INIT(hello, 1.0, chenghjy@gmail.com)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])
# 2. 我們后面使用 automake, 所以需要加上這個配置
AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
# 3. 輸出文件指定為 Makefile
AC_OUTPUT(Makefile)
3. aclocal
從上圖可以看到我們生成 configure
需要 [aclocal.m4]
文件,我們使用 aclocal
來生成:
aclocal
結果生成了 aclocal.m4
文件,如下圖所示:
4. autoconf
現在可以使用 autoconf
來生成 configure
啦:
autoconf
結果如下,生成了 configure
:
我們直接執行 ./configure
看看是否能夠配置成功:
./configure
# 結果
configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."
但是結果提示我們缺少一些 shell 腳本,因此我們還需要進行后面的配置。
5. autoheader
如果在 configure.ac
中需要頭文件,則需要進行這一步,否則不需要,我們配置了所以需要:
autoheader
6. 編寫 Makefile.am
上圖中,我們如果要使用 automake
來生成 Makefile.in
,則還需要 Makefile.am
文件,但是這個文件需要我們手動編寫,具體如何編寫,可以查看 automake 官方文檔,在下面這個圖片位置:
因為我們的 hello
程序很簡單,不需要依賴其他的庫,所以這里只需要寫 2 行:
bin_PROGRAMS = hello
hello_SOURCES = hello.c
7. automake
上面寫完了 Makefile.am
文件,現在就可以使用 automake
來生成 Makefile.in
啦:
automake
# 結果
configure.ac:10: error: required file './compile' not found
configure.ac:10: 'automake --add-missing' can install 'compile'
configure.ac:8: error: required file './install-sh' not found
configure.ac:8: 'automake --add-missing' can install 'install-sh'
configure.ac:8: error: required file './missing' not found
configure.ac:8: 'automake --add-missing' can install 'missing'
Makefile.am: error: required file './INSTALL' not found
Makefile.am: 'automake --add-missing' can install 'INSTALL'
Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
Makefile.am: error: required file './COPYING' not found
Makefile.am: 'automake --add-missing' can install 'COPYING'
Makefile.am: error: required file './depcomp' not found
Makefile.am: 'automake --add-missing' can install 'depcomp'
但是出現了一些錯誤,從提示信息中發現可以使用 automake --add-missing
:
automake --add-missing
# 結果
configure.ac:10: installing './compile'
configure.ac:8: installing './install-sh'
configure.ac:8: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am: Consider adding the COPYING file to the version control system
Makefile.am: for your code, to avoid questions about which license your project uses
Makefile.am: installing './depcomp'
還是有錯誤,但是這個錯誤很好解決,它提示 NEWS
,README
,AUTHORS
,ChangeLog
這 4 個文件沒有找到,其實這 4 個文件是一個正規軟件發布的時候一般都帶有的,我們這里新建這 4 個文件即可,就不寫內容了:
touch NEWS README AUTHORS ChangeLog
# 再次執行,沒有錯誤信息
automake --add-missing
結果生成了 Makefile.in
,這也就是我們配置的最終結果啦:
可以看到目前我們的文件已經很多了,是不是有些正式發布的軟件的樣子了,其實正式的軟件里面的大部分配置文件也是自動生成的。不過我們還差最后一步:打包。
8. 打包
我們使用 make dist
命令直接打包:
# 先要配置得到 Makefile
./configure
# 打包
make dist
# ls 結果
hello-1.0.tar.gz
測試 hello-1.0.tar.gz
下面我們就來測試我們打包的 hello
程序是否可用,我們從配置到最后的卸載一共分為 6 步:
# 1. 配置:./configure
./configure
# 2. 編譯:make
make
# 3. 安裝:install
sudo make install
# 4. 運行
hello
# 5. 結果,打印 Hello World! 說明成功啦!
Hello World!
# 6. 卸載:uninstall
sudo make uninstall
一路綠燈,說明我們打的包沒有問題,那么這個實驗也就到此結束了。
結語
本次我們學習如何在 Linux 下通過命令行和 Autoconf 來打包一個程序,我們也知道了從網上下載下的軟件包中的那么多文件是怎么來的了,并且我們也實際練習了如何打包一個 Hello World!
給別人使用了。通過這個例子,希望你能夠掌握 Autoconf
基本的用法,在以后遇到問題的時候,還望你能主動去 Autoconf 官網 查找資料,做一個主動學習的人。
最后,感謝你的閱讀,我們下次再見 :)