automake自动生成makefile文件

来源:互联网 发布:mysql数据库开发工具 编辑:程序博客网 时间:2024/06/05 17:36

 Linux下编程时,为了方便编译,往往使用Makefile文件自动完成编译,但是Makefile文件本身的书写十分复杂,规则很多。好在Linux为我们提供了自动生成功能完善的Makefile文件的工具autoconf/automake。本文讲述如何使用它们生成Makefile文件。


  整个工具组的工作流程如下图:




Linux Makefile第一步

在/root/project/main目录下创建一个文件main.c,其内容如下:

  1. #include <stdio.h>   
  2. int main(int argc, char** argv)   
  3. {   
  4. printf("Hello world!\n");   
  5. return 0;   
  6. }  

此时状态如下:

  1. [root@localhost main]# ls  
  2. main.c  

Linux Makefile第二步:

  1. 运行 autoscan , 自动创建两个文件:   
  2. autoscan.log  configure.scan此时状态如下:  
  3. [root@localhost main]# autoscan  
  4. [root@localhost main]# ls  
  5. autoscan.log  configure.scan  main.c  

第三步:修改configure.scan的文件名为configure.in查看configure.in的内容:

修改动作:

1.修改AC_INIT里面的参数: AC_INIT(main,1.0, test@263.com)

2.添加宏AM_INIT_AUTOMAKE, 它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号。
   3.在AC_OUTPUT后添加输出文件Linux Makefile

修改后的结果:

  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.  
  4. AC_PREREQ(2.61)  
  5. AC_INIT(main, 1.0, test@263.com)  
  6. AC_CONFIG_SRCDIR([main.c])  
  7. AC_CONFIG_HEADER([config.h])  
  8. AM_INIT_AUTOMAKE(main,1.0)  
  9.  
  10. # Checks for programs.  
  11. AC_PROG_CC  
  12. # Checks for libraries.  
  13. # Checks for header files.  
  14. # Checks for typedefs, structures, and compiler characteristics.  
  15. # Checks for library functions.  
  16. AC_OUTPUT([Makefile]) 


第四步:运行 aclocal,

生成一个“aclocal.m4”文件和一个缓冲文件夹autom4te.cache,该文件主要处理本地的宏定义。此时的状态是:

  1. [root@localhost main]# aclocal  
  2. [root@localhost main]# ls  
  3. aclocal.m4  autom4te.cache  autoscan.log  configure.in  
  4. configure.in~  main.c  

第五步:运行 autoconf, 目的是生成 configure 此时的状态是:

  1. [root@localhost main]# autoconf  
  2. [root@localhost main]# ls  
  3. aclocal.m4      autoscan.log  configure.in   main.c  
  4. autom4te.cache  configure     configure.in~  

第六步:运行 autoheader,它负责生成config.h.in文件。

该工具通常会从“acconfig.h”文件中复制用户附加的符号定义,因此此处没有附加符号定义,所以不需要创建“acconfig.h”文件。此时的状态是:

  1. [root@localhost main]# autoheader  
  2. [root@localhost main]# ls  
  3. aclocal.m4      autoscan.log  configure     configure.in~  
  4. autom4te.cache  config.h.in   configure.in  main.c  

第七步:下面即将运行 automake, 但在此之前应该做一下准备工作!

首先创建一个 Linux Makefile.am.这一步是创建Linux Makefile很重要的一步,automake要用的脚本配置文件是Linux Makefile.am,用户需要自己创建相应的文件。之后,automake工具转换成Linux Makefile.in。

这个Linux Makefile.am的内容如下:

  1. AUTOMAKE_OPTIONS=foreign 
  2. bin_PROGRAMS=main 
  3. main_SOURCES=main.c 

下面对该脚本文件的对应项进行解释。其中的AUTOMAKE_OPTIONS为设置automake的选项。由于GNU(在第1章中已经有所介绍)对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING等,否则automake执行时会报错。

automake提供了三种软件等级:foreign、gnu和gnits,让用户选择采用,默认等级为gnu。在本例使用foreign等级,它只检测必须的文件。定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。

main_SOURCES定义“main”这个执行程序所需要的原始文件。如果”main”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用空格隔开。例如:若目标体“main”需要“main.c”、“sunq.c”、“main.h”三个依赖文件,则定义main_SOURCES=main.c sunq.c main.h。要注意的是,如果要定义多个执行文件,则对每个执行程序都要定义相应的file_SOURCES。

其次使用automake对其生成“configure.in”文件,在这里使用选项“—adding-missing”可以让automake自动添加有一些必需的脚本文件。
运行后的状态是:

  1. [root@localhost main]# automake --add-missing  
  2. configure.in:8: installing `./missing'  
  3. configure.in:8: installing `./install-sh'  
  4. Makefile.am: installing `./depcomp'  
  5. [root@localhost main]# ls  
  6. aclocal.m4      config.h.in   configure.in~  main.c        Makefile.in  
  7. autom4te.cache  configure     depcomp        Makefile.am   missing  
  8. autoscan.log    configure.in  install-sh     Makefile.am~  

此步骤如果报以下错误:

 Makefile.am: error: required file './NEWS' not found
    Makefile.am: error: required file './README' not found
    Makefile.am: error: required file './AUTHORS' not found
    Makefile.am: error: required file './ChangeLog' not found

只要touch出以上四个文件,再重新执行即可。

Linux Makefile第八步运行configure,

在这一步中,通过运行自动配置设置文件configure,把Makefile.in变成了最终的Makefile。运行的结果如下:

  1. [root@localhost main]# ./configure   
  2. checking for a BSD-compatible install... /usr/bin/install -c  
  3. checking whether build environment is sane... yes  
  4. checking for a thread-safe mkdir -p... /bin/mkdir -p  
  5. checking for gawk... gawk  
  6. checking whether make sets $(MAKE)... yes  
  7. checking for gcc... gcc  
  8. checking for C compiler default output file name... a.out  
  9. checking whether the C compiler works... yes  
  10. checking whether we are cross compiling... no  
  11. checking for suffix of executables...   
  12. checking for suffix of object files... o  
  13. checking whether we are using the GNU C compiler... yes  
  14. checking whether gcc accepts -g... yes  
  15. checking for gcc option to accept ISO C89... none needed  
  16. checking for style of include used by make... GNU  
  17. checking dependency style of gcc... gcc3  
  18. configure: creating ./config.status  
  19. config.status: creating Makefile  
  20. config.status: creating config.h  
  21. config.status: executing depfiles commands  
  22. [root@localhost main]# ls  
  23. aclocal.m4      config.h.in    configure.in   main.c        Makefile.in  
  24. autom4te.cache  config.log     configure.in~  Makefile      missing  
  25. autoscan.log    config.status  depcomp        Makefile.am   stamp-h1  
  26. config.h        configure      install-sh     Makefile.am~   

Linux Makefile第九步运行 make,

对配置文件Makefile进行测试一下此时的状态如下:

  1. [root@localhost main]# make  
  2. cd . && /bin/sh /root/project/main/missing --run aclocal-1.10   
  3. cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign   
  4. cd . && /bin/sh /root/project/main/missing --run autoconf  
  5. /bin/sh ./config.status --recheck  
  6. running CONFIG_SHELL=/bin/sh /bin/sh ./configure   --no-create --no-recursion  
  7. checking for a BSD-compatible install... /usr/bin/install -c  
  8. checking whether build environment is sane... yes  
  9. checking for a thread-safe mkdir -p... /bin/mkdir -p  
  10. checking for gawk... gawk  
  11. checking whether make sets $(MAKE)... yes  
  12. checking for gcc... gcc  
  13. checking for C compiler default output file name... a.out  
  14. checking whether the C compiler works... yes  
  15. checking whether we are cross compiling... no  
  16. checking for suffix of executables...   
  17. checking for suffix of object files... o  
  18. checking whether we are using the GNU C compiler... yes  
  19. checking whether gcc accepts -g... yes  
  20. checking for gcc option to accept ISO C89... none needed  
  21. checking for style of include used by make... GNU  
  22. checking dependency style of gcc... gcc3  
  23. configure: creating ./config.status  
  24. /bin/sh ./config.status  
  25. config.status: creating Makefile  
  26. config.status: creating config.h  
  27. config.status: config.h is unchanged  
  28. config.status: executing depfiles commands  
  29. cd . && /bin/sh /root/project/main/missing --run autoheader  
  30. rm -f stamp-h1  
  31. touch config.h.in  
  32. make  all-am  
  33. make[1]: Entering directory `/root/project/main'  
  34. gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c  
  35. mv -f .deps/main.Tpo .deps/main.Po  
  36. gcc  -g -O2   -o main main.o    
  37. cd . && /bin/sh ./config.status config.h  
  38. config.status: creating config.h  
  39. config.status: config.h is unchanged  
  40. make[1]: Leaving directory `/root/project/main'  
  41. [root@localhost main]# ls  
  42. aclocal.m4      autoscan.log  config.h.in  config.status  configure.in   depcomp     main    main.o    Makefile.am   Makefile.in  stamp-h1  
  43. autom4te.cache  config.h      config.log   configure      configure.in~  install-sh  main.c  Makefile  Makefile.am~  missing  
  44.  

Linux Makefile第十步运行生成的文件 main:

  1. [root@localhost main]# ./main  
  2. Hello world!  
  3. [root@localhost main]#  

 备注:

 如果要执行release版本,则执行make CFLAGS=" -O3" 或者 make CXXFLAGS=" -O3",具体是CFLAGS还是CXXFLAGS,可在Makefile文件中查找确定。


本文参考:

http://www.cnblogs.com/njucslzh/archive/2010/04/29/1723320.html

http://os.51cto.com/art/201003/185539.htm

0 0