Chinaunix首页 | 论坛 | 博客
  • 博客访问: 287496
  • 博文数量: 77
  • 博客积分: 1422
  • 博客等级: 上尉
  • 技术积分: 932
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-21 12:39
文章分类
文章存档

2011年(1)

2009年(3)

2008年(73)

我的朋友

分类:

2008-07-13 15:55:40


    最近开始学习linux c开发,对autotools比较感兴趣,所以找了一些国外的文档看了看,然后自己做了小例子,在这里跟大家分享。
wWkLinux联盟
1、准备:wWkLinux联盟
需要工具autoscan aclocal autoheader automake autoconf make 等工具.wWkLinux联盟

2、测试程序编写:wWkLinux联盟
建立目录:mkdir include srcwWkLinux联盟
    
编写程序:include/str.h
wWkLinux联

#include wWkLinux联盟
int str(char *string);
wWkLinux联盟
编写程序:src/str.c

#include "str.h"   (这里应该改成#include "../include/str.h")wWkLinux联盟
//print stringwWkLinux联盟
int str(char *string){wWkLinux联盟
        printf("\n----PRINT STRING----\n\"%s\"\n",string);wWkLinux联盟
        return 0;wWkLinux联盟
}wWkLinux联盟
wWkLinux联盟
//interface of this programwWkLinux联盟
int main(int argc , char **argv)
{wWkLinux联盟
        char str_read[1024];wWkLinux联盟
        printf("Please INPUT something end by [ENTER]\n");wWkLinux联盟
        scanf("%s", str_read);wWkLinux联盟
        return str(str_read );wWkLinux联盟
}wWkLinux联

3、生成configure.acwWkLinux联盟
  configure.ac是automake的输入文件,所以必须先生成该文件。wWkLinux联盟
  执行命令:


[root@localhost str]# lswWkLinux联盟
include  srcwWkLinux联盟
[root@localhost str]# autoscanwWkLinux联盟
autom4te: configure.ac: no such file or directorywWkLinux联盟
autoscan: /usr/bin/autom4te failed with exit status: 1wWkLinux联盟
[root@localhost str]# lswWkLinux联盟
autoscan.log  configure.scan  include  srcwWkLinux联盟
[root@localhost str]# cp configure.scan configure.ac
wWkLinux联盟
修改 configure.ac
#                   -*- Autoconf -*-wWkLinux联盟
# Process this file with autoconf to produce a configure script.wWkLinux联盟
wWkLinux联盟
AC_PREREQ(2.59)wWkLinux联盟
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)wWkLinux联盟
AC_CONFIG_SRCDIR([include/str.h])wWkLinux联盟
AC_CONFIG_HEADER([config.h])wWkLinux联盟
wWkLinux联盟
# Checks for programs.wWkLinux联盟
AC_PROG_CCwWkLinux联盟
wWkLinux联盟
# Checks for libraries.wWkLinux联盟
wWkLinux联盟
# Checks for header files.wWkLinux联盟
wWkLinux联盟
# Checks for typedefs, structures, and compiler characteristics.wWkLinux联盟
wWkLinux联盟
# Checks for library functions.wWkLinux联盟
AC_OUTPUT
wWkLinux联盟
修改
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
wWkLinux联盟
WkLinux联盟
CODE:
AC_INIT(str,0.0.1, [bug@sounos.org])
wWkLinux联盟
wWkLinux联盟FULL-PACKAGE-NAME 为程序名称,VERSION为当前版本, BUG-REPORT-ADDRESS为bug汇报地址

添加AM_INIT_AUTOMAKE wWkLinux联盟
添加AC_CONFIG_FILES([Makefile])wWkLinux联盟
wWkLinux联盟
CODE:
#                            -*- Autoconf -*-wWkLinux联盟
# Process this file with autoconf to produce a configure script.wWkLinux联盟
wWkLinux联盟
AC_PREREQ(2.59)wWkLinux联盟
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)wWkLinux联盟
AC_INIT(str, 0.0.1, [bug@sounos.org])wWkLinux联盟
AM_INIT_AUTOMAKEwWkLinux联盟
AC_CONFIG_SRCDIR([include/str.h])wWkLinux联盟
AC_CONFIG_HEADER([config.h])wWkLinux联盟
wWkLinux联盟
# Checks for programs.wWkLinux联盟
AC_PROG_CCwWkLinux联盟
wWkLinux联盟
# Checks for libraries.wWkLinux联盟
wWkLinux联盟
# Checks for header files.wWkLinux联盟
wWkLinux联盟
# Checks for typedefs, structures, and compiler characteristics.wWkLinux联盟
wWkLinux联盟
# Checks for library functions.wWkLinux联盟
AC_CONFIG_FILES([Makefile])wWkLinux联盟
AC_OUTPUT
wWkLinux联盟
4、执行aclocalwWkLinux联盟
wWkLinux联盟
CODE:
[root@localhost str]# aclocalwWkLinux联盟
/usr/share/aclocal/libfame.m4:6: warning: underquoted definition of AM_PATH_LIBFAMEwWkLinux联盟
  run info '(automake)Extending aclocal'wWkLinux联盟
  or see
wWkLinux联盟
5、制作Makefile.amwWkLinux联盟
wWkLinux联盟
CODE:
[root@localhost str]# cat Makefile.amwWkLinux联盟
#Makefile.amwWkLinux联盟
bin_PROGRAMS    = strwWkLinux联盟
str_SOURCES     = include/str.h src/str.cwWkLinux联盟
str_CPPFLAGS    = -I include/
wWkLinux联盟
6、autoheadernux联盟
wWkLinux联盟
CODE:
[root@localhost str]# autoheader
wWkLinux联盟
7、automake必须文件:wWkLinux联盟
wWkLinux联盟
CODE:
    *  install-shwWkLinux联盟
    * missingwWkLinux联盟
    * INSTALLwWkLinux联盟
    * NEWSwWkLinux联盟
    * READMEwWkLinux联盟
    * AUTHORSwWkLinux联盟
    * ChangeLogwWkLinux联盟
    * COPYINGwWkLinux联盟
    * depcomp
wWkLinux联盟
其中wWkLinux联盟
wWkLinux联盟
CODE:
    * install-shwWkLinux联盟
    * missingwWkLinux联盟
    * INSTALLwWkLinux联盟
    * COPYINGwWkLinux联盟
    * depcomp
wWkLinux联盟
可以通过automake -a选项自动生成,所以这里只需要建立如下文件wWkLinux联盟
wWkLinux联盟
CODE:
[root@localhost str]# touch NEWS README AUTHORS ChangeLog
wWkLinux联盟
8、执行automakewWkLinux联盟
wWkLinux联盟
CODE:
[root@localhost str]# automake -awWkLinux联盟
configure.ac: installing `./install-sh'wWkLinux联盟
configure.ac: installing `./missing'wWkLinux联盟
Makefile.am: installing `./INSTALL'wWkLinux联盟
Makefile.am: installing `./COPYING'wWkLinux联盟
Makefile.am: installing `./compile'wWkLinux联盟
Makefile.am: installing `./depcomp'
wWkLinux联盟
9、autoconfwWkLinux联盟
wWkLinux联盟
CODE:
[root@localhost str]# autoconfwWkLinux联盟
[root@localhost str]# lswWkLinux联盟
aclocal.m4      autoscan.log  config.h.in   configure.scan  include     Makefile.am  NEWSwWkLinux联盟
AUTHORS         ChangeLog     configure     COPYING         INSTALL     Makefile.in  READMEwWkLinux联盟
autom4te.cache  compile       configure.ac  depcomp         install-sh  missing      src
wWkLinux联盟
10、执行测试:wWkLinux联盟
执行./configurenux联盟
CODE:
[root@localhost str]# ./configure --prefix=/uwWkLinux联盟
checking for a BSD-compatible install... /usr/bin/install -cwWkLinux联盟
checking whether build environment is sane... yeswWkLinux联盟
checking for gawk... gawkwWkLinux联盟
checking whether make sets $(MAKE)... yeswWkLinux联盟
checking for gcc... gccwWkLinux联盟
checking for C compiler default output file name... a.outwWkLinux联盟
checking whether the C compiler works... yeswWkLinux联盟
checking whether we are cross compiling... nowWkLinux联盟
checking for suffix of executables...wWkLinux联盟
checking for suffix of object files... owWkLinux联盟
checking whether we are using the GNU C compiler... yeswWkLinux联盟
checking whether gcc accepts -g... yeswWkLinux联盟
checking for gcc option to accept ANSI C... none neededwWkLinux联盟
checking for style of include used by make... GNUwWkLinux联盟
checking dependency style of gcc... gcc3wWkLinux联盟
configure: creating ./config.statuswWkLinux联盟
config.status: creating MakefilewWkLinux联盟
config.status: creating config.hwWkLinux联盟
config.status: config.h is unchangedwWkLinux联盟
config.status: executing depfiles commands
wWkLinux联盟
执行 make
CODE:
[root@localhost str]# makewWkLinux联盟
make  all-amwWkLinux联盟
make[1]: Entering directory `/data/devel/c/str'wWkLinux联盟
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; \wWkLinux联盟
then mv -f ".deps/str-str.Tpo" ".deps/str-str.Po"; else rm -f ".deps/str-str.Tpo"; exit 1; fiwWkLinux联盟
gcc  -g -O2   -o str  str-str.owWkLinux联盟
make[1]: Leaving directory `/data/devel/c/str'
wWkLinux联盟
执行 make installwWkLinux联盟
wWkLinux联盟
CODE:
[root@localhost str]# make installwWkLinux联盟
make[1]: Entering directory `/data/devel/c/str'wWkLinux联盟
test -z "/u/bin" || mkdir -p -- "/u/bin"wWkLinux联盟
  /usr/bin/install -c 'str' '/u/bin/str'wWkLinux联盟
make[1]: Nothing to be done for `install-data-am'.wWkLinux联盟
make[1]: Leaving directory `/data/devel/c/str'     
wWkLinux联盟
11、测试程序:wWkLinux联盟

wWkLi
CODE:
[root@localhost str]# /u/bin/strwWkLinux联盟
Please INPUT something end by [ENTER]wWkLinux联盟
abcksdhfklsdklfdjlkfdwWkLinux联盟
wWkLinux联盟
----PRINT STRING----wWkLinux联盟
"abcksdhfklsdklfdjlkfd"
wWkLinux联盟
结束语:这只是一个小例子,如果要把这个用得很好需要不断的磨练。。。。呵呵,见笑了。wWkLinux联盟
wWkLinux联盟
wWkLinux联盟
wWkLinux联盟-----------------------------------------------------------------------wWkLinux联盟
wWkLinux联盟
添加测试包:wWkLinux联盟
wWkLinux联盟
CODE:
[root@localhost str]# make dist-gzipwWkLinux联盟
{ 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; }; }wWkLinux联盟
mkdir str-0.0.1wWkLinux联盟
find str-0.0.1 -type d ! -perm -777 -exec chmod a+rwx {} \; -o \wWkLinux联盟
  ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \wWkLinux联盟
  ! -type d ! -perm -400 -exec chmod a+r {} \; -o \wWkLinux联盟
  ! -type d ! -perm -444 -exec /bin/sh /data/devel/c/str/install-sh -c -m a+r {} {} \; \wWkLinux联盟
|| chmod -R a+r str-0.0.1wWkLinux联盟
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.gzwWkLinux联盟
{ 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; }; }
wWkLinux联盟
添加一个支持子目录、静态库、自定义configure选项的包wWkLinux联盟
wWkLinux联盟
支持子目录Makefile.am 选项 SUBDIR =wWkLinux联盟
wWkLinux联盟
CODE:
#Automake interface wWkLinux联盟
SUBDIRS = src
wWkLinux联盟
支持静态库Makefile.amwWkLinux联盟
EXTRA_DIST  用于添加除源码外的文件到dist包wWkLinux联盟
wWkLinux联盟
CODE:
#Automake interfacewWkLinux联盟
bin_PROGRAMS = hellowWkLinux联盟
hello_SOURCES = hello.c lib/sbase.hwWkLinux联盟
hello_CPPFLAGS = -I libwWkLinux联盟
hello_LDFLAGS = -static lib/libsbase.awWkLinux联盟
EXTRA_DIST = lib/libsbase.a
wWkLinux联盟
configure.acwWkLinux联盟
wWkLinux联盟
wWkLinux联盟
CODE:
AC_PREREQ(2.59)wWkLinux联盟
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)wWkLinux联盟
AC_INIT(hello, 0.0.1, [SounOS@gmail.com])wWkLinux联盟
#AM 声明wWkLinux联盟
AM_INIT_AUTOMAKEwWkLinux联盟
AC_CONFIG_SRCDIR([src/hello.c])wWkLinux联盟
AC_CONFIG_HEADER([config.h])wWkLinux联盟
wWkLinux联盟
# Checks for programs.wWkLinux联盟
AC_PROG_CCwWkLinux联盟
wWkLinux联盟
# Checks for libraries.wWkLinux联盟
wWkLinux联盟
# Checks for header files.wWkLinux联盟
AC_HEADER_STDCwWkLinux联盟
AC_CHECK_HEADERS([stdint.h stdlib.h sys/socket.h])wWkLinux联盟
wWkLinux联盟
# Checks for typedefs, structures, and compiler characteristics.wWkLinux联盟
AC_C_CONSTwWkLinux联盟
AC_TYPE_SIZE_TwWkLinux联盟
AC_TYPE_UINT32_TwWkLinux联盟
AC_TYPE_UINT64_TwWkLinux联盟
wWkLinux联盟
#用于自定义configure 选项,见acinclude.amwWkLinux联盟
AC_CHECK_EXTRA_OPTIONSwWkLinux联盟
# Checks for library functions.wWkLinux联盟
wWkLinux联盟
AC_CONFIG_FILES([MakefilewWkLinux联盟
                 src/Makefile])wWkLinux联盟
AC_OUTPUT
阅读(1712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~