Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1144126
  • 博文数量: 234
  • 博客积分: 5592
  • 博客等级: 大校
  • 技术积分: 1987
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-23 14:12
文章分类

全部博文(234)

文章存档

2015年(1)

2013年(4)

2012年(16)

2011年(204)

2010年(9)

分类: LINUX

2011-08-05 15:45:02

最近开始学习linux c开发,对autotools比较感兴趣,所以找了一些国外的文档看了看,然后自己做了小例子,在这里跟大家分享。
  1、准备:
     需要工具autoscan aclocal autoheader automake autoconf make 等工具.
  2、测试程序编写:
     建立目录:mkdir include src
     编写程序:include/str.h
  1. #include
  2. int str(char *string);
复制代码

      编写程序:src/str.c
   
  1. #include "str.h"
  2. //print string
  3. int str(char *string){
  4.         printf("\n----PRINT STRING----\n\"%s\"\n",string);
  5.         return 0;
  6. }

  7. //interface of this program
  8. int main(int argc , char **argv){
  9.         char str_read[1024];
  10.         printf("Please INPUT something end by [ENTER]\n");
  11.         scanf("%s",str_read);
  12.         return str(str_read );
  13. }

  14.      
复制代码


  3、生成configure.ac
    configure.ac是automake的输入文件,所以必须先生成该文件。
    执行命令:
  1. [root@localhost str]# ls
  2. include  src
  3. [root@localhost str]# autoscan
  4. autom4te: configure.ac: no such file or directory
  5. autoscan: /usr/bin/autom4te failed with exit status: 1
  6. [root@localhost str]# ls
  7. autoscan.log  configure.scan  include  src
  8. [root@localhost str]# cp configure.scan configure.ac
复制代码

    修改 configure.ac 
  1. #                                               -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.

  3. AC_PREREQ(2.59)
  4. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
  5. AC_CONFIG_SRCDIR([include/str.h])
  6. AC_CONFIG_HEADER([config.h])

  7. # Checks for programs.
  8. AC_PROG_CC

  9. # Checks for libraries.

  10. # Checks for header files.

  11. # Checks for typedefs, structures, and compiler characteristics.

  12. # Checks for library functions.
  13. AC_OUTPUT
复制代码

     修改
  1. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
复制代码

      为
  1. AC_INIT(str,0.0.1, [bug@sounos.org])
复制代码


FULL-PACKAGE-NAME 为程序名称,VERSION为当前版本, BUG-REPORT-ADDRESS为bug汇报地址
    添加AM_INIT_AUTOMAKE 
    添加AC_CONFIG_FILES([Makefile])
  1. #                                               -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.

  3. AC_PREREQ(2.59)
  4. #AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
  5. AC_INIT(str, 0.0.1, [bug@sounos.org])
  6. AM_INIT_AUTOMAKE
  7. AC_CONFIG_SRCDIR([include/str.h])
  8. AC_CONFIG_HEADER([config.h])

  9. # Checks for programs.
  10. AC_PROG_CC

  11. # Checks for libraries.

  12. # Checks for header files.

  13. # Checks for typedefs, structures, and compiler characteristics.

  14. # Checks for library functions.
  15. AC_CONFIG_FILES([Makefile])
  16. AC_OUTPUT
复制代码

4、执行aclocal
  1. [root@localhost str]# aclocal
  2. /usr/share/aclocal/libfame.m4:6: warning: underquoted definition of AM_PATH_LIBFAME
  3.   run info '(automake)Extending aclocal'
  4.   or see redhat.com/automake/automake.html#Extending-aclocal
复制代码

5、制作Makefile.am
  1. [root@localhost str]# cat Makefile.am
  2. #Makefile.am
  3. bin_PROGRAMS    = str
  4. str_SOURCES     = include/str.h src/str.c
  5. str_CPPFLAGS    = -I include/
复制代码

6、autoheader
  1. [root@localhost str]# autoheader
复制代码

7、automake必须文件:
  1.     *  install-sh
  2.     * missing
  3.     * INSTALL
  4.     * NEWS
  5.     * README
  6.     * AUTHORS
  7.     * ChangeLog
  8.     * COPYING
  9.     * depcomp
复制代码

其中 
  1.     * install-sh
  2.     * missing
  3.     * INSTALL
  4.     * COPYING
  5.     * depcomp
复制代码

可以通过automake -a选项自动生成,所以这里只需要建立如下文件
  1. [root@localhost str]# touch NEWS README AUTHORS ChangeLog
复制代码

8、执行automake
  1. [root@localhost str]# automake -a
  2. configure.ac: installing `./install-sh'
  3. configure.ac: installing `./missing'
  4. Makefile.am: installing `./INSTALL'
  5. Makefile.am: installing `./COPYING'
  6. Makefile.am: installing `./compile'
  7. Makefile.am: installing `./depcomp'
复制代码

9、autoconf
  1. [root@localhost str]# autoconf
  2. [root@localhost str]# ls
  3. aclocal.m4      autoscan.log  config.h.in   configure.scan  include     Makefile.am  NEWS
  4. AUTHORS         ChangeLog     configure     COPYING         INSTALL     Makefile.in  README
  5. autom4te.cache  compile       configure.ac  depcomp         install-sh  missing      src
复制代码

10、执行测试:
      执行./configure
  1. [root@localhost str]# ./configure --prefix=/u
  2. checking for a BSD-compatible install... /usr/bin/install -c
  3. checking whether build environment is sane... yes
  4. checking for gawk... gawk
  5. checking whether make sets $(MAKE)... yes
  6. checking for gcc... gcc
  7. checking for C compiler default output file name... a.out
  8. checking whether the C compiler works... yes
  9. checking whether we are cross compiling... no
  10. checking for suffix of executables...
  11. checking for suffix of object files... o
  12. checking whether we are using the GNU C compiler... yes
  13. checking whether gcc accepts -g... yes
  14. checking for gcc option to accept ANSI C... none needed
  15. checking for style of include used by make... GNU
  16. checking dependency style of gcc... gcc3
  17. configure: creating ./config.status
  18. config.status: creating Makefile
  19. config.status: creating config.h
  20. config.status: config.h is unchanged
  21. config.status: executing depfiles commands
复制代码

       执行 make
  1. [root@localhost str]# make
  2. make  all-am
  3. make[1]: Entering directory `/data/devel/c/str'
  4. if gcc -DHAVE_CONFIG_H -I. -I. -I.  -I include/   -g -O2 -MT str-str.o -MD -MP -MF ".deps/str-str.Tpo" -c -o str-str.o `test -f 'src/str.c' || echo './'`src/str.c; \
  5. then mv -f ".deps/str-str.Tpo" ".deps/str-str.Po"; else rm -f ".deps/str-str.Tpo"; exit 1; fi
  6. gcc  -g -O2   -o str  str-str.o
  7. make[1]: Leaving directory `/data/devel/c/str'
复制代码

         执行 make install
  1. [root@localhost str]# make install
  2. make[1]: Entering directory `/data/devel/c/str'
  3. test -z "/u/bin" || mkdir -p -- "/u/bin"
  4.   /usr/bin/install -c 'str' '/u/bin/str'
  5. make[1]: Nothing to be done for `install-data-am'.
  6. make[1]: Leaving directory `/data/devel/c/str'     
复制代码

11、测试程序:
  1. [root@localhost str]# /u/bin/str
  2. Please INPUT something end by [ENTER]
  3. abcksdhfklsdklfdjlkfd

  4. ----PRINT STRING----
  5. "abcksdhfklsdklfdjlkfd"
复制代码

结束语:这只是一个小例子,如果要把这个用得很好需要不断的磨练。。。。呵呵,见笑了。



-----

添加测试包:
  1. [root@localhost str]# make dist-gzip
  2. { test ! -d str-0.0.1 || { find str-0.0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr str-0.0.1; }; }
  3. mkdir str-0.0.1
  4. find str-0.0.1 -type d ! -perm -777 -exec chmod a+rwx {} \; -o \
  5.   ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
  6.   ! -type d ! -perm -400 -exec chmod a+r {} \; -o \
  7.   ! -type d ! -perm -444 -exec /bin/sh /data/devel/c/str/install-sh -c -m a+r {} {} \; \
  8. || chmod -R a+r str-0.0.1
  9. tardir=str-0.0.1 && /bin/sh /data/devel/c/str/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >str-0.0.1.tar.gz
  10. { test ! -d str-0.0.1 || { find str-0.0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr str-0.0.1; }; }
复制代码



添加一个支持子目录、静态库、自定义configure选项的包

支持子目录Makefile.am 选项 SUBDIR = 
  1. #Automake interface 
  2. SUBDIRS = src
复制代码


支持静态库Makefile.am
EXTRA_DIST  用于添加除源码外的文件到dist包

  1. #Automake interface
  2. bin_PROGRAMS = hello
  3. hello_SOURCES = hello.c lib/sbase.h
  4. hello_CPPFLAGS = -I lib
  5. hello_LDFLAGS = -static lib/libsbase.a
  6. EXTRA_DIST = lib/libsbase.a
复制代码

configure.ac
  1. AC_PREREQ(2.59)
  2. #AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
  3. AC_INIT(hello, 0.0.1, [SounOS@gmail.com])
  4. #AM 声明
  5. AM_INIT_AUTOMAKE
  6. AC_CONFIG_SRCDIR([src/hello.c])
  7. AC_CONFIG_HEADER([config.h])

  8. # Checks for programs.
  9. AC_PROG_CC

  10. # Checks for libraries.

  11. # Checks for header files.
  12. AC_HEADER_STDC
  13. AC_CHECK_HEADERS([stdint.h stdlib.h sys/socket.h])

  14. # Checks for typedefs, structures, and compiler characteristics.
  15. AC_C_CONST
  16. AC_TYPE_SIZE_T
  17. AC_TYPE_UINT32_T
  18. AC_TYPE_UINT64_T

  19. #用于自定义configure 选项,见acinclude.am
  20. AC_CHECK_EXTRA_OPTIONS
  21. # Checks for library functions.

  22. AC_CONFIG_FILES([Makefile
  23.                  src/Makefile])
  24. AC_OUTPUT
复制代码

转:
阅读(868) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~