autotool的使用

来源:互联网 发布:php配置环境 编辑:程序博客网 时间:2024/06/06 15:46

最近做的项目要用autotools,所以看了下:

Autotools: a practitioner's guideto Autoconf, Automake and Libtool

其实autotool用的很简单,这里介绍一种非lib的使用方法(lib的使用放到以后):

[plain] view plaincopy
  1. autoreconf -i --force  
  2. ./configure  
  3. make  

比如在一个openssl项目里,下面是一个实例:

[plain] view plaincopy
  1. root@test:~/sslclient# ls -lp  
  2. 總計 28  
  3. drwxr-xr-x 2 root root 4096 2012-08-24 16:39 bin/  
  4. drwxr-xr-x 2 root root 4096 2012-08-24 12:01 cacerts/  
  5. drwxr-xr-x 2 root root 4096 2012-08-24 12:02 certs/  
  6. drwxr-xr-x 2 root root 4096 2012-08-24 12:02 private/  
  7. drwxr-xr-x 2 root root 4096 2012-08-24 17:11 src/  


在src文件夹存放客户端和服务器端的实现,它们都依赖于ssl,encrypto两个动态库

[plain] view plaincopy
  1.   
[plain] view plaincopy
  1. root@test:~/sslclient# ls -lp src  
  2. 總計 36  
  3. -rw-r--r-- 1 root root  7963 2012-08-21 15:44 sample_cli.c  
  4. -rw-r--r-- 1 root root  7884 2012-08-21 15:43 sample_srv.c  


bin: 目录用于存放可执行文件

cacerts: 存放证书发行机构的证书

certs: 存放客户端和服务器证书

private: 存放秘钥


首先在工程的根目录添加configure.ac文件,这个文件可从autoscan中生成的configure.scan修改

[plain] view plaincopy
  1. root@test:~/sslclient# cat configure.ac   
  2. #                                               -*- Autoconf -*-  
  3. # Process this file with autoconf to produce a configure script.  
  4.   
  5.   
  6. AC_PREREQ([2.67])                          #autoconf的版本  
  7. AC_INIT([ssl_sample], [0.0.1], [test@qq.com])  
  8. #[ssl_sample]:项目名称  
  9. #[0.0.1]:版本号  
  10. #[test@qq.com]:开发者的联系方式  
  11. AM_INIT_AUTOMAKE  
  12. #初始化automake  
  13. AC_CONFIG_SRCDIR([src/sample_srv.c])  
  14. #在项目文件夹的随便一个文件  
  15. AC_CONFIG_HEADERS([config.h])  
  16. # 这个宏的意思是,从将模板里的@***@变量替换掉生成config.h头文件。模板是什么?默认是*.in文件,比如Makefile.in,这里就是config.h.in  
  17.   
  18. # Checks for programs.  
  19. AC_PROG_CC  
  20.   
  21.   
  22. # Checks for libraries.  
  23. AC_SEARCH_LIBS([SSL_new], [ssl])  
  24. # 调用SSL_new来测试,是否存在ssl库  
  25. AC_SEARCH_LIBS([X509_NAME_oneline], [crypto])  
  26. # 调用X509_NAME_oneline来测试,是否存在crypto库  
  27.   
  28. # Checks for header files.  
  29. AC_CHECK_HEADERS([arpa/inet.h netdb.h netinet/in.h stdlib.h string.h sys/socket.h unistd.h])  
  30.   
  31.   
  32. AC_CONFIG_FILES([Makefile  
  33.                  src/Makefile])  
  34. # AC_CONFIG_FILES(file……, [command])  
  35. # ‘file’ 参数指要创建的文件,该文件复制于一份输入文件(默认为‘file.in’),并替换掉输出变量值。‘file’就是一份输出文件,称之为系统编译环境配 置文件更准确一些。譬如,若‘FOO’是一个输出变量,而它的值为‘Bar’,那么在文件‘file.in’中出现的所有‘@FOO@’,在输出文件中都 会被替换为‘Bar’  
  36.   
  37. AC_OUTPUT  
  38. # 用于完成所有输出  

2.在工程的根目录添加Makefile.am,为Makefile.in的模板

[plain] view plaincopy
  1. root@test:~/sslclient# cat Makefile.am   
  2. SUBDIRS = src  
  3. #指定源代码的目录  

3.在源代码目录添加Makefile.am

[plain] view plaincopy
  1. root@test:~/sslclient# cat src/Makefile.am  
  2. bin_PROGRAMS = samplesrv samplecli  
  3. #指定要输出的二进制文件  
  4. samplesrv_SOURCES = sample_srv.c  
  5. samplecli_SOURCES = sample_cli.c  
  6. #指定二进制文件对应的C代码  

好了,必须的配置文件都写好了,就可以跑了:

[plain] view plaincopy
  1. root@test:~/sslclient# autoreconf -i  
  2. configure.ac:6: installing `./install-sh'  
  3. configure.ac:6: installing `./missing'  
  4. src/Makefile.am: installing `./depcomp'  
  5. Makefile.am: installing `./INSTALL'  
  6. Makefile.am: required file `./NEWS' not found  
  7. Makefile.am: required file `./README' not found  
  8. Makefile.am: required file `./AUTHORS' not found  
  9. Makefile.am: required file `./ChangeLog' not found  
  10. Makefile.am: installing `./COPYING' using GNU General Public License v3 file  
  11. Makefile.am:     Consider adding the COPYING file to the version control system  
  12. Makefile.am:     for your code, to avoid questions about which license your project uses.  
  13. autoreconf: automake failed with exit status: 1  

首先man一下,看autoreconf用到的两个参数

[plain] view plaincopy
  1. -i, --install  
  2.               copy missing auxiliary files  

虽然指定了-i,拷贝所有的缺失文件,但

INSTALL  NEWS  README  AUTHORS  ChangeLog  COPYING必须手动加的。

[plain] view plaincopy
  1. root@test:~/sslclient# touch INSTALL  NEWS  README  AUTHORS  ChangeLog  COPYING  
[plain] view plaincopy
  1. root@test:~/sslclient# autoreconf -i  
[plain] view plaincopy
  1. root@test:~/sslclient# ls -lp  
  2. 總計 356  
  3. -rw-r--r-- 1 root root  34611 2012-08-25 03:55 aclocal.m4  
  4. -rw-r--r-- 1 root root      0 2012-08-25 04:00 AUTHORS  
  5. drwxr-xr-x 2 root root   4096 2012-08-25 03:55 autom4te.cache/  
  6. drwxr-xr-x 2 root root   4096 2012-08-24 16:39 bin/  
  7. drwxr-xr-x 2 root root   4096 2012-08-24 12:01 cacerts/  
  8. drwxr-xr-x 2 root root   4096 2012-08-24 12:02 certs/  
  9. -rw-r--r-- 1 root root      0 2012-08-25 04:00 ChangeLog  
  10. -rw-r--r-- 1 root root   1777 2012-08-25 03:55 config.h.in  
  11. -rwxr-xr-x 1 root root 162634 2012-08-25 03:55 configure  
  12. -rw-r--r-- 1 root root    615 2012-08-24 16:13 configure.ac  
  13. -rw-r--r-- 1 root root  35147 2012-08-25 04:00 COPYING  
  14. -rwxr-xr-x 1 root root  18615 2012-08-25 03:55 depcomp  
  15. -rw-r--r-- 1 root root  15578 2012-08-25 04:00 INSTALL  
  16. -rwxr-xr-x 1 root root  13663 2012-08-25 03:55 install-sh  
  17. -rw-r--r-- 1 root root     15 2012-08-24 15:35 Makefile.am  
  18. -rw-r--r-- 1 root root  21430 2012-08-25 04:03 Makefile.in  
  19. -rwxr-xr-x 1 root root  11419 2012-08-25 03:55 missing  
  20. -rw-r--r-- 1 root root      0 2012-08-25 04:00 NEWS  
  21. drwxr-xr-x 2 root root   4096 2012-08-24 12:02 private/  
  22. -rw-r--r-- 1 root root      0 2012-08-25 04:00 README  
  23. drwxr-xr-x 2 root root   4096 2012-08-25 03:55 src/  
生成了大量熟悉的文件,基本都可以在开源项目中看到。

[plain] view plaincopy
  1. root@test:~/sslclient# ./configure  
  2. checking for a BSD-compatible install... /usr/bin/install -c  
  3. checking whether build environment is sane... yes  
  4. checking for a thread-safe mkdir -p... /bin/mkdir -p  
  5. checking for gawk... no  
  6. checking for mawk... mawk  
  7. checking whether make sets $(MAKE)... yes  
  8. checking for gcc... gcc  
  9. checking whether the C compiler works... yes  
  10. checking for C compiler default output file name... a.out  
  11. checking for suffix of executables...   
  12. checking whether we are cross compiling... no  
  13. checking for suffix of object files... o  
  14. checking whether we are using the GNU C compiler... yes  
  15. checking whether gcc accepts -g... yes  
  16. checking for gcc option to accept ISO C89... none needed  
  17. checking for style of include used by make... GNU  
  18. checking dependency style of gcc... gcc3  
  19. checking for library containing SSL_new... -lssl<span style="white-space:pre">          </span>#我们在configure.ac中加入的AC_SEARCH_LIBS([SSL_new], [ssl])  
  20. checking for library containing X509_NAME_oneline... none required<span style="white-space:pre">    </span>#这个是AC_SEARCH_LIBS([X509_NAME_oneline], [crypto])  
  21. checking how to run the C preprocessor... gcc -E  
  22. checking for grep that handles long lines and -e... /bin/grep  
  23. checking for egrep... /bin/grep -E  
  24. checking for ANSI C header files... yes  
  25. checking for sys/types.h... yes  
  26. checking for sys/stat.h... yes  
  27. checking for stdlib.h... yes  
  28. checking for string.h... yes  
  29. checking for memory.h... yes  
  30. checking for strings.h... yes  
  31. checking for inttypes.h... yes  
  32. checking for stdint.h... yes  
  33. checking for unistd.h... yes  
  34. checking arpa/inet.h usability... yes  
  35. checking arpa/inet.h presence... yes  
  36. checking for arpa/inet.h... yes  
  37. checking netdb.h usability... yes  
  38. checking netdb.h presence... yes  
  39. checking for netdb.h... yes  
  40. checking netinet/in.h usability... yes  
  41. checking netinet/in.h presence... yes  
  42. checking for netinet/in.h... yes  
  43. checking for stdlib.h... (cached) yes  
  44. checking for string.h... (cached) yes  
  45. checking sys/socket.h usability... yes  
  46. checking sys/socket.h presence... yes  
  47. checking for sys/socket.h... yes  
  48. checking for unistd.h... (cached) yes  
  49. configure: creating ./config.status  
  50. config.status: creating Makefile  
  51. config.status: creating src/Makefile  
  52. config.status: creating config.h  
  53. config.status: executing depfiles commands  

configure成功了,现在你可以看到在项目的根目录和src目录,都有Makefile.in,和Makefile生成了。

[plain] view plaincopy
  1. root@test:~sslclient# make  
  2. 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语句,是不是很方便呢?
0 0
原创粉丝点击