版權(quán)聲明:本文為 cdeveloper 原創(chuàng)文章镶殷,可以隨意轉(zhuǎn)載利花,但必須在明確位置注明出處!
Autoconf 簡(jiǎn)介
Autoconf 是一種用于生成 shell
腳本的工具泊藕,可自動(dòng)配置軟件源代碼包辅辩,以適應(yīng)多種類型的類似 Posix
的系統(tǒng)。你可以簡(jiǎn)單地把它看為打包源碼的工具娃圆,例如你在 GNU 官網(wǎng)下載的那些 xxx.tar.gz
格式的軟件包玫锋,我們使用這個(gè)工具最終目的就是將源代碼打成一個(gè)包來提供給別人使用。本次就跟大家分享下如何打包一個(gè)基本的 Hello World
給別人使用讼呢,掌握這個(gè)步驟以后就可以類比來打包別的軟件包了撩鹿,首先我們來看看 Autoconf 的基本打包流程。
Autoconf 打包流程
我們使用 Autoconf
打包一個(gè)軟件包主要依靠下面這張圖:
這張圖中主要使用了 5 個(gè)與 autoconf 相關(guān)的工具悦屏,我們分別來了解即可节沦,不需要深入學(xué)習(xí)。
1. autoscan
autoscan
用來掃描源代碼目錄并生成 configure.scan
文件础爬,這個(gè)文件包含了系統(tǒng)配置的基本選項(xiàng)散劫,里面都是一些宏定義,在使用的時(shí)候需要將這個(gè)文件改名為 configure.ac
幕帆,并修改相關(guān)的配置,我們后面在實(shí)際例子中介紹赖条。
2. aclocal
aclocal
是一個(gè) prel
腳本程序失乾,aclocal
根據(jù) configure.ac
文件的內(nèi)容自動(dòng)生成 aclocal.m4
文件常熙,這個(gè)文件內(nèi)容是 configure.ac
中的宏展開。
3. autoconf
autoconf
用來產(chǎn)生 configure
文件碱茁,這個(gè)文件就是我們?cè)谑謩?dòng)編譯一個(gè)軟件是要做的第一步:./configure
裸卫。
4. autoheader
autoheader
自動(dòng)生成相關(guān)的文件,這個(gè)功能在源碼需要頭文件時(shí)才使用纽竣。
5. automake
automake
可以將 Makefile.am
生成 Makfile.in
墓贿,但是 Makefil.am
需要我們手動(dòng)書寫。
了解了這 5 個(gè)工具蜓氨,下面我們就來打包一個(gè)實(shí)際的 Hello World !
實(shí)踐:Autoconf 打包 Hello World
我們來以一個(gè)實(shí)際打包 hello.c
的例子來介紹 Autoconf 的基本用法聋袋。
安裝 autoconf
首先確定你的系統(tǒng)有沒有安裝 autoconf
,在命令行鍵入 autoconf
穴吹,如果提示沒有安裝幽勒,則需要先安裝:
sudo apt-get install autoconf
編輯 hello.c
我們編寫一個(gè) hello.c
作為測(cè)試:
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
return 0;
}
下面就開始正式打包流程。
1. autoscan
從上面的圖中可以看到港令,第一步我們需要使用 autoscan
來生成 configure.scan
文件:
autoscan
執(zhí)行的結(jié)果除了 hello.c
還有另外 2 個(gè)文件:
2. 修改 configure.ac
之后我們還需要將 configure.scan
改名為 configure.ac
啥容,并修改以下的 3 點(diǎn)內(nèi)容:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
# 1. 修改:可執(zhí)行文件名稱,版本號(hào)顷霹,bug 郵箱
AC_INIT(hello, 1.0, chenghjy@gmail.com)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])
# 2. 我們后面使用 automake, 所以需要加上這個(gè)配置
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
結(jié)果生成了 aclocal.m4
文件,如下圖所示:
4. autoconf
現(xiàn)在可以使用 autoconf
來生成 configure
啦:
autoconf
結(jié)果如下淋淀,生成了 configure
:
我們直接執(zhí)行 ./configure
看看是否能夠配置成功:
./configure
# 結(jié)果
configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."
但是結(jié)果提示我們缺少一些 shell 腳本遥昧,因此我們還需要進(jìn)行后面的配置。
5. autoheader
如果在 configure.ac
中需要頭文件绅喉,則需要進(jìn)行這一步渠鸽,否則不需要,我們配置了所以需要:
autoheader
6. 編寫 Makefile.am
上圖中柴罐,我們?nèi)绻褂?automake
來生成 Makefile.in
徽缚,則還需要 Makefile.am
文件,但是這個(gè)文件需要我們手動(dòng)編寫革屠,具體如何編寫凿试,可以查看 automake 官方文檔,在下面這個(gè)圖片位置:
因?yàn)槲覀兊?hello
程序很簡(jiǎn)單似芝,不需要依賴其他的庫(kù)那婉,所以這里只需要寫 2 行:
bin_PROGRAMS = hello
hello_SOURCES = hello.c
7. automake
上面寫完了 Makefile.am
文件,現(xiàn)在就可以使用 automake
來生成 Makefile.in
啦:
automake
# 結(jié)果
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'
但是出現(xiàn)了一些錯(cuò)誤党瓮,從提示信息中發(fā)現(xiàn)可以使用 automake --add-missing
:
automake --add-missing
# 結(jié)果
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'
還是有錯(cuò)誤详炬,但是這個(gè)錯(cuò)誤很好解決,它提示 NEWS
寞奸,README
呛谜,AUTHORS
在跳,ChangeLog
這 4 個(gè)文件沒有找到,其實(shí)這 4 個(gè)文件是一個(gè)正規(guī)軟件發(fā)布的時(shí)候一般都帶有的隐岛,我們這里新建這 4 個(gè)文件即可猫妙,就不寫內(nèi)容了:
touch NEWS README AUTHORS ChangeLog
# 再次執(zhí)行,沒有錯(cuò)誤信息
automake --add-missing
結(jié)果生成了 Makefile.in
聚凹,這也就是我們配置的最終結(jié)果啦:
可以看到目前我們的文件已經(jīng)很多了割坠,是不是有些正式發(fā)布的軟件的樣子了,其實(shí)正式的軟件里面的大部分配置文件也是自動(dòng)生成的妒牙。不過我們還差最后一步:打包彼哼。
8. 打包
我們使用 make dist
命令直接打包:
# 先要配置得到 Makefile
./configure
# 打包
make dist
# ls 結(jié)果
hello-1.0.tar.gz
測(cè)試 hello-1.0.tar.gz
下面我們就來測(cè)試我們打包的 hello
程序是否可用,我們從配置到最后的卸載一共分為 6 步:
# 1. 配置:./configure
./configure
# 2. 編譯:make
make
# 3. 安裝:install
sudo make install
# 4. 運(yùn)行
hello
# 5. 結(jié)果单旁,打印 Hello World! 說明成功啦沪羔!
Hello World!
# 6. 卸載:uninstall
sudo make uninstall
一路綠燈,說明我們打的包沒有問題象浑,那么這個(gè)實(shí)驗(yàn)也就到此結(jié)束了蔫饰。
結(jié)語
本次我們學(xué)習(xí)如何在 Linux 下通過命令行和 Autoconf 來打包一個(gè)程序,我們也知道了從網(wǎng)上下載下的軟件包中的那么多文件是怎么來的了愉豺,并且我們也實(shí)際練習(xí)了如何打包一個(gè) Hello World!
給別人使用了篓吁。通過這個(gè)例子,希望你能夠掌握 Autoconf
基本的用法蚪拦,在以后遇到問題的時(shí)候杖剪,還望你能主動(dòng)去 Autoconf 官網(wǎng) 查找資料,做一個(gè)主動(dòng)學(xué)習(xí)的人驰贷。
最后盛嘿,感謝你的閱讀,我們下次再見 :)