用Autotools工具编写Makefile文件

当项目规模增大,手工编写Makefile变得复杂。Autotools包括autoscan、aclocal、autoconf、autoheader和automake等工具,用于简化这一过程。通过这些工具,可以扫描源代码、生成configure文件、Makefile.in文件以及config.h.in头文件,自动化构建流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之所以要用到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$ autoscan
lmtd@lmtd-virtual-machine:~/demo$ ls
autoscan.log	configure.scan	hello.c
lmtd@lmtd-virtual-machine:~/demo$ mv configure.scan configure.ac
lmtd@lmtd-virtual-machine:~/demo$ ls
autoscan.log	configure.ac	hello.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$ aclocal
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  hello.c
之后我们会发现多了一个aclocal.m4宏展开文件和一个autom4te.cache缓存文件.


autoconf

功能:生成configure文件.
执行autoconf命令:
lmtd@lmtd-virtual-machine:~/demo$ autoconf
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.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-missing
configure.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-invocation
configure.ac:11: installing './compile'
configure.ac:8: installing './install-sh'
configure.ac:8: installing './missing'
automake: error: no 'Makefile.am' found for any configure output
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4      autoscan.log  config.log  configure.ac  install-sh
autom4te.cache  compile       configure   hello.c       missing

如果没加 --add-missing的话,就会有如下提示:
configure.ac:11: error: required file './compile' not found
configure.ac:11:   'automake --add-missing' can install 'compile'
configure.ac:8: error: required file './install-sh' not found
configure.ac:8:   'automake --add-missing' can install 'install-sh'
configure.ac:8: error: required file './missing' not found
configure.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$ ls
aclocal.m4      compile        configure     install-sh
autom4te.cache  config.log     configure.ac  Makefile.am
autoscan.log    config.status  hello.c       missing

可是执行automake命令的时候还是没有成功~:
lmtd@lmtd-virtual-machine:~/demo$ automake
configure.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-invocation
Makefile.am: installing './INSTALL'
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
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses
configure.ac:7: error: required file 'config.h.in' not found
Makefile.am: installing './depcomp'

这是因为我们缺少一些NEWS, AUTHORS等文本文件,所以我们加上就好啦!(这里只是建立的空文件)
lmtd@lmtd-virtual-machine:~/demo$ touch NEWS README AUTHORS ChangeLog
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4      ChangeLog      configure     hello.c      missing
AUTHORS         compile        configure.ac  INSTALL      NEWS
autom4te.cache  config.log     COPYING       install-sh   README
autoscan.log    config.status  depcomp       Makefile.am
lmtd@lmtd-virtual-machine:~/demo$ automake --add-missing
configure.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-invocation
configure.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$ autoheader
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4      ChangeLog    config.status  depcomp     Makefile.am
AUTHORS         compile      configure      hello.c     missing
autom4te.cache  config.h.in  configure.ac   INSTALL     NEWS
autoscan.log    config.log   COPYING        install-sh  README




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值