automake创建Makefile

使用automake主要会用到aclocal、autoscan、autoconf、autoheader和automake这几个命令。

首先简略的说一下用automake生成makefile的步骤:

(1)创建源代码文件,使用”autoscan”生成configure.scan文件,将其重命名为configure.ac,并做适当修改,然后使用”aclocal”命令生成aclocal.m4文件,使用”autoconf”命令由configure.ac和aclocal.m4文件生成configure文件。
(2)手工编辑Makefile.am文件,使用”automake”命令生成configure.in文件。
(3)手工编辑或由系统给定acconfig.h文件,使用”autoheader”命令生成config.h.in文件。
(4)使用”configure”命令由configure、configure.in和config.h.in文件生成Makefile文件。从而完成Makefile文件的创建过程。

下面用一个经典的hello world的例子说明automake的用法
1.编辑源文件

root@ubuntu:~# mkdir hello
root@ubuntu:~# cd hello
root@ubuntu:~/hello# vim hello.c
root@ubuntu:~/hello# cat hello.c
#include "stdio.h"
int main()
{
printf("hello world\n");
return 0;
}

2.使用Autoscan工具生成configure.ac文件
Autoscan工具用来扫描源代码以搜寻一般的可移植性问题,比如检查编译器、库和头文件等,并创建configure.scan文件,它会在给定目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目录树中进行检查。

root@ubuntu:~/hello# autoscan          //扫描源文件,生成configure.scan
root@ubuntu:~/hello# ls
autoscan.log  configure.scan  hello.c
root@ubuntu:~/hello# cat configure.scan
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([hello.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

下面给出本文件的简要说明(所有以”#”号开始的行为注释):

(1)AC_PREREQ宏声明本文件要求的autoconf版本,本例使用的版本为2.59。

(2)AC_INIT宏用来定义软件的名称和版本等信息,”FULL-PACKAGE-NAME”为软件包名称,”VERSION”为软件版本号,”BUG-REPORT-ADDRESS”为BUG报告地址(一般为软件作者邮件地址)。

(3)AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。此处为当前目录下的hello.c。

(4)AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

(5)AC_PROG_CC用来指定编译器,如果不指定,选用默认gcc。

(6)AC_OUTPUT用来设定 configure 所要产生的文件,如果是makefile,configure会把它检查出来的结果带入makefile.in文件产生合适的makefile。使用Automake时,还需要一些其他的参数,这些额外的宏用aclocal工具产生。

此文件只是下面要使用的configure.ac文件的原型,当然我们还要对其做一定的修改:

root@ubuntu:~/hello# mv configure.scan  configure.ac   #将名字改成configure.in也行
root@ubuntu:~/hello# vim configure.ac
root@ubuntu:~/hello# cat configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])               //automake版本
AC_INIT(hello, 1.0, xx@163.com)             //设置软件包信息
AM_INIT_AUTOMAKE(hello, 1.0)               //这个需要手动添加,它是automake必备的宏
AC_CONFIG_SRCDIR([hello.c])                //源文件名
#AC_CONFIG_HEADERS([config.h])           //config.h 文件, 这个地方我们不需要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([Makefile])                  //要输出的文件名

3.使用aclocal工具生成aclocal.m4
aclocal根据configure.ac生成aclocal.m4

root@ubuntu:~/hello# aclocal
root@ubuntu:~/hello# ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  hello.c

4.使用autoconf工具生成configure文件
将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。

root@ubuntu:~/hello# autoconf
root@ubuntu:~/hello# ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  hello.c

5.生成config.h.in文件

运行 autoheader,它负责生成config.h.in文件。该工具通常会从“acconfig.h”文件中复制用户附加的符号定义,因此此处没有附加符号定义,且我们在上面2中已经注释掉了AC_CONFIG_HEADERS([config.h]) ,所以不需要创建“config.h.in”文件。


假如我们没有注释掉,运行autoheader后的状态如下:
[root@localhost main]# 
autoheader
[root@localhost main]# 
ls
aclocal.m4      autoscan.log configure     configure.in~
autom4te.cache 
config.h.in   configure.in hello.c

6.创建Makefile.am文件
Automake工具会根据configure.in中的参量把Makefile.am转换成Makefile.in文件。所以在使用automake之前我们需要自己创建Makefile.am文件

root@ubuntu:~/hello# vim Makefile.am
root@ubuntu:~/hello# cat Makefile.am
AUTOMAKE_OPTIONS=foreign           //软件等级
bin_PROGRAMS=hello                  //可执行文件名
hello_SOURCES=hello.c              //源文件名

其中:

(1)AUTOMAKE_OPTIONS为设置Automake的选项。由于GNU对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING等,否则Automake执行时会报错。Automake提供了3种软件等级:foreign、gnu和gnits,供用户选择,默认等级为gnu。本例使需用foreign等级,它只检测必须的文件。

(2)bin_PROGRAMS定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。

(3)hello_SOURCES定义”hello”这个执行程序所需要的原始文件。如果”hello”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用空格隔开。例如:若目标体”hello”需要”hello.c”、”hello.h”两个依赖文件,则定义hello_SOURCES=hello.c hello.h。

7.使用Automake生成Makefile.in文件

下面使用Automake生成”Makefile.in”文件,使用选项”–add-missing”可以让Automake自动添加一些必需的脚本文件。如下所示:

root@ubuntu:~/hello# automake --add-missing
configure.ac:6: installing `./install-sh'
configure.ac:6: installing `./missing'
Makefile.am: installing `./depcomp'
root@ubuntu:~/hello# ls
aclocal.m4      autoscan.log  configure.ac  hello.c     Makefile.am  missing
autom4te.cache  configure     depcomp       install-sh  Makefile.in

8.执行./configure 生成Makefile

9.测试

root@ubuntu:~/hello# ls
aclocal.m4      config.log     configure.ac  hello.c     Makefile     missing
autom4te.cache  config.status  depcomp       hello.o     Makefile.am
autoscan.log    configure      hello         install-sh  Makefile.in
root@ubuntu:~/hello# ./hello
hello world

使用”make dist”命令将程序和相关的文档打包为一个压缩文档以供发布。

root@ubuntu:~/hello# make dist
root@ubuntu:~/hello# ls
aclocal.m4      config.status  hello             install-sh   missing
autom4te.cache  configure      hello-1.0.tar.gz  Makefile
autoscan.log    configure.ac   hello.c           Makefile.am
config.log      depcomp        hello.o           Makefile.in

hello-1.0.tar.gz为打包过后的文件
 
 

下面是另一种文档结构

|-- conf
|   `-- test.conf
`-- src
    |-- test1.c
    |-- test2.c
    `-- test2.h

src为源文件目录,conf为配置文件的目录。创建Makefile的方式和上面有一点点不同

1.执行autoscan命令

root@ubuntu:~/test# autoscan
root@ubuntu:~/test# ls
autoscan.log  conf  configure.scan  src
root@ubuntu:~/test# cat configure.scan
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/test2.h])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

修改configure.scan为configure.ac

root@ubuntu:~/test# mv configure.scan configure.ac
root@ubuntu:~/test# vim configure.ac
root@ubuntu:~/test# cat configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT(test, 1.0)
AM_INIT_AUTOMAKE(test, 1.0)
AC_CONFIG_SRCDIR([src/test2.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.
AC_CHECK_LIB(pthread, pthread_create)  //pthread为链接库名字, pthread_create为库中的一个函数

# Checks for header files.
AC_CHECK_HEADERS(stdlib.h string.h pthread.h) 

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT([Makefile
		    src/Makefile
		    conf/Makefile])             //每一个目录需要输出一个Makefile文件

2.使用aclocal工具生成aclocal.m4
3.使用autoconf工具生成configure文件
4. 创建Makefile.am
在顶层目录创建的Makefile.am的内容如下

root@ubuntu:~/test# vim Makefile.am
root@ubuntu:~/test# cat Makefile.am
SUBDIRS= src conf

在src目录下创建的Makefile.am内容如下

root@ubuntu:~/test# cd src/
root@ubuntu:~/test/src# vim Makefile.am
root@ubuntu:~/test/src# cat Makefile.am
bin_PROGRAMS= test
test_SOURCES= test1.c test2.c

在conf目录下创建的Makefile.am内容如下

root@ubuntu:~/test/conf# vim Makefile.am
root@ubuntu:~/test/conf# cat Makefile.am
configdir = /etc             //这个数据文件将要移动到的目录
config_DATA = test.conf    //数据文件名,如果有多个文件则这样写config_DATA = test1.dat test2.dat

5.使用Automake生成Makefile.in文件

root@ubuntu:~/test# automake --add-missing
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found

这里出现了一个问题,NEWS, README这几个文件不存在, 那么我们就手工创建他们

root@ubuntu:~/test# touch NEWS README AUTHORS ChangeLog

再次执行automake

root@ubuntu:~/test# automake

6.后面的步骤就和上面的例子一样了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
autoconf和automake是一组用于生成makefile文件的工具,用于自动化构建过程。autoconf主要用于检测系统环境和软件依赖,而automake用于生成makefile文件。 在使用autoconf生成makefile文件的过程中,首先需要创建一个configure.ac文件,该文件包含了一些宏和脚本代码。在configure.ac文件中,通过使用宏来检测系统环境,比如检测编译器、库和头文件的存在与版本。同时,还可以通过宏来检测用户配置选项,比如编译参数和安装路径等。 在configure.ac文件中,可以使用一些宏编写脚本代码来实现自定义操作。例如,可以使用AC_CHECK_LIB宏来检测某个库是否存在,AC_CHECK_HEADER宏来检测某个头文件是否存在,AC_CONFIG_FILES宏来定义要生成的makefile文件等。 然后,通过运行autoconf命令来生成一个configure脚本。这个configure脚本用于根据configure.ac文件中的宏和脚本代码来生成makefile.in文件,makefile.in文件是一个模板文件,它包含了一些变量和规则,但是还没有具体的值。 接下来,使用automake工具来生成最终的makefile文件。automake会读取configure.ac文件和makefile.in文件,并根据约定的规则自动生成makefile文件。在生成makefile文件的过程中,automake会根据configure.ac文件中的宏和脚本代码来解析变量的默认值和用户配置选项,以及生成各个编译目标和规则。最终生成的makefile文件可用于构建和安装软件。 总结来说,autoconf和automake是一对强大的工具,可以自动化生成makefile文件,减少开发人员的工作量。通过检测系统环境和用户配置选项,以及根据约定的规则生成makefile文件,可以方便地进行软件构建和安装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值