autotools使用流程

本文详细介绍了一套自动化构建工具的工作流程,包括使用autoscan检查源文件、利用autoconf生成configure脚本、通过automake创建Makefile等关键步骤。此外,还介绍了如何通过autoheader生成config.h.in文件,并最终通过make命令构建项目。

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

1.autoscan

[root@localhost automake]# autoscan
它会在给定目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目
录树中进行检查。它会搜索源文件以寻找一般的移植性问题并创建一个文件“configure.scan”,
该文件就是接下来autoconf要用到的“configure.in”原型。

2.autoconf
configure.in是autoconf的脚本配置文件,它的原型文件“configure.scan”如下所示:更改.scan文件,添加一些语句,并保存为configure.in文件。
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
#The next one is modified by sunq
#AC_INIT(FULL-PACKAGE-NAME,VERSION,BUG-REPORT-ADDRESS)      要注释掉
AC_INIT(hello,1.0)                                                   添加此句
# The next one is added by sunq
AM_INIT_AUTOMAKE(hello,1.0)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADER([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_CONFIG_FILES([Makefile])                                          添加此句
AC_OUTPUT

 

接下来首先运行aclocal,生成一个“aclocal.m4”文件,该文件主要处理本地的宏定义。
如下所示:
[root@localhost automake]# aclocal
再接着运行autoconf,生成“configure”可执行文件。如下所示:
[root@localhost automake]# autoconf
[root@localhost automake]# ls
aclocal.m4 autom4te.cache autoscan.log configure configure.in hello.c

3.autoheader
接着使用autoheader 命令,它负责生成config.h.in文件。该工具通常会从“acconfig.h”
文件中复制用户附加的符号定义,因此此处没有附加符号定义,所以不需要创建“acconfig.h”
文件。如下所示:
[root@localhost automake]# autoheader

4.automake
这一步是创建Makefile 很重要的一步,automake 要用的脚本配置文件是Makefile.am,
用户需要自己创建相应的文件。之后,automake工具转换成Makefile.in。在该例中,笔者创
建的文件为Makefile.am如下所示:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c  hello.h   要是有多个文件就全部列举出来

automake提供了3 种软件等级:foreign、gnu和gnits,让用户
选择采用,默认等级为gnu。在本例使用foreign等级,它只检测必须的文件

 

接下来可以使用automake 对其生成“configure.in”文件,在这里使用选项
“—adding-missing”可以让automake自动添加有一些必需的脚本文件。如下所示:
[root@localhost automake]# automake --add-missing
configure.in: installing './install-sh'
configure.in: installing './missing'
Makefile.am: installing 'depcomp'
[root@localhost automake]# ls
aclocal.m4 autoscan.log configure.in hello.c Makefile.am missing
autom4te.cache configure depcomp install-sh Makefile.in config.h.in

5.运行configure
在这一步中,通过运行自动配置设置文件configure,把Makefile.in 变成了最终的
Makefile。如下所示:
[root@localhost automake]# ./configure

6.make
键入make默认执行“make all”命令,即目标体为all,其执行情况如下所示:
[root@localhost automake]# make

此时在本目录下就生成了可执行文件“hello”,运行“./hello”能出现正常结果,如下
所示:
[root@localhost automake]# ./hello
Hello!Autoconf!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值