Configure,Makefile.am, Makefile.in, Makefile文件之间关系

本文详细解析了自动化构建流程中的关键步骤,包括autoscan、aclocal、autoconf、automake、configure、make等,通过具体实例展示了如何从源代码到生成可执行程序的全过程。重点介绍了每个阶段的作用及相互关系,帮助开发者更好地理解和应用自动化构建工具。

自己:最近搞一个ntpdsim,一直没能生成libntpsim.a,所以一直没有成功生成ntpdsim。

            原因是对makefile的理解一直不太深,尤其是对automake之类的应用,没有理解他们之间的相对关系,还一直想自己动手写一个完整的Makefile,但是相对于庞大的工程真是无能为力。偶然看到这篇文章讲解了整个体系,大体是autoscan 、aclocal、autoconf、automake、configure、make的流程。

           在我的真是项目中,因为实际在根目录中已经自带了configure, 执行这个configure后,会在各个子目录中生成Makefile,然后到相应子目录中make,先在libntp 中生成libntpsim.a,然后在 ntpd中生成ntpdsim,这样会找到libntpsim.a。

原文章地址:http://hi.baidu.com/litaosmile/blog/item/0c5562139fe5ced9f6039ee3.html


1.autoscan (autoconf): 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是configure.ac的一个雏形。

    your source files --> [autoscan*] --> [configure.scan] --> configure.ac

2.aclocal (automake):根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到文件 aclocal.m4中。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”
user input files   optional input     process          output files
================   ==============     =======          ============

                    acinclude.m4 - - - - -.
                                          V
                                      .-------,
configure.ac ------------------------>|aclocal|
                 {user macro files} ->|       |------> aclocal.m4
                                      `-------'
3.autoheader(autoconf): 根据configure.ac中的某些宏,比如cpp宏定义,运行m4,声称config.h.in

user input files    optional input     process          output files
================    ==============     =======          ============

                    aclocal.m4 - - - - - - - .
                                             |
                                             V
                                     .----------,
configure.ac ----------------------->|autoheader|----> autoconfig.h.in
                                     `----------'

4.automake: automake将Makefile.am中定义的结构建立Makefile.in,然后configure脚本将生成的Makefile.in文件转换 为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,否则它 会自己产生config.guess和config.sub

user input files   optional input   processes          output files
================   ==============   =========          ============

                                     .--------,
                                     |        | - - -> COPYING
                                     |        | - - -> INSTALL
                                     |        |------> install-sh
                                     |        |------> missing
                                     |automake|------> mkinstalldirs
configure.ac ----------------------->|        |
Makefile.am  ----------------------->|        |------> Makefile.in
                                     |        |------> stamp-h.in
                                 .---+        | - - -> config.guess
                                 |   |        | - - -> config.sub
                                 |   `------+-'
                                 |          | - - - -> config.guess
                                 |libtoolize| - - - -> config.sub
                                 |          |--------> ltmain.sh
                                 |          |--------> ltconfig
                                 `----------'

5.autoconf:将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。

user input files   optional input   processes          output files
================   ==============   =========          ============

aclocal.m4 ,autoconfig.h.in - - - - - - -.
                                         V
                                     .--------,
configure.ac ----------------------->|autoconf|------> configure
 
6. ./configure的过程

.-------------> [config.cache] configure* --------------------------+-------------> config.log | [config.h.in] -. v .--> [autoconfig.h]
+-------> config.status* -+ Makefile.in ---' `--> Makefile
 
7. make过程
 
[autoconfig.h] -.
+--> make* ---> 程序
Makefile ---'
 
.---------,
                   config.site - - ->|         |
                  config.cache - - ->|configure| - - -> config.cache
                                     |         +-,
                                     `-+-------' |
                                       |         |----> config.status
                   config.h.in ------->|config-  |----> config.h
                   Makefile.in ------->|  .status|----> Makefile
                                       |         |----> stamp-h
                                       |         +--,
                                     .-+         |  |
                                     | `------+--'  |
                   ltmain.sh ------->|ltconfig|-------> libtool
                                     |        |     |
                                     `-+------'     |
                                       |config.guess|
                                       | config.sub |
                                       `------------'

.--------,
                   Makefile ------>|        |
                   config.h ------>|  make  |
{project sources} ---------------->|        |--------> {project targets}
                                 .-+        +--,
                                 | `--------'  |
                                 |   libtool   |
                                 |   missing   |
                                 |  install-sh |
                                 |mkinstalldirs|
                                 `-------------'

实例
在/hello/目录下创建一个hello.c文件,并编译运行它:

#cd /hello/

(1) 编写源文件hello.c:

#include<stdio.h>
int main(int argc, char** argv)
{
printf("Hello, GNU!n");
return 0;
}

[litao@vm0000131 hello]$ ll
total 4
-rw-rw-r-- 1 litao litao 68 Aug 12 12:02 hello.c

一、autoscan

[litao@vm0000131 hello]$ autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[litao@vm0000131 hello]$ ll
total 8
-rw-rw-r-- 1 litao litao   0 Aug 12 12:03 autoscan.log
-rw-rw-r-- 1 litao litao 457 Aug 12 12:03 configure.scan
-rw-rw-r-- 1 litao litao  68 Aug 12 12:02 hello.c

已经生成了configure.scan,autoscan.log文件

将configure.scan 修改为 configure.in,最后修改的内容如下:

[litao@vm0000131 hello]$ mv configure.scan configure.in   
[litao@vm0000131 hello]$ vim configure.in

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([hello.c])
#AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(hello, 1.0)
# 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)

二、acloacl

[litao@vm0000131 hello]$ aclocal

生成 aclocal.m4 和 autom4te.cache (生成aclocal.m4的过程中涉及到configure.in)

[litao@vm0000131 hello]$ ll
total 44
-rw-rw-r-- 1 litao litao 31120 Aug 12 12:08 aclocal.m4
drwxr-xr-x 2 litao litao  4096 Aug 12 12:08 autom4te.cache
-rw-rw-r-- 1 litao litao     0 Aug 12 12:03 autoscan.log
-rw-rw-r-- 1 litao litao   496 Aug 12 12:08 configure.in
-rw-rw-r-- 1 litao litao    68 Aug 12 12:02 hello.c

三、antoconf

[litao@vm0000131 hello]$ autoconf
生成 configure (根据 configure.in, 和 aclocal.m4)

[litao@vm0000131 hello]$ ll
total 168
-rw-rw-r-- 1 litao litao  31120 Aug 12 12:08 aclocal.m4
drwxr-xr-x 2 litao litao   4096 Aug 12 12:11 autom4te.cache
-rw-rw-r-- 1 litao litao      0 Aug 12 12:03 autoscan.log
-rwxrwxr-x 1 litao litao 122297 Aug 12 12:11 configure
-rw-rw-r-- 1 litao litao    496 Aug 12 12:08 configure.in
-rw-rw-r-- 1 litao litao     68 Aug 12 12:02 hello.c

四、编写Makefile.am:

AUTOMAKE_OPTIONS= foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c

五、automake

生成 Makefile.in, depcomp, install-sh, 和 missing (根据 Makefile.am, 和 aclocal.m4)

[litao@vm0000131 hello]$ automake
configure.in: required file `./install-sh' not found
configure.in: required file `./missing' not found
Makefile.am: required file `./depcomp' not found
[litao@vm0000131 hello]$ automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'
[litao@vm0000131 hello]$ ll
total 192
-rw-rw-r-- 1 litao litao  31120 Aug 12 12:08 aclocal.m4
drwxr-xr-x 2 litao litao   4096 Aug 12 12:14 autom4te.cache
-rw-rw-r-- 1 litao litao      0 Aug 12 12:03 autoscan.log
-rwxrwxr-x 1 litao litao 122297 Aug 12 12:11 configure
-rw-rw-r-- 1 litao litao    496 Aug 12 12:08 configure.in
lrwxrwxrwx 1 litao litao     31 Aug 12 12:16 depcomp -> /usr/share/automake-1.9/depcomp
-rw-rw-r-- 1 litao litao     68 Aug 12 12:02 hello.c
lrwxrwxrwx 1 litao litao     34 Aug 12 12:16 install-sh -> /usr/share/automake-1.9/install-sh
-rw-rw-r-- 1 litao litao     69 Aug 12 12:15 Makefile.am
-rw-rw-r-- 1 litao litao  16561 Aug 12 12:16 Makefile.in
lrwxrwxrwx 1 litao litao     31 Aug 12 12:16 missing -> /usr/share/automake-1.9/missing

六、configure
生成 Makefile, config.log, 和 config.status



<think>嗯,用户之前问了如何指定Makefile文件,现在又进一步想了解Makefile.amMakefile.in的区别以及它们的用途。我需要结合之前的回答和用户提供的引用内容来组织答案。 首先,用户的问题分为两个部分:如何在make命令中指定Makefile文件,以及Makefile.amMakefile.in的区别。关于指定Makefile文件的部分,我之前已经详细回答了,可能需要简要回顾一下,但重点放在区别上。 用户提供的引用提到Makefile.am由程序员编写,automake生成Makefile.in,再由configure脚本生成最终的Makefile。需要明确这三者的关系,以及各自的作用。用户可能在使用automake工具链时遇到困惑,所以需要解释清楚每个文件的用途和生成过程。 要注意用户可能对autotools的工作流程不太熟悉,所以需要用简单的中文解释,避免术语过多。同时,结合之前的回答,强调如何指定自定义名称的Makefile文件,特别是当使用automake生成的情况下,可能需要指定不同的文件。 另外,用户可能在开发中需要手动编写Makefile,或者使用自动生成工具,所以需要区分不同场景下的使用方式。比如,在automake流程中,用户通常不会直接修改Makefile.in,而是通过修改Makefile.am来自动生成。而手动编写的Makefile则不需要这些步骤。 还要注意引用中的内容,比如引用[1]和[2]提到automake生成Makefile.inconfigure生成Makefile,引用[3]提到手动编写符合惯例的Makefile的难度。这些内容需要整合到回答中,说明为什么会有这些文件的存在,以及它们如何简化开发流程。 最后,确保回答结构清晰,分点说明,并包含操作示例,帮助用户理解如何在实际中使用这些文件。可能还需要提到常见错误,比如在automake流程中直接修改Makefile.in导致的问题,提醒用户正确的工作流程。</think>#### **一、指定Makefile文件的完整方法** 当使用非默认名称的Makefile时,必须通过 `-f` 参数显式指定文件。以下是具体场景及验证方法: --- ##### **1. 基础语法** ```bash make -f <自定义文件名> [目标] -j$(nproc) ``` - **示例**: ```bash make -f Buildfile clean # 执行清理操作 make -f Buildfile install -j8 # 带并行编译的安装 ``` ##### **2. 路径处理** - **相对路径**: ```bash make -f ./build/debug.mk ``` - **绝对路径**: ```bash make -f /opt/project/config/arm64.mk ``` ##### **3. 动态验证** 通过 `ls` 和 `file` 命令验证文件有效性: ```bash ls -l Buildfile &amp;&amp; file Buildfile # 确认文件存在且为文本文件 ``` --- #### **二、Makefile.amMakefile.in的核心区别** 通过Autotools工具链(automake/autoconf)的生成流程理解二者关系: ##### **1. 文件角色对比表** | **文件类型** | **创建者** | **修改权限** | **典型内容** | **生命周期阶段** | |------------------|---------------|--------------|----------------------------------|---------------------| | `Makefile.am` | 开发者手动编写 | 可编辑 | 声明目标、源码、编译选项 | 项目初始化阶段 | | `Makefile.in` | automake生成 | 禁止直接修改 | 含`@VARIABLE@`的模板 | `./configure`执行前 | | `Makefile` | configure生成 | 自动生成 | 替换变量后的完整编译指令 | 构建阶段 | ##### **2. 生成流程详解** ```mermaid graph TD A[Makefile.am] -->|automake| B(Makefile.in) B -->|configure脚本| C(Makefile) C -->|make命令| D[可执行文件] ``` - **关键过程**: 1. 开发者编写 `Makefile.am`,定义如: ```makefile bin_PROGRAMS = hello hello_SOURCES = main.c utils.c ``` 2. 运行 `automake --add-missing` 生成 `Makefile.in`(包含平台相关占位符) 3. `./configure` 扫描系统环境,将 `Makefile.in` 中的 `@CC@`、`@CFLAGS@` 替换为实际值,输出最终 `Makefile` ##### **3. 文件内容差异示例** - **Makefile.am**(开发者维护): ```makefile ACLOCAL_AMFLAGS = -I m4 SUBDIRS = src lib ``` - **Makefile.in**(自动生成): ```makefile CC = @CC@ CFLAGS = @CFLAGS@ all: $(SUBDIRS) ``` --- #### **三、Autotools工具链最佳实践** ##### **1. 开发环境配置** ```bash # 安装必要工具 sudo apt-get install automake autoconf libtool # 初始化项目 autoscan &amp;&amp; mv configure.scan configure.ac aclocal autoheader automake --add-missing autoconf ``` ##### **2. 常见问题排查** - **错误**:`missing automake-1.16` - **解决方案**: ```bash autoreconf --install # 自动修复工具链版本 ``` - **错误**:`undefined reference to 'function_name'` - **根源**:`Makefile.am` 中缺少库链接声明 - **修正**: ```makefile hello_LDADD = -lmylib # 添加链接库 ``` --- #### **四、手动编写 vs 自动生成的抉择** | **场景** | **推荐方案** | **优势** | |------------------------|--------------------|-----------------------------------| | 小型单文件项目 | 手动编写Makefile | 简单直接,无需复杂配置 | | 跨平台C/C++项目 | 使用Autotools | 自动处理编译器差异和依赖检测 | | 需要打包RPM/DEB | 必须用Autotools | 符合Linux发行版打包规范[^1] | | 嵌入式系统交叉编译 | CMake+Autotools组合| 兼顾灵活性和自动化(引用[3]) | --- #### **五、引用说明** - **Makefile.in的模板作用**:允许configure阶段动态插入系统特定参数(如编译器路径)[^1] - **automake --add-missing**:自动补全缺失的标准文件(如`INSTALL`、`COPYING`)[^2] - **跨平台构建**:Autotools可自动检测POSIX兼容性问题和硬件特性(引用[3])
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值