autotools是個系列工具,首先確認你的Ubuntu系統是否安裝了以下工具(可以通過which命令查看):
aclocal
autoscan
autoconf
autoheader
automake
安裝方法:
root@ubuntu:~# sudo apt-get install autoconf (我只執行了這一條安裝指令 下面的使用中沒有出錯)
顯示如下:
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關係樹
正在讀取狀態信息... 完成
E: 無法找到軟件包 autoscan
將會安裝下列額外的軟件包:
automake autotools-dev m4
建議安裝的軟件包:
autoconf2.13 autobook autoconf-archive gnu-standards autoconf-doc libtool
gettext
下列【新】軟件包將被安裝:
autoconf automake autotools-dev m4
共升級了 0 個軟件包,新安裝了 4 個軟件包,要卸載 0 個軟件包,有 28 個軟件未被升級。
需要下載 1315kB 的軟件包。
解壓縮後會消耗掉 4366kB 的額外空間。
您希望繼續執行嗎?[Y/n]
輸入y,安裝
最好一起安裝它建議安裝的軟件包,否則autotools工具使用可能出錯。
root@ubuntu:~# sudo apt-get install autotools-dev m4 autoconf2.13 autobook autoconf-archive gnu-standards autoconf-doc libtool
使用:
官方文檔http://www.gnu.org/software/automake/manual/index.html
流程
第一部份:執行autoscan 修改configure.scan 成 configure.in
- # -*- Autoconf -*-
- # Process this file with autoconf to produce a configure script.
- AC_PREREQ(2.68)
- AC_INIT(hello,1.0)
- AM_INIT_AUTOMAKE(hello,1.0)
- AC_CONFIG_SRCDIR([hello.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_CONFIG_FILES([makefile])
- AC_OUTPUT
第二部份:執行aclocal 執行autoconf
第三部份: 執行autoheader
第四部份: 新建makefile.am 執行automake -a
- AUTOMAKE_OPTIONS=foreign
- bin_PROGRAMS= hello
- hello_SOURCES= hello.c
最後:
。/configure make make install make clean
教程:
http://public0821.iteye.com/blog/665786
這片文章不錯 : http://www.cnblogs.com/276815076/archive/2010/09/30/1839589.html
http://www.jcwcn.com/article-22327-1.html
文庫教程: http://wenku.baidu.com/view/bcb0074669eae009581becbb.html
實例:(應注意觀察每一步的執行結果是否正確,是否生成了想要的文件)
實例1:精簡實例很不錯
(我測試過沒問題 只是中間有個單詞寫錯了
vim Makefile.am
文件內容爲:
AUTOMAKE——OPTIONS=foreign
bin_PROGRAMS=hello
hello_SUORCES=hello.c 此處應爲 hello_SOURCES=hello.c
)
http://www.cnblogs.com/276815076/archive/2010/09/30/1839589.html
實例2:(未測試,但流程正確)
http://blog.163.com/adam_mhl/blog/static/64278267200710291130488/
建立目錄:mkdir include src
編寫程序:include/str.h
#include <stdio.h>
int str(char *string);
編寫程序:src/str.c
#include "str.h"
//print string
int str(char *string){
printf("\n----PRINT STRING----\n\"%s\"\n",string);
return 0;
}
//interface of this program
int main(int argc , char **argv){
char str_read[1024];
printf("Please INPUT something end by [ENTER]\n");
scanf("%s",str_read);
return str(str_read );
}
第一部份:(執行一條命令+修改一個文件)
3、生成configure.in
include src
[root@localhost str]#autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[root@localhost str]# ls
autoscan.log configure.scan(上面的命令生成此文件)
include src
[root@localhost str]# cp configure.scan configure.ac
修改 configure.scan 成 condigure.in:
1,修改AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)爲
AC_INIT(str,0.0.1, [[email protected]])
(註解:FULL-PACKAGE-NAME 爲程序名稱,VERSION爲當前版本, BUG-REPORT-ADDRESS爲bug彙報地址)
2,添加AM_INIT_AUTOMAKE
3,添加AC_CONFIG_FILES([Makefile])
修改後文件爲:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_INIT(str, 0.0.1, [[email protected]])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([include/str.h])
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_CONFIG_FILES([Makefile])
AC_OUTPUT
第二部份:(執行兩條命令)
執行aclocal
[root@localhost str]# aclocal
/usr/share/aclocal/libfame.m4:6: warning: underquoted definition of AM_PATH_LIBFAME
run info '(automake)Extending aclocal'
or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
執行autoconf
[root@localhost str]# autoconf
[root@localhost str]# ls
aclocal.m4 autoscan.log config.h.in configure.scan include Makefile.am NEWS
AUTHORS ChangeLog configure COPYING INSTALL Makefile.in README
autom4te.cache compile configure.ac depcomp install-sh missing src
第三部份(執行一條命令)
5、autoheader
root@localhost str]# autoheader
第四部份(新建文件+執行automake)
6、創建Makefile.am
[root@localhost str]# cat Makefile.am
#Makefile.am
bin_PROGRAMS = str
str_SOURCES = include/str.h src/str.c
str_CPPFLAGS = -I include/
7、automake必須文件(可略過此步):
* install-sh
* missing
* INSTALL
* NEWS
* README
* AUTHORS
* ChangeLog
* COPYING
* depcomp
其中
* install-sh
* missing
* INSTALL
* COPYING
* depcomp
可以通過automake -a選項自動生成,所以這裏只需要建立如下文件
[root@localhost str]# touch NEWS README AUTHORS ChangeLog(也可不加這一步)
8、執行automake -a
[root@localhost str]# automake -a
configure.ac: installing `./install-sh'
configure.ac: installing `./missing'
Makefile.am: installing `./INSTALL'
Makefile.am: installing `./COPYING'
Makefile.am: installing `./compile'
Makefile.am: installing `./depcomp'
最後一步:
10、執行測試:
執行./configure
執行 make 此時應該已經生成可執行文件,ls看一下
執行 make install
11、測試程序:#可執行文件
make clean 清除編譯過程生成的文件
make uninstall 卸載
12. 添加測試包:make dist-gzip
13.添加一個支持子目錄、靜態庫、自定義configure選項的包
支持子目錄Makefile.am 選項 SUBDIR =
#Automake interface
SUBDIRS = src
支持靜態庫Makefile.am
EXTRA_DIST 用於添加除源碼外的文件到dist包
#Automake interface
bin_PROGRAMS = hello
hello_SOURCES = hello.c lib/sbase.h
hello_CPPFLAGS = -I lib
hello_LDFLAGS = -static lib/libsbase.a
EXTRA_DIST = lib/libsbase.a
configure.ac:
AC_PREREQ(2.59)
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_INIT(hello, 0.0.1, [[email protected]])
#AM 聲明
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/hello.c])
AC_CONFIG_HEADER([config.h])
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([stdint.h stdlib.h sys/socket.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_TYPE_UINT32_T
AC_TYPE_UINT64_T
#用於自定義configure 選項,見acinclude.am
AC_CHECK_EXTRA_OPTIONS
# Checks for library functions.
AC_CONFIG_FILES([Makefile
src/Makefile])
AC_OUTPUT