视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
autotools
2025-10-03 14:34:30 责编:小OO
文档
[原创] 
学习使用autotools

http://www.chinaunix.net 作者:redor  发表于:2008-10-21 17:53:02

【发表评论】【查看原文】【C/C++讨论区】【关闭】 

最近开始学习linux c开发,对autotools比较感兴趣,所以找了一些国外的文档看了看,然后自己做了小例子,在这里跟大家分享。 

  1、准备: 

     需要工具autoscan aclocal autoheader automake autoconf make 等工具. 

  2、测试程序编写: 

     建立目录:mkdir include src 

     编写程序:include/str.h 

#include 

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.ac 

    configure.ac是automake的输入文件,所以必须先生成该文件。 

    执行命令: 

 [root@localhost str]# ls

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.ac  

#                                               -*- 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_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_OUTPUT

     修改 

AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)

      为 

AC_INIT(str,0.0.1, [bug@sounos.org])

FULL-PACKAGE-NAME 为程序名称,VERSION为当前版本, BUG-REPORT-ADDRESS为bug汇报地址 

    添加[color=red]AM_INIT_AUTOMAKE[/color]  

    添加[color=red]AC_CONFIG_FILES([Makefile])[/color] 

#                                               -*- 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, [bug@sounos.org])

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

4、执行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

5、制作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/

6、autoheader 

[root@localhost str]# autoheader

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 

[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'

9、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

10、执行测试: 

      执行./configure 

[root@localhost str]# ./configure --prefix=/u

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking for gcc... gcc

checking for C compiler default output file name... a.out

checking whether the C compiler works... yes

checking whether we are cross compiling... no

checking for suffix of executables...

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 ANSI C... none needed

checking for style of include used by make... GNU

checking dependency style of gcc... gcc3

configure: creating ./config.status

config.status: creating Makefile

config.status: creating config.h

config.status: config.h is unchanged

config.status: executing depfiles commands

       执行 make 

[root@localhost str]# make

make  all-am

make[1]: Entering directory `/data/devel/c/str'

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; \

then mv -f ".deps/str-str.Tpo" ".deps/str-str.Po"; else rm -f ".deps/str-str.Tpo"; exit 1; fi

gcc  -g -O2   -o str  str-str.o

make[1]: Leaving directory `/data/devel/c/str'

         执行 make install 

[root@localhost str]# make install

make[1]: Entering directory `/data/devel/c/str'

test -z "/u/bin" || mkdir -p -- "/u/bin"

  /usr/bin/install -c 'str' '/u/bin/str'

make[1]: Nothing to be done for `install-data-am'.

make[1]: Leaving directory `/data/devel/c/str'     

11、测试程序: 

[root@localhost str]# /u/bin/str

Please INPUT something end by [ENTER]

abcksdhfklsdklfdjlkfd

----PRINT STRING----

"abcksdhfklsdklfdjlkfd"

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

----- 

添加测试包: 

[root@localhost str]# make dist-gzip

{ 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; }; }

mkdir str-0.0.1

find str-0.0.1 -type d ! -perm -777 -exec chmod a+rwx {} \\; -o \

  ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \\; -o \

  ! -type d ! -perm -400 -exec chmod a+r {} \\; -o \

  ! -type d ! -perm -444 -exec /bin/sh /data/devel/c/str/install-sh -c -m a+r {} {} \\; \

|| chmod -R a+r str-0.0.1

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

{ 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 =  

#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, [SounOS@gmail.com])

#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_UINT_T

#用于自定义configure 选项,见acinclude.am

AC_CHECK_EXTRA_OPTIONS

# Checks for library functions.

AC_CONFIG_FILES([Makefile

                 src/Makefile])

AC_OUTPUT

下载本文
显示全文
专题