最近做的项目要用autotools,所以看了下:
《Autotools: a practitioner's guideto Autoconf, Automake and Libtool》
其实autotool用的很简单,这里介绍一种非lib的使用方法(lib的使用放到以后):
- autoreconf -i --force
- ./configure
- make
比如在一个openssl项目里,下面是一个实例:
- root@test:~/sslclient# ls -lp
- 總計 28
- drwxr-xr-x 2 root root 4096 2012-08-24 16:39 bin/
- drwxr-xr-x 2 root root 4096 2012-08-24 12:01 cacerts/
- drwxr-xr-x 2 root root 4096 2012-08-24 12:02 certs/
- drwxr-xr-x 2 root root 4096 2012-08-24 12:02 private/
- drwxr-xr-x 2 root root 4096 2012-08-24 17:11 src/
在src文件夹存放客户端和服务器端的实现,它们都依赖于ssl,encrypto两个动态库
- root@test:~/sslclient# ls -lp src
- 總計 36
- -rw-r--r-- 1 root root 7963 2012-08-21 15:44 sample_cli.c
- -rw-r--r-- 1 root root 7884 2012-08-21 15:43 sample_srv.c
bin: 目录用于存放可执行文件
cacerts: 存放证书发行机构的证书
certs: 存放客户端和服务器证书
private: 存放秘钥
首先在工程的根目录添加configure.ac文件,这个文件可从autoscan中生成的configure.scan修改
- root@test:~/sslclient# cat configure.ac
- # -*- Autoconf -*-
- # Process this file with autoconf to produce a configure script.
- AC_PREREQ([2.67]) #autoconf的版本
- AC_INIT([ssl_sample], [0.0.1], [test@qq.com])
- #[ssl_sample]:项目名称
- #[0.0.1]:版本号
- #[test@qq.com]:开发者的联系方式
- AM_INIT_AUTOMAKE
- #初始化automake
- AC_CONFIG_SRCDIR([src/sample_srv.c])
- #在项目文件夹的随便一个文件
- AC_CONFIG_HEADERS([config.h])
- # 这个宏的意思是,从将模板里的@***@变量替换掉生成config.h头文件。模板是什么?默认是*.in文件,比如Makefile.in,这里就是config.h.in
- # Checks for programs.
- AC_PROG_CC
- # Checks for libraries.
- AC_SEARCH_LIBS([SSL_new], [ssl])
- # 调用SSL_new来测试,是否存在ssl库
- AC_SEARCH_LIBS([X509_NAME_oneline], [crypto])
- # 调用X509_NAME_oneline来测试,是否存在crypto库
- # Checks for header files.
- AC_CHECK_HEADERS([arpa/inet.h netdb.h netinet/in.h stdlib.h string.h sys/socket.h unistd.h])
- AC_CONFIG_FILES([Makefile
- src/Makefile])
- # AC_CONFIG_FILES(file……, [command])
- # ‘file’ 参数指要创建的文件,该文件复制于一份输入文件(默认为‘file.in’),并替换掉输出变量值。‘file’就是一份输出文件,称之为系统编译环境配 置文件更准确一些。譬如,若‘FOO’是一个输出变量,而它的值为‘Bar’,那么在文件‘file.in’中出现的所有‘@FOO@’,在输出文件中都 会被替换为‘Bar’
- AC_OUTPUT
- # 用于完成所有输出
2.在工程的根目录添加Makefile.am,为Makefile.in的模板
- root@test:~/sslclient# cat Makefile.am
- SUBDIRS = src
- #指定源代码的目录
3.在源代码目录添加Makefile.am
- root@test:~/sslclient# cat src/Makefile.am
- bin_PROGRAMS = samplesrv samplecli
- #指定要输出的二进制文件
- samplesrv_SOURCES = sample_srv.c
- samplecli_SOURCES = sample_cli.c
- #指定二进制文件对应的C代码
好了,必须的配置文件都写好了,就可以跑了:
- root@test:~/sslclient# autoreconf -i
- configure.ac:6: installing `./install-sh'
- configure.ac:6: installing `./missing'
- src/Makefile.am: installing `./depcomp'
- Makefile.am: installing `./INSTALL'
- Makefile.am: required file `./NEWS' not found
- Makefile.am: required file `./README' not found
- Makefile.am: required file `./AUTHORS' not found
- Makefile.am: 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.
- autoreconf: automake failed with exit status: 1
首先man一下,看autoreconf用到的两个参数
- -i, --install
- copy missing auxiliary files
虽然指定了-i,拷贝所有的缺失文件,但
INSTALL NEWS README AUTHORS ChangeLog COPYING必须手动加的。
- root@test:~/sslclient# touch INSTALL NEWS README AUTHORS ChangeLog COPYING
- root@test:~/sslclient# autoreconf -i
- root@test:~/sslclient# ls -lp
- 總計 356
- -rw-r--r-- 1 root root 34611 2012-08-25 03:55 aclocal.m4
- -rw-r--r-- 1 root root 0 2012-08-25 04:00 AUTHORS
- drwxr-xr-x 2 root root 4096 2012-08-25 03:55 autom4te.cache/
- drwxr-xr-x 2 root root 4096 2012-08-24 16:39 bin/
- drwxr-xr-x 2 root root 4096 2012-08-24 12:01 cacerts/
- drwxr-xr-x 2 root root 4096 2012-08-24 12:02 certs/
- -rw-r--r-- 1 root root 0 2012-08-25 04:00 ChangeLog
- -rw-r--r-- 1 root root 1777 2012-08-25 03:55 config.h.in
- -rwxr-xr-x 1 root root 162634 2012-08-25 03:55 configure
- -rw-r--r-- 1 root root 615 2012-08-24 16:13 configure.ac
- -rw-r--r-- 1 root root 35147 2012-08-25 04:00 COPYING
- -rwxr-xr-x 1 root root 18615 2012-08-25 03:55 depcomp
- -rw-r--r-- 1 root root 15578 2012-08-25 04:00 INSTALL
- -rwxr-xr-x 1 root root 13663 2012-08-25 03:55 install-sh
- -rw-r--r-- 1 root root 15 2012-08-24 15:35 Makefile.am
- -rw-r--r-- 1 root root 21430 2012-08-25 04:03 Makefile.in
- -rwxr-xr-x 1 root root 11419 2012-08-25 03:55 missing
- -rw-r--r-- 1 root root 0 2012-08-25 04:00 NEWS
- drwxr-xr-x 2 root root 4096 2012-08-24 12:02 private/
- -rw-r--r-- 1 root root 0 2012-08-25 04:00 README
- drwxr-xr-x 2 root root 4096 2012-08-25 03:55 src/
- root@test:~/sslclient# ./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... no
- checking for mawk... mawk
- checking whether make sets $(MAKE)... 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 for style of include used by make... GNU
- checking dependency style of gcc... gcc3
- checking for library containing SSL_new... -lssl<span style="white-space:pre"> </span>#我们在configure.ac中加入的AC_SEARCH_LIBS([SSL_new], [ssl])
- checking for library containing X509_NAME_oneline... none required<span style="white-space:pre"> </span>#这个是AC_SEARCH_LIBS([X509_NAME_oneline], [crypto])
- checking how to run the C preprocessor... gcc -E
- checking for grep that handles long lines and -e... /bin/grep
- checking for egrep... /bin/grep -E
- checking for ANSI C header files... yes
- checking for sys/types.h... yes
- checking for sys/stat.h... yes
- checking for stdlib.h... yes
- checking for string.h... yes
- checking for memory.h... yes
- checking for strings.h... yes
- checking for inttypes.h... yes
- checking for stdint.h... yes
- checking for unistd.h... yes
- checking arpa/inet.h usability... yes
- checking arpa/inet.h presence... yes
- checking for arpa/inet.h... yes
- checking netdb.h usability... yes
- checking netdb.h presence... yes
- checking for netdb.h... yes
- checking netinet/in.h usability... yes
- checking netinet/in.h presence... yes
- checking for netinet/in.h... yes
- checking for stdlib.h... (cached) yes
- checking for string.h... (cached) yes
- checking sys/socket.h usability... yes
- checking sys/socket.h presence... yes
- checking for sys/socket.h... yes
- checking for unistd.h... (cached) yes
- configure: creating ./config.status
- config.status: creating Makefile
- config.status: creating src/Makefile
- config.status: creating config.h
- config.status: executing depfiles commands
configure成功了,现在你可以看到在项目的根目录和src目录,都有Makefile.in,和Makefile生成了。
- root@test:~sslclient# make
- make all-recursivemake[1]: Entering directory `/home/sunbird/workplace/sslclient'Making all in srcmake[2]: Entering directory `/home/sunbird/workplace/sslclient/src'gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT sample_srv.o -MD -MP -MF .deps/sample_srv.Tpo -c -o sample_srv.o sample_srv.csample_srv.c: In function ‘main’:sample_srv.c:205:37: warning: passing argument 3 of ‘accept’ from incompatible pointer type/usr/include/sys/socket.h:214:12: note: expected ‘socklen_t * __restrict__’ but argument is of type ‘size_t *’sample_srv.c:210:2: warning: format ‘%lx’ expects type ‘long unsigned int’, but argument 2 has type ‘in_addr_t’mv -f .deps/sample_srv.Tpo .deps/sample_srv.Pogcc -g -O2 -o samplesrv sample_srv.o -lssl gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT sample_cli.o -MD -MP -MF .deps/sample_cli.Tpo -c -o sample_cli.o sample_cli.csample_cli.c: In function ‘main’:sample_cli.c:147:8: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_resultmv -f .deps/sample_cli.Tpo .deps/sample_cli.Pogcc -g -O2 -o samplecli sample_cli.o -lssl make[2]: Leaving directory `/home/sunbird/workplace/sslclient/src'make[2]: Entering directory `/home/sunbird/workplace/sslclient'make[2]: Leaving directory `/home/sunbird/workplace/sslclient'make[1]: Leaving directory `/home/sunbird/workplace/sslclient'
呵呵,寥寥数语就生成了那么多漂亮的Makefile语句,是不是很方便呢?