| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > 软件开发

linux c 一个autotools的最简单例子

时间:2007-11-18 17:29:01  来源:Linux联盟收集整理  作者:
最近开始学习linux c开发,对autotools比较感兴趣,所以找了一些国外的文档看了看,然后自己做了小例子,在这里跟大家分享。NAiLinux联盟
  1、准备:NAiLinux联盟
     需要工具autoscan aclocal autoheader automake autoconf make 等工具.NAiLinux联盟
  2、测试程序编写:NAiLinux联盟
     建立目录:mkdir include srcNAiLinux联盟
     编写程序:include/str.hNAiLinux联盟
NAiLinux联盟
#include <stdio.h>NAiLinux联盟
int str(char *string);
NAiLinux联盟
编写程序:src/str.cNAiLinux联盟
NAiLinux联盟
#include "str.h"NAiLinux联盟
//print stringNAiLinux联盟
int str(char *string){NAiLinux联盟
        printf("\n----PRINT STRING----\n\"%s\"\n",string);NAiLinux联盟
        return 0;NAiLinux联盟
}NAiLinux联盟
NAiLinux联盟
//interface of this programNAiLinux联盟
int main(int argc , char **argv){NAiLinux联盟
        char str_read[1024];NAiLinux联盟
        printf("Please INPUT something end by [ENTER]\n");NAiLinux联盟
        scanf("%s",str_read);NAiLinux联盟
        return str(str_read );NAiLinux联盟
}NAiLinux联盟
NAiLinux联盟
     
NAiLinux联盟
3、生成configure.acNAiLinux联盟
    configure.ac是automake的输入文件,所以必须先生成该文件。NAiLinux联盟
    执行命令:NAiLinux联盟
NAiLinux联盟
[root@localhost str]# lsNAiLinux联盟
include  srcNAiLinux联盟
[root@localhost str]# autoscanNAiLinux联盟
autom4te: configure.ac: no such file or directoryNAiLinux联盟
autoscan: /usr/bin/autom4te failed with exit status: 1NAiLinux联盟
[root@localhost str]# lsNAiLinux联盟
autoscan.log  configure.scan  include  srcNAiLinux联盟
[root@localhost str]# cp configure.scan configure.ac
NAiLinux联盟
修改 configure.acNAiLinux联盟
NAiLinux联盟
#                                               -*- Autoconf -*-NAiLinux联盟
# Process this file with autoconf to produce a configure script.NAiLinux联盟
NAiLinux联盟
AC_PREREQ(2.59)NAiLinux联盟
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)NAiLinux联盟
AC_CONFIG_SRCDIR([include/str.h])NAiLinux联盟
AC_CONFIG_HEADER([config.h])NAiLinux联盟
NAiLinux联盟
# Checks for programs.NAiLinux联盟
AC_PROG_CCNAiLinux联盟
NAiLinux联盟
# Checks for libraries.NAiLinux联盟
NAiLinux联盟
# Checks for header files.NAiLinux联盟
NAiLinux联盟
# Checks for typedefs, structures, and compiler characteristics.NAiLinux联盟
NAiLinux联盟
# Checks for library functions.NAiLinux联盟
AC_OUTPUT
NAiLinux联盟
修改NAiLinux联盟
NAiLinux联盟
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
NAiLinux联盟
NAiLinux联盟
NAiLinux联盟
CODE:
AC_INIT(str,0.0.1, [bug@sounos.org])
NAiLinux联盟
FULL-PACKAGE-NAME 为程序名称,VERSION为当前版本, BUG-REPORT-ADDRESS为bug汇报地址NAiLinux联盟
    添加AM_INIT_AUTOMAKE NAiLinux联盟
    添加AC_CONFIG_FILES([Makefile])NAiLinux联盟
NAiLinux联盟
CODE:
#                                               -*- Autoconf -*-NAiLinux联盟
# Process this file with autoconf to produce a configure script.NAiLinux联盟
NAiLinux联盟
AC_PREREQ(2.59)NAiLinux联盟
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)NAiLinux联盟
AC_INIT(str, 0.0.1, [bug@sounos.org])NAiLinux联盟
AM_INIT_AUTOMAKENAiLinux联盟
AC_CONFIG_SRCDIR([include/str.h])NAiLinux联盟
AC_CONFIG_HEADER([config.h])NAiLinux联盟
NAiLinux联盟
# Checks for programs.NAiLinux联盟
AC_PROG_CCNAiLinux联盟
NAiLinux联盟
# Checks for libraries.NAiLinux联盟
NAiLinux联盟
# Checks for header files.NAiLinux联盟
NAiLinux联盟
# Checks for typedefs, structures, and compiler characteristics.NAiLinux联盟
NAiLinux联盟
# Checks for library functions.NAiLinux联盟
AC_CONFIG_FILES([Makefile])NAiLinux联盟
AC_OUTPUT
NAiLinux联盟
4、执行aclocalNAiLinux联盟
NAiLinux联盟
CODE:
[root@localhost str]# aclocalNAiLinux联盟
/usr/share/aclocal/libfame.m4:6: warning: underquoted definition of AM_PATH_LIBFAMENAiLinux联盟
  run info '(automake)Extending aclocal'NAiLinux联盟
  or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
NAiLinux联盟
5、制作Makefile.amNAiLinux联盟
NAiLinux联盟
CODE:
[root@localhost str]# cat Makefile.amNAiLinux联盟
#Makefile.amNAiLinux联盟
bin_PROGRAMS    = strNAiLinux联盟
str_SOURCES     = include/str.h src/str.cNAiLinux联盟
str_CPPFLAGS    = -I include/
NAiLinux联盟
6、autoheaderNAiLinux联盟
NAiLinux联盟
[root@localhost str]# autoheader
NAiLinux联盟
7、automake必须文件:NAiLinux联盟
NAiLinux联盟
    *  install-shNAiLinux联盟
    * missingNAiLinux联盟
    * INSTALLNAiLinux联盟
    * NEWSNAiLinux联盟
    * READMENAiLinux联盟
    * AUTHORSNAiLinux联盟
    * ChangeLogNAiLinux联盟
    * COPYINGNAiLinux联盟
    * depcomp
NAiLinux联盟
其中NAiLinux联盟
NAiLinux联盟
    * install-shNAiLinux联盟
    * missingNAiLinux联盟
    * INSTALLNAiLinux联盟
    * COPYINGNAiLinux联盟
    * depcomp
NAiLinux联盟
可以通过automake -a选项自动生成,所以这里只需要建立如下文件NAiLinux联盟
NAiLinux联盟
[root@localhost str]# touch NEWS README AUTHORS ChangeLog
NAiLinux联盟
8、执行automakeNAiLinux联盟
NAiLinux联盟
[root@localhost str]# automake -aNAiLinux联盟
configure.ac: installing `./install-sh'NAiLinux联盟
configure.ac: installing `./missing'NAiLinux联盟
Makefile.am: installing `./INSTALL'NAiLinux联盟
Makefile.am: installing `./COPYING'NAiLinux联盟
Makefile.am: installing `./compile'NAiLinux联盟
Makefile.am: installing `./depcomp'
NAiLinux联盟
9、autoconfNAiLinux联盟
NAiLinux联盟
[root@localhost str]# autoconfNAiLinux联盟
[root@localhost str]# lsNAiLinux联盟
aclocal.m4      autoscan.log  config.h.in   configure.scan  include     Makefile.am  NEWSNAiLinux联盟
AUTHORS         ChangeLog     configure     COPYING         INSTALL     Makefile.in  READMENAiLinux联盟
autom4te.cache  compile       configure.ac  depcomp         install-sh  missing      src
NAiLinux联盟
10、执行测试:NAiLinux联盟
      执行./configureNAiLinux联盟
NAiLinux联盟
[root@localhost str]# ./configure --prefix=/uNAiLinux联盟
checking for a BSD-compatible install... /usr/bin/install -cNAiLinux联盟
checking whether build environment is sane... yesNAiLinux联盟
checking for gawk... gawkNAiLinux联盟
checking whether make sets $(MAKE)... yesNAiLinux联盟
checking for gcc... gccNAiLinux联盟
checking for C compiler default output file name... a.outNAiLinux联盟
checking whether the C compiler works... yesNAiLinux联盟
checking whether we are cross compiling... noNAiLinux联盟
checking for suffix of executables...NAiLinux联盟
checking for suffix of object files... oNAiLinux联盟
checking whether we are using the GNU C compiler... yesNAiLinux联盟
checking whether gcc accepts -g... yesNAiLinux联盟
checking for gcc option to accept ANSI C... none neededNAiLinux联盟
checking for style of include used by make... GNUNAiLinux联盟
checking dependency style of gcc... gcc3NAiLinux联盟
configure: creating ./config.statusNAiLinux联盟
config.status: creating MakefileNAiLinux联盟
config.status: creating config.hNAiLinux联盟
config.status: config.h is unchangedNAiLinux联盟
config.status: executing depfiles commands
NAiLinux联盟
执行 makeNAiLinux联盟
NAiLinux联盟
[root@localhost str]# makeNAiLinux联盟
make  all-amNAiLinux联盟
make[1]: Entering directory `/data/devel/c/str'NAiLinux联盟
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; \NAiLinux联盟
then mv -f ".deps/str-str.Tpo" ".deps/str-str.Po"; else rm -f ".deps/str-str.Tpo"; exit 1; fiNAiLinux联盟
gcc  -g -O2   -o str  str-str.oNAiLinux联盟
make[1]: Leaving directory `/data/devel/c/str'
NAiLinux联盟
执行 make installNAiLinux联盟
NAiLinux联盟
[root@localhost str]# make installNAiLinux联盟
make[1]: Entering directory `/data/devel/c/str'NAiLinux联盟
test -z "/u/bin" || mkdir -p -- "/u/bin"NAiLinux联盟
  /usr/bin/install -c 'str' '/u/bin/str'NAiLinux联盟
make[1]: Nothing to be done for `install-data-am'.NAiLinux联盟
make[1]: Leaving directory `/data/devel/c/str'     
NAiLinux联盟
11、测试程序:NAiLinux联盟
NAiLinux联盟
[root@localhost str]# /u/bin/strNAiLinux联盟
Please INPUT something end by [ENTER]NAiLinux联盟
abcksdhfklsdklfdjlkfdNAiLinux联盟
NAiLinux联盟
----PRINT STRING----NAiLinux联盟
"abcksdhfklsdklfdjlkfd"
NAiLinux联盟
结束语:这只是一个小例子,如果要把这个用得很好需要不断的磨练。。。。呵呵,见笑了。NAiLinux联盟
NAiLinux联盟
NAiLinux联盟
NAiLinux联盟
-----NAiLinux联盟
NAiLinux联盟
添加测试包:NAiLinux联盟
NAiLinux联盟
[root@localhost str]# make dist-gzipNAiLinux联盟
{ 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; }; }NAiLinux联盟
mkdir str-0.0.1NAiLinux联盟
find str-0.0.1 -type d ! -perm -777 -exec chmod a+rwx {} \; -o \NAiLinux联盟
  ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \NAiLinux联盟
  ! -type d ! -perm -400 -exec chmod a+r {} \; -o \NAiLinux联盟
  ! -type d ! -perm -444 -exec /bin/sh /data/devel/c/str/install-sh -c -m a+r {} {} \; \NAiLinux联盟
|| chmod -R a+r str-0.0.1NAiLinux联盟
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.gzNAiLinux联盟
{ 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; }; }
NAiLinux联盟
添加一个支持子目录、静态库、自定义configure选项的包NAiLinux联盟
NAiLinux联盟
支持子目录Makefile.am 选项 SUBDIR =NAiLinux联盟
NAiLinux联盟
#Automake interface NAiLinux联盟
SUBDIRS = src
NAiLinux联盟
支持静态库Makefile.amNAiLinux联盟
EXTRA_DIST  用于添加除源码外的文件到dist包NAiLinux联盟
NAiLinux联盟
#Automake interfaceNAiLinux联盟
bin_PROGRAMS = helloNAiLinux联盟
hello_SOURCES = hello.c lib/sbase.hNAiLinux联盟
hello_CPPFLAGS = -I libNAiLinux联盟
hello_LDFLAGS = -static lib/libsbase.aNAiLinux联盟
EXTRA_DIST = lib/libsbase.a
NAiLinux联盟
configure.acNAiLinux联盟
NAiLinux联盟
NAiLinux联盟
CODE:
AC_PREREQ(2.59)NAiLinux联盟
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)NAiLinux联盟
AC_INIT(hello, 0.0.1, [SounOS@gmail.com])NAiLinux联盟
#AM 声明NAiLinux联盟
AM_INIT_AUTOMAKENAiLinux联盟
AC_CONFIG_SRCDIR([src/hello.c])NAiLinux联盟
AC_CONFIG_HEADER([config.h])NAiLinux联盟
NAiLinux联盟
# Checks for programs.NAiLinux联盟
AC_PROG_CCNAiLinux联盟
NAiLinux联盟
# Checks for libraries.NAiLinux联盟
NAiLinux联盟
# Checks for header files.NAiLinux联盟
AC_HEADER_STDCNAiLinux联盟
AC_CHECK_HEADERS([stdint.h stdlib.h sys/socket.h])NAiLinux联盟
NAiLinux联盟
# Checks for typedefs, structures, and compiler characteristics.NAiLinux联盟
AC_C_CONSTNAiLinux联盟
AC_TYPE_SIZE_TNAiLinux联盟
AC_TYPE_UINT32_TNAiLinux联盟
AC_TYPE_UINT64_TNAiLinux联盟
NAiLinux联盟
#用于自定义configure 选项,见acinclude.amNAiLinux联盟
AC_CHECK_EXTRA_OPTIONSNAiLinux联盟
# Checks for library functions.NAiLinux联盟
NAiLinux联盟
AC_CONFIG_FILES([MakefileNAiLinux联盟
                 src/Makefile])NAiLinux联盟
AC_OUTPUT
NAiLinux联盟
NAiLinux联盟

文件:str-0.0.1.tar.gz
大小:67KB
下载:下载
NAiLinux联盟
文件:hello-0.0.1.tar.gz
大小:146KB
下载:下载
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门