autotools简单例子说明

1、用autotools产生makefile的整体流程:


2、利用autotools系列工具生成“Makefile”文件的基本步骤 :


3、例子说明

main.c

#include <stdio.h>

float avg(int avr[], int num);

int main(void)
{
    int n, i;
    float average;
    printf("please input how many the numbers:");
    scanf("%d",&n);
    int array[n];

    for( i=0;i<n;i++ )
    {
        printf("number[%d]: ",i+1);
        scanf("%d",&array[i]);
//        printf("\n");
    }
    average = avg(array,n);
    printf("the average value of %d numbers : %6.2f\n",n,average);
}

avg.c

float avg(int var[], int num)
{
    float avrg = 0;
    int i;
    for(i=0;i<num;i++)
    {
        avrg += var[i];
    }
    avrg /= num;
    return avrg;
}

第一步:使用autoscan,生成autoscan.log和configure.scan

hubimaso@hubimaso-virtual-machine:~/base$ ls
avg.c  main.c
hubimaso@hubimaso-virtual-machine:~/base$ autoscan
hubimaso@hubimaso-virtual-machine:~/base$ ls
autoscan.log  avg.c  configure.scan  main.c
第二步 :修改configure.scan,重命名为configure.in

原configure.scan

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([avg.c])
AC_CONFIG_HEADERS([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
修改后的configure.scan

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])

<span style="color:#ff0000;">AC_INIT(main,1.0)
AM_INIT_AUTOMAKE(main,1.0)</span>
<span style="color:#ff0000;">AC_CONFIG_FILES([Makefile])</span>
AC_CONFIG_SRCDIR([avg.c])
<span style="color:#333333;">AC</span>_CONFIG_HEADERS([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
hubimaso@hubimaso-virtual-machine:~/base$ mv configure.scan configure.in
hubimaso@hubimaso-virtual-machine:~/base$ ls
autoscan.log  avg.c  configure.in  main.c
第三步:使用aclocal、autoconf、autoheader(这里我先编辑了makefile.am,可以在第4步编辑)

hubimaso@hubimaso-virtual-machine:~/base$ aclocal
hubimaso@hubimaso-virtual-machine:~/base$ ls
aclocal.m4  autom4te.cache  autoscan.log  avg.c  configure.in  main.c  Makefile.am
hubimaso@hubimaso-virtual-machine:~/base$ autoconf 
hubimaso@hubimaso-virtual-machine:~/base$ ls
aclocal.m4  autom4te.cache  autoscan.log  avg.c  configure  configure.in  main.c  Makefile.am
hubimaso@hubimaso-virtual-machine:~/base$ autoheader 
hubimaso@hubimaso-virtual-machine:~/base$ ls
aclocal.m4  autom4te.cache  autoscan.log  avg.c  config.h.in  configure  configure.in  main.c  Makefile.am

第四步:使用automake, 编辑makefile.am

hubimaso@hubimaso-virtual-machine:~/base$ cat makefile.am 
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=main.c avg.c

hubimaso@hubimaso-virtual-machine:~/base$ automake --add-missing
configure.in:7: installing `./install-sh'
configure.in:7: installing `./missing'
Makefile.am: installing `./depcomp'
hubimaso@hubimaso-virtual-machine:~/base$ automake
hubimaso@hubimaso-virtual-machine:~/base$ ls
aclocal.m4      autoscan.log  config.h.in  configure.in  install-sh  Makefile.am  missing
autom4te.cache  avg.c         configure    depcomp       main.c      Makefile.in

第五步:运行configure

hubimaso@hubimaso-virtual-machine:~/base$ ./configure 
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
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 ISO C89... 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: executing depfiles commands
hubimaso@hubimaso-virtual-machine:~/base$ ls
aclocal.m4      autoscan.log  config.h     config.log     configure     depcomp     main.c    Makefile.am  missing
autom4te.cache  avg.c         config.h.in  config.status  configure.in  install-sh  Makefile  Makefile.in  stamp-h1

第六步:sudo make && make install

最后执行./main

hubimaso@hubimaso-virtual-machine:~/base$ ./main 
please input how many the numbers:5
number[1]: 1
number[2]: 2
number[3]: 3
number[4]: 4
number[5]: 5
the average value of 5 numbers :   3.00

参考:

http://blog.sina.com.cn/s/blog_62eb16bb0101cmcj.html

http://www.linuxdiyf.com/viewarticle.php?id=197457

http://tech.sina.com.cn/s/2004-10-19/1115443047.shtml

http://www.ibm.com/developerworks/cn/linux/l-makefile/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值