autotool 简单用法

本文介绍了一种使用Autotools简化跨平台软件构建过程的方法。从创建简单的hello.c开始,逐步介绍了autoscan、aclocal、autoconf等命令的使用流程,并生成configure文件,最后完成编译、安装及打包。

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

通过搜索资料和自身体会总结autotools的一个简单使用方法。


大致包括如下命令:

autoscan

aclocal

autoconf

autoheader

automake


首先创建hello.c文件并编辑

[cpp]  view plain  copy
 print ?
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     printf("hello\n");  
  6.     return 0;  
  7. }  

(1)autoscan

在源代码目录中执行 autoscan

生成configure.scan文件。

编辑configure.scan文件,通常添加如下两行

[plain]  view plain  copy
 print ?
  1. AM_INIT_AUTOMAKE(hello, 1.0)  
  2. AC_CONFIG_FILES([Makefile])  

并另存为configure.ac(或configure.in),和autoscan.log。

(2)aclocal

执行aclocal,工具根据configure.ac(或configure.in)生成aclocal.m4文件和autom4te.cache文件夹。

(3)autoconf

执行autoconf,生成configure文件。

(4)autoheader

执行autoheader,生成config.h.in文件。

(5)automake

新建文件Makefile.am并编辑

[plain]  view plain  copy
 print ?
  1. AUTOMAKE_OPTIONS=foreign  
  2. bin_PROGRAMS=hello  
  3. hello_SOURCES=hello.c  

执行automake,提示:
[plain]  view plain  copy
 print ?
  1. configure.ac:8: error: required file './install-sh' not found  
  2. configure.ac:8:   'automake --add-missing' can install 'install-sh'  
  3. configure.ac:8: error: required file './missing' not found  
  4. configure.ac:8:   'automake --add-missing' can install 'missing'  
  5. Makefile.am: error: required file './depcomp' not found  
  6. Makefile.am:   'automake --add-missing' can install 'depcomp'  
  7.    

执行automake --add-missing

再执行automake

后面的过程即为熟知的

./configure

make

make install


补充:

autotools工具还提供 make dist 打包功能

执行 make dist

根据configure.ac中 

AC_INIT( [ hello ], [ 1.0 ] )

生成hello-1.0.tar.gz的源码包文件。


总结:

在autotools的使用过程中,除了源代码以外,必须的文件只有两个 configure.ac 和 Makefile.am


<think>我们正在处理用户关于autotool的查询。用户希望获得autotool的使用指南或配置教程。根据提供的引用,特别是引用[1]中提到了Autotools的使用,我们可以以此为基础构建回答。引用[1]中提到了在Autotools项目中,如何确保头文件目录(include)在打包时被包含:需要在src目录的Makefile.am中通过include_HEADERS指定头文件,否则使用makedist时不会打包include文件夹。此外,我们还可以结合其他知识来丰富回答。Autotools是一组工具,包括autoconf、automake、libtool等,用于生成可移植的构建脚本。典型的Autotools项目配置过程包括运行autoscan、aclocal、autoconf、automake等命令。回答结构:1.介绍Autotools及其组件2.基本使用步骤(从创建项目到生成可发布的tar包)3.示例:一个简单的项目配置4.常见问题(如头文件打包问题)注意:用户要求使用中文回答,并按照指定的格式要求(数学表达式使用$...$,独立公式使用$$...$$,但本问题可能涉及较少数学公式,但如果有需要则按照格式)。由于问题涉及的是构建工具,可能较少涉及数学公式,但我们仍需要注意格式要求。同时,按照指令,我们需要在回答末尾生成相关问题。下面开始组织回答:</think>### Autotools 使用指南与配置教程 Autotools 是 GNU 构建系统的核心工具链,包含 **autoconf**、**automake** 和 **libtool** 三大组件,用于生成可移植的构建脚本(如 `configure` 脚本)。以下是详细使用流程: --- #### **一、基础工作流程** 1. **项目初始化** - 创建项目目录结构(示例): ``` project/ ├── src/ # 源代码 ├── include/ # 头文件 └── Makefile.am ``` - 在根目录创建 `configure.ac`(Autoconf 输入文件)和 `Makefile.am`(Automake 输入文件)。 2. **关键配置文件** - **`configure.ac`**:定义项目元数据和检查依赖 ```m4 AC_INIT([myapp], [1.0], [bug@example.com]) AM_INIT_AUTOMAKE AC_PROG_CC # 检查C编译器 AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefile]) # 指定输出Makefile AC_OUTPUT ``` - **`Makefile.am`**:描述构建规则 ```makefile bin_PROGRAMS = myapp # 生成可执行文件 myapp_SOURCES = src/main.c # 源文件 include_HEADERS = include/myapp.h # 头文件(确保打包时包含)[^1] ``` 3. **生成构建系统** 按顺序执行命令: ```bash aclocal # 生成aclocal.m4宏集合 autoconf # 生成configure脚本 automake --add-missing # 生成Makefile.in ``` 4. **用户端构建** ```bash ./configure # 检测系统环境并生成Makefile make # 编译项目 make install # 安装到系统 make dist # 打包源码为.tar.gz(包含include目录)[^1] ``` --- #### **二、多目录项目配置** 若项目含子目录(如 `src/` 和 `lib/`): 1. **根目录 `Makefile.am`**: ```makefile SUBDIRS = lib src # 指定子目录构建顺序 ``` 2. **子目录 `Makefile.am`**(示例 `lib/` 目录): ```makefile noinst_LTLIBRARIES = libutils.la # 生成静态库 libutils_la_SOURCES = utils.c ``` --- #### **三、常见问题解决** 1. **头文件未打包** 务必在 `Makefile.am` 声明 `include_HEADERS`,否则 `make dist` 会忽略头文件目录[^1]。 2. **依赖检测** 在 `configure.ac` 中添加检查: ```m4 AC_CHECK_LIB([m], [sqrt]) # 检查数学库 AC_CHECK_HEADERS([stdio.h]) # 检查标准头文件 ``` 3. **跨平台编译** 使用 `libtool` 编译共享库: ```makefile lib_LTLIBRARIES = libfoo.la libfoo_la_SOURCES = foo.c ``` --- #### **四、进阶技巧** - **自定义规则**:在 `Makefile.am` 中使用 `EXTRA_DIST` 添加非标准文件到发行包 - **条件编译**:在 `configure.ac` 中用 `AM_CONDITIONAL` 定义条件,在 `Makefile.am` 中通过 `if CONDITION` 控制编译 - **测试支持**:添加 `TESTS` 变量定义单元测试 > 提示:完整文档参考 [GNU Autotools Manual](https://www.gnu.org/software/automake/manual/)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值