用Autotools工具编写Makefile文件

来源:互联网 发布:中国对东盟投资数据 编辑:程序博客网 时间:2024/05/16 09:50

之所以要用到Autotools, 是因为当工程比较大的时候, 手工编写Makefile文件是很麻烦的一件事情~此时就需要一款可以收集系统配置信息并自动生成Makefile文件的工具~

Autotools工具是由以下5个工具组成:

1)autoscan 扫描源代码目录

2)autocal 展开宏  

3)autoconf 生成configure文件

4)autoheader生成header文件

5)automake 生成Makefile相关


先来看一下GNU官网的Autotools工具使用流程图: 

[http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Making-configure-Scripts.html#Making-configure-Scripts]

Files used in preparing a software package for distribution, when using just Autoconf:

     your source files --> [autoscan*] --> [configure.scan] --> configure.ac          configure.ac --.                    |   .------> autoconf* -----> configure     [aclocal.m4] --+---+                    |   `-----> [autoheader*] --> [config.h.in]     [acsite.m4] ---'          Makefile.in

Additionally, if you use Automake, the following additional productions come into play:

     [acinclude.m4] --.                      |     [local macros] --+--> aclocal* --> aclocal.m4                      |     configure.ac ----'          configure.ac --.                    +--> automake* --> Makefile.in     Makefile.am ---'

Files used in configuring a software package:

                            .-------------> [config.cache]     configure* ------------+-------------> config.log                            |     [config.h.in] -.       v            .-> [config.h] -.                    +--> config.status* -+               +--> make*     Makefile.in ---'                    `-> Makefile ---'

下面这个可能更直观一些:




举个栗子: 

先用vi写一个hello.c并保存;

#include<stdio.h>void main(){     printf("hello,tudou!");}


autoscan

功能: 扫描源代码目录并生成configure.scan文件
首先执行autoscan命令,然后将生成的configure.scan文件改名为configure.ac:
lmtd@lmtd-virtual-machine:~/demo$ autoscanlmtd@lmtd-virtual-machine:~/demo$ lsautoscan.logconfigure.scanhello.clmtd@lmtd-virtual-machine:~/demo$ mv configure.scan configure.aclmtd@lmtd-virtual-machine:~/demo$ lsautoscan.logconfigure.achello.c

编辑一下configure.ac文件:
  1 #                                               -*- Autoconf -*-  2 # Process this file with autoconf to produce a configure script.  3  4 AC_PREREQ([2.69])  5 AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])  6 AC_CONFIG_SRCDIR([hello.c])  7 AC_CONFIG_HEADERS([config.h])  8  9 # Checks for programs. 10 AC_PROG_CC 11 12 # Checks for libraries. 13 14 # Checks for header files. 15 16 # Checks for typedefs, structures, and compiler characteristics. 17 18 # Checks for library functions. 19 20 AC_OUTPUT

将第5行,第20行的
5 AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
<pre name="code" class="cpp"> 20 AC_OUTPUT
改为:
5 AC_INIT(hello,1.0,lmtd@sina.com)      //分别对应三个参数([文件全称],[版本号],[错误报告地址])
 20 AC_OUTPUT(hello,1.0)
保存.

aclocal

功能:perl脚本程序,自动生成aclocal.m4文件.
执行aclocal命令:
lmtd@lmtd-virtual-machine:~/demo$ aclocallmtd@lmtd-virtual-machine:~/demo$ lsaclocal.m4  autom4te.cache  autoscan.log  configure.ac  hello.c
之后我们会发现多了一个aclocal.m4宏展开文件和一个autom4te.cache缓存文件.


autoconf

功能:生成configure文件.
执行autoconf命令:
lmtd@lmtd-virtual-machine:~/demo$ autoconflmtd@lmtd-virtual-machine:~/demo$ lsaclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  hello.c
虽然已经生成了configure文件,但是现在还不能进行配置,因为还缺少一些shell脚本文件,这些文件可以由automake生成.


automake

功能:产生Makefile.in文件.
具体用法:automake --add-missing
lmtd@lmtd-virtual-machine:~/demo$ automake --add-missingconfigure.ac:8: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:configure.ac:8: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocationconfigure.ac:11: installing './compile'configure.ac:8: installing './install-sh'configure.ac:8: installing './missing'automake: error: no 'Makefile.am' found for any configure outputlmtd@lmtd-virtual-machine:~/demo$ lsaclocal.m4      autoscan.log  config.log  configure.ac  install-shautom4te.cache  compile       configure   hello.c       missing

如果没加 --add-missing的话,就会有如下提示:
configure.ac:11: error: required file './compile' not foundconfigure.ac:11:   'automake --add-missing' can install 'compile'configure.ac:8: error: required file './install-sh' not foundconfigure.ac:8:   'automake --add-missing' can install 'install-sh'configure.ac:8: error: required file './missing' not foundconfigure.ac:8:   'automake --add-missing' can install 'missing'
就是说缺少install-sh等脚本文件.


那么如何建立配置Makefile.am文件呢?
GNU官网说它举个栗子:
[https://www.gnu.org/software/automake/manual/automake.html#Complete]

Now it is time to write your Makefile.am for zardoz. Since zardoz is a user program, you want to install it where the rest of the user programs go: bindir. Additionally, zardoz has some Texinfo documentation. Your configure.ac script uses AC_REPLACE_FUNCS, so you need to link against ‘$(LIBOBJS)’. So here’s what you’d write:

bin_PROGRAMS = zardoz                 //要命名的程序zardoz_SOURCES = main.c head.c float.c vortex9.c gun.c       //源代码程序zardoz_LDADD = $(LIBOBJS)             //引用类库的路径info_TEXINFOS = zardoz.texi          

Now you can run ‘automake --add-missing’ to generate your Makefile.in and grab any auxiliary files you might need, and you’re done!


我们建立一个Makefile.am文件,并写入:
  1 bin_PROGRAMS = hello  2 hello_SOURCES = hello.c

此时我们已经有这么多文件啦!
lmtd@lmtd-virtual-machine:~/demo$ lsaclocal.m4      compile        configure     install-shautom4te.cache  config.log     configure.ac  Makefile.amautoscan.log    config.status  hello.c       missing

可是执行automake命令的时候还是没有成功~:
lmtd@lmtd-virtual-machine:~/demo$ automakeconfigure.ac:8: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:configure.ac:8: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocationMakefile.am: installing './INSTALL'Makefile.am: error: required file './NEWS' not foundMakefile.am: error: required file './README' not foundMakefile.am: error: required file './AUTHORS' not foundMakefile.am: error: required file './ChangeLog' not foundMakefile.am: installing './COPYING' using GNU General Public License v3 fileMakefile.am:     Consider adding the COPYING file to the version control systemMakefile.am:     for your code, to avoid questions about which license your project usesconfigure.ac:7: error: required file 'config.h.in' not foundMakefile.am: installing './depcomp'

这是因为我们缺少一些NEWS, AUTHORS等文本文件,所以我们加上就好啦!(这里只是建立的空文件)
lmtd@lmtd-virtual-machine:~/demo$ touch NEWS README AUTHORS ChangeLog
lmtd@lmtd-virtual-machine:~/demo$ lsaclocal.m4      ChangeLog      configure     hello.c      missingAUTHORS         compile        configure.ac  INSTALL      NEWSautom4te.cache  config.log     COPYING       install-sh   READMEautoscan.log    config.status  depcomp       Makefile.amlmtd@lmtd-virtual-machine:~/demo$ automake --add-missingconfigure.ac:8: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:configure.ac:8: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocationconfigure.ac:7: error: required file 'config.h.in' not found

咦?这里说缺少"config.h.in"这是为什么呢?
因为我们还没执行autoheader工具呢~

autoheader

功能:自动生成config.h.in.
执行了autoheader命令之后,就会产生config.h.in文件啦~之后无论是执行automake --add-missing还是./configure还是make就都可以了!
lmtd@lmtd-virtual-machine:~/demo$ autoheaderlmtd@lmtd-virtual-machine:~/demo$ lsaclocal.m4      ChangeLog    config.status  depcomp     Makefile.amAUTHORS         compile      configure      hello.c     missingautom4te.cache  config.h.in  configure.ac   INSTALL     NEWSautoscan.log    config.log   COPYING        install-sh  README




0 0
原创粉丝点击