利用Linux下自动生成makefile的工具: automake, autoconf 生成makefile的一般过程

来源:互联网 发布:信息门户源码 编辑:程序博客网 时间:2024/06/05 14:10

利用Linux下自动生成makefile的工具: automake, autoconf 生成makefile的一般过程



1.  创建工程目录和各个目录下的makefile.am。工程的名字一般和最终生成应用程序的名字相同。


wzb@embedded ~]$ mkdir workspace[wzb@embedded ~]$ cd workspace/[wzb@embedded workspace]$ ls[wzb@embedded workspace]$ mkdir testAutoMake[wzb@embedded workspace]$ lstestAutoMake[wzb@embedded workspace]$ cd testAutoMake/[wzb@embedded testAutoMake]$ ls[wzb@embedded testAutoMake]$ mkdir src[wzb@embedded testAutoMake]$ lssrc[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lssrc[wzb@embedded testAutoMake]$ vi makefile.am[wzb@embedded testAutoMake]$ cd src[wzb@embedded src]$ ls[wzb@embedded src]$ vi main.c[wzb@embedded src]$ lsmain.c[wzb@embedded src]$ vi main.c[wzb@embedded src]$ clear[wzb@embedded src]$ lsmain.c[wzb@embedded src]$ vi makefile.am[wzb@embedded src]$ clear[wzb@embedded src]$ lsmain.c  makefile.am[wzb@embedded src]$ cd ..[wzb@embedded testAutoMake]$ lsmakefile.am  src[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ ls -R.:makefile.am  src./src:main.c  makefile.am[wzb@embedded testAutoMake]$
[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsmakefile.am  src[wzb@embedded testAutoMake]$ cat makefile.amSUBDIRS=src[wzb@embedded testAutoMake]$ vi makefile.am[wzb@embedded testAutoMake]$ cd src[wzb@embedded src]$ lsmain.c  makefile.am[wzb@embedded src]$ cat main.c#include <stdio.h>#include <stdarg.h>int accumulate(int nr, ...);int main(int argc, char *argv[]) {        int n = 5;        int result = accumulate(5, 5, 4, 3, 2, 1);        printf("%d,\n", result);        printf("test.......makefile ........ok\n");        return 0;}int accumulate(int nr, ...) {        int i = 0;        int result = 0;        va_list arg ;        va_start(arg, nr);        for(i=0; i<nr; i++) {                result += va_arg(arg, int);        }        va_end(arg);        return result;}[wzb@embedded src]$ pwd/home/wzb/workspace/testAutoMake/src[wzb@embedded src]$ cat makefile.ambin_PROGRAMS=testAutoMaketestAutoMake_SOURCES=main.c[wzb@embedded src]$


2.通过命令autoscan,创建autoconf的模板,生成configure.in 文件。

首先在工程目录中,通过执行autoscan命令,生成autoconf的模板文件configure.scan 文件,将其改名为configure.in。

需要两处修改:

 (1). 将configure.in文件中的语句:AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)中的

        FULL-PACKAGE-NAME: 替换为你指派的开发工程包的名字:如本工程的:test_AutoMake;

        VERSION: 开发的版本号,一般格式:主版本号.从版本号。 如 0.1;

         BUG-REPORT-ADDRESS:  提交bug的邮件地址: 如wzb12008@sina.com。

 (2). 还要添加一句:       AM_INIT_AUTOMAKE(testAutoMake, 0.1) 即AM_INIT_AUTOMAKE(package_name, version);

           用来初始化automake。


[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsmakefile.am  src[wzb@embedded testAutoMake]$ autoscanautom4te: configure.ac: no such file or directoryautoscan: /usr/bin/autom4te failed with exit status: 1[wzb@embedded testAutoMake]$ lsautoscan.log  configure.scan  makefile.am  src[wzb@embedded testAutoMake]$ vi configure.scan[wzb@embedded testAutoMake]$ vi configure.scan[wzb@embedded testAutoMake]$ mv configure.scan configure.in[wzb@embedded testAutoMake]$ lsautoscan.log  configure.in  makefile.am  src[wzb@embedded testAutoMake]$ cat configure.in#                                               -*- 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([src/main.c])AC_CONFIG_HEADER([config.h])# Checks for programs.AC_PROG_CC# Checks for libraries.# Checks for header files.AC_HEADER_STDC# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.AC_CONFIG_FILES([makefile                 src/makefile])AC_OUTPUT[wzb@embedded testAutoMake]$ vi configure.in[wzb@embedded testAutoMake]$ clear[wzb@embedded testAutoMake]$ lsautoscan.log  configure.in  makefile.am  src[wzb@embedded testAutoMake]$ cat configure.in#                                               -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_PREREQ(2.59)AC_INIT(testAutoMake, 0.1, wzb12008@sina.com)AC_CONFIG_SRCDIR([src/main.c])AC_CONFIG_HEADER([config.h])AM_INIT_AUTOMAKE(testAutoMake, 0.1)# Checks for programs.AC_PROG_CC# Checks for libraries.# Checks for header files.AC_HEADER_STDC# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.AC_CONFIG_FILES([makefile                 src/makefile])AC_OUTPUT[wzb@embedded testAutoMake]$

3. 使用aclocal, 将configure.in中宏展开,生成alocal.m4 和临时加速文件autom4te.cache。

[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsautoscan.log  configure.in  makefile.am  src[wzb@embedded testAutoMake]$ aclocal[wzb@embedded testAutoMake]$ lsaclocal.m4  autom4te.cache  autoscan.log  configure.in  makefile.am  src[wzb@embedded testAutoMake]$[wzb@embedded testAutoMake]$

4. 使用autoheader 生成config.h.in 文件。

[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsaclocal.m4  autom4te.cache  autoscan.log  configure.in  makefile.am  src[wzb@embedded testAutoMake]$ autoheader[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  configure.in  srcautom4te.cache  config.h.in   makefile.am[wzb@embedded testAutoMake]$


5. 创建文件 :README 、NEWS、AUTHORS、ChangeLog。touch  README NEWS AUTHORS ChangeLog

 

[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  configure.in  srcautom4te.cache  config.h.in   makefile.am[wzb@embedded testAutoMake]$ touch NEWS README AUTHORS ChangeLog[wzb@embedded testAutoMake]$ lsaclocal.m4  autom4te.cache  ChangeLog    configure.in  NEWS    srcAUTHORS     autoscan.log    config.h.in  makefile.am   README[wzb@embedded testAutoMake]$ vi ChangeLog[wzb@embedded testAutoMake]$ cat ChangeLog2012-04-07 Earl wzb12008@sina.com        * Created[wzb@embedded testAutoMake]$

6. 执行automake -a 命令生成makefile.in 文件以及其他相关文件 install-sh, missing, depcomp, INSTALL , COPYING等。

[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsaclocal.m4  autom4te.cache  ChangeLog    configure.in  NEWS    srcAUTHORS     autoscan.log    config.h.in  makefile.am   README[wzb@embedded testAutoMake]$ automake -aconfigure.in: installing `./install-sh'configure.in: installing `./missing'src/makefile.am: installing `./depcomp'makefile.am: installing `./INSTALL'makefile.am: installing `./COPYING'[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  configure.in  INSTALL      makefile.in  READMEAUTHORS         ChangeLog     COPYING       install-sh   missing      srcautom4te.cache  config.h.in   depcomp       makefile.am  NEWS[wzb@embedded testAutoMake]$


7. 执行 autoconf命令,生成configure脚本。


[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  configure.in  INSTALL      makefile.in  READMEAUTHORS         ChangeLog     COPYING       install-sh   missing      srcautom4te.cache  config.h.in   depcomp       makefile.am  NEWS[wzb@embedded testAutoMake]$ autoconf[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  configure     depcomp     makefile.am  NEWSAUTHORS         ChangeLog     configure.in  INSTALL     makefile.in  READMEautom4te.cache  config.h.in   COPYING       install-sh  missing      src[wzb@embedded testAutoMake]$


8.执行./configure 生成 makefile 文件。

configure脚本的两个参数,-prefix 指定bin目录的父目录;另一个-host 指定处理器架构,交叉编译环境搭建时有用。

[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  configure     depcomp     makefile.am  NEWSAUTHORS         ChangeLog     configure.in  INSTALL     makefile.in  READMEautom4te.cache  config.h.in   COPYING       install-sh  missing      src[wzb@embedded testAutoMake]$ ./configurechecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables...checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ANSI C... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3checking how to run the C preprocessor... gcc -Echecking for egrep... grep -Echecking for ANSI C header files... yesconfigure: creating ./config.statusconfig.status: creating makefileconfig.status: creating src/makefileconfig.status: creating config.hconfig.status: executing depfiles commands[wzb@embedded testAutoMake]$ lsaclocal.m4      ChangeLog    config.status  depcomp     makefile.am  READMEAUTHORS         config.h     configure      INSTALL     makefile.in  srcautom4te.cache  config.h.in  configure.in   install-sh  missing      stamp-h1autoscan.log    config.log   COPYING        makefile    NEWS[wzb@embedded testAutoMake]$

9.  一下步骤基本与我们从源码编译安装应用的步骤相似。

       首先,./configure -prefix bin目录所在的父目录(绝对路径)

        接着  make  ; 编译

        最后  make install;   在bin目录中找到生成可执行程序测试。

[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsaclocal.m4  autom4te.cache  ChangeLog  config.h.in  config.status  configure.in  depcomp  install-sh  makefile.am  missing  README  stamp-h1AUTHORS     autoscan.log    config.h   config.log   configure      COPYING       INSTALL  makefile    makefile.in  NEWS     src[wzb@embedded testAutoMake]$ ./configure -prefix /home/wzb/wworkspace/ wzb/[wzb@embedded testAutoMake]$ ./configure -prefix /home/wzb/workspace/testAutoMake/checking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables...checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ANSI C... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3checking how to run the C preprocessor... gcc -Echecking for egrep... grep -Echecking for ANSI C header files... yesconfigure: creating ./config.statusconfig.status: creating makefileconfig.status: creating src/makefileconfig.status: creating config.hconfig.status: config.h is unchangedconfig.status: executing depfiles commands[wzb@embedded testAutoMake]$ makemake  all-recursivemake[1]: Entering directory `/home/wzb/workspace/testAutoMake'Making all in srcmake[2]: Entering directory `/home/wzb/workspace/testAutoMake/src'if gcc -DHAVE_CONFIG_H -I. -I. -I..     -g -O2 -MT main.o -MD -MP -MF ".deps/main.Tpo" -c -o main.o main.c; \        then mv -f ".deps/main.Tpo" ".deps/main.Po"; else rm -f ".deps/main.Tpo"; exit 1; figcc  -g -O2   -o testAutoMake  main.omake[2]: Leaving directory `/home/wzb/workspace/testAutoMake/src'make[2]: Entering directory `/home/wzb/workspace/testAutoMake'make[2]: Leaving directory `/home/wzb/workspace/testAutoMake'make[1]: Leaving directory `/home/wzb/workspace/testAutoMake'[wzb@embedded testAutoMake]$ lsaclocal.m4  autom4te.cache  ChangeLog  config.h.in  config.status  configure.in  depcomp  install-sh  makefile.am  missing  README  stamp-h1AUTHORS     autoscan.log    config.h   config.log   configure      COPYING       INSTALL  makefile    makefile.in  NEWS     src[wzb@embedded testAutoMake]$ make installMaking install in srcmake[1]: Entering directory `/home/wzb/workspace/testAutoMake/src'make[2]: Entering directory `/home/wzb/workspace/testAutoMake/src'test -z "/home/wzb/workspace/testAutoMake//bin" || mkdir -p -- "/home/wzb/workspace/testAutoMake//bin"  /usr/bin/install -c 'testAutoMake' '/home/wzb/workspace/testAutoMake//bin/testAutoMake'make[2]: Nothing to be done for `install-data-am'.make[2]: Leaving directory `/home/wzb/workspace/testAutoMake/src'make[1]: Leaving directory `/home/wzb/workspace/testAutoMake/src'make[1]: Entering directory `/home/wzb/workspace/testAutoMake'make[2]: Entering directory `/home/wzb/workspace/testAutoMake'make[2]: Nothing to be done for `install-exec-am'.make[2]: Nothing to be done for `install-data-am'.make[2]: Leaving directory `/home/wzb/workspace/testAutoMake'make[1]: Leaving directory `/home/wzb/workspace/testAutoMake'[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  config.h     config.status  COPYING  install-sh   makefile.in  READMEAUTHORS         bin           config.h.in  configure      depcomp  makefile     missing      srcautom4te.cache  ChangeLog     config.log   configure.in   INSTALL  makefile.am  NEWS         stamp-h1[wzb@embedded testAutoMake]$ ./bin/testAutoMake15,test.......makefile ........ok[wzb@embedded testAutoMake]$



10. 当程序稳定了,可以通过命令 make dist , 生成工程的发布文件。(还有 make distcheck命令)
[wzb@embedded testAutoMake]$ pwd/home/wzb/workspace/testAutoMake[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  config.h     config.status  COPYING  install-sh   makefile.in  READMEAUTHORS         bin           config.h.in  configure      depcomp  makefile     missing      srcautom4te.cache  ChangeLog     config.log   configure.in   INSTALL  makefile.am  NEWS         stamp-h1[wzb@embedded testAutoMake]$ make dist{ test ! -d testAutoMake-0.1 || { find testAutoMake-0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr testAutoMake-0.1; }; }mkdir testAutoMake-0.1list='src'; for subdir in $list; do \          if test "$subdir" = .; then :; else \            test -d "testAutoMake-0.1/$subdir" \            || mkdir -p -- "testAutoMake-0.1/$subdir" \            || exit 1; \            distdir=`CDPATH="${ZSH_VERSION+.}:" && cd testAutoMake-0.1 && pwd`; \            top_distdir=`CDPATH="${ZSH_VERSION+.}:" && cd testAutoMake-0.1 && pwd`; \            (cd $subdir && \              make  \                top_distdir="$top_distdir" \                distdir="$distdir/$subdir" \                distdir) \              || exit 1; \          fi; \        donemake[1]: Entering directory `/home/wzb/workspace/testAutoMake/src'make[1]: Leaving directory `/home/wzb/workspace/testAutoMake/src'find testAutoMake-0.1 -type d ! -perm -755 -exec chmod a+rwx,go+rx {} \; -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 /home/wzb/workspace/testAutoMake/install-sh -c -m a+r {} {} \; \        || chmod -R a+r testAutoMake-0.1tardir=testAutoMake-0.1 && /bin/sh /home/wzb/workspace/testAutoMake/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >testAutoMake-0.1.tar.gz{ test ! -d testAutoMake-0.1 || { find testAutoMake-0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr testAutoMake-0.1; }; }[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  config.h     config.status  COPYING  install-sh   makefile.in  README    testAutoMake-0.1.tar.gzAUTHORS         bin           config.h.in  configure      depcomp  makefile     missing      srcautom4te.cache  ChangeLog     config.log   configure.in   INSTALL  makefile.am  NEWS         stamp-h1[wzb@embedded testAutoMake]$ clear[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  config.h     config.status  COPYING  install-sh   makefile.in  README    testAutoMake-0.1.tar.gzAUTHORS         bin           config.h.in  configure      depcomp  makefile     missing      srcautom4te.cache  ChangeLog     config.log   configure.in   INSTALL  makefile.am  NEWS         stamp-h1[wzb@embedded testAutoMake]$ clear[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  config.h     config.status  COPYING  install-sh   makefile.in  README    testAutoMake-0.1.tar.gzAUTHORS         bin           config.h.in  configure      depcomp  makefile     missing      srcautom4te.cache  ChangeLog     config.log   configure.in   INSTALL  makefile.am  NEWS         stamp-h1[wzb@embedded testAutoMake]$ vi ChangeLog[wzb@embedded testAutoMake]$ clear[wzb@embedded testAutoMake]$ lsaclocal.m4      autoscan.log  config.h     config.status  COPYING  install-sh   makefile.in  README    testAutoMake-0.1.tar.gzAUTHORS         bin           config.h.in  configure      depcomp  makefile     missing      srcautom4te.cache  ChangeLog     config.log   configure.in   INSTALL  makefile.am  NEWS         stamp-h1[wzb@embedded testAutoMake]$ vi configure.in


以上是是使用automake生成makefile的全过程,

 想到了为什么,安装源码文件时,要先 ./configure -prefix ... , 接着make, 接着make install 。很清楚了。。。









原创粉丝点击