【Makefile】自动生产makefile的工具autotools(autoconf和automake)和autogen.sh、configure、make 三剑客

目录

前言

autotools

安装autoTools

实战

1、创建目录

2、创建源码文件

3、用autoTools生成makefile


bandaoyu 本文随时更新,连接:https://blog.csdn.net/bandaoyu/article/details/108246849

前言

automake 属于第二代工具,尽量转到最新的构建工具吧,目前最新的构建工具应该是cmake和scons。

1.make工具,我们可以将大型的开发项目分解成为多个更易于管理的模块,使用make和 makefile工具理顺各个源文件之间纷繁复杂的相互关系。

手工编写Makefile,对任何程序员都是一场挑战。Autoconf及Automake这两套工具使得编写makefile不再是一个难题。

利用 GNU Autoconf 及 Automake 这两套工具来协助我们自动产生 Makefile文件,并且让开发出来的软件可以像大多数源码包那样,只需"./configure", "make","make install" 就可以把程序安装到系统中。

2.autotools生成makefile还是太麻烦,所以现在人们都开始倾向于用Cmake

autotools

为了生成Makefile需要使用以下工具。

  • autoscan
  • aclocal
  • autoconf
  • autoheader
  • automake

这些工具在autoconf安装包和automake安装包中。可以下载源码安装。在安装时还需m4配合。

4. autotools使用流程
第一步:手工编写Makefile.am这个文件
第二步:在源代码目录树的最高层运行autoscan。然后手动修改configure.scan文件,并改名为configure.ac/configure.in
第三步:运行aclocal,它会根据configure.ac的内容生成aclocal.m4文件
第四步:运行autoconf,它根据configure.ac和aclocal.m4的内容生成configure这个配置脚本文件
第五步:运行automake –add-missing,它根据Makefile.am的内容生成Makefile.in
第六步:运行configure,它会根据Makefile.in的内容生成Makefile这个文件

5. 流程图
autotools

安装autoTools

安装autoTools工具集,Centos下可以使用 yum install autoconf automake 在线安装。

yum install autoconf 

yum install automake

(yum -y install 包名(支持*) :自动选择y,全自动

 yum install 包名(支持*) :手动选择y or n)

实战

1、创建目录

建立一个TestautoTools的目录,这个目录将作为存放sayHello程序及其相关档案的地方。

2、创建源码文件

用编辑器在TestautoTools目录下创建sayHello.c源文件和sayHello.h头文件,内容如下:

hello.c

#include <stdio.h>
#include "hello.h"
int main()
{
    printf("Program say : %s\n" ,STR);
    return 0;
}

hello.h 

#ifndef SAYHELLO_H
#define SAYHELLO_H

#define STR "hello AutoTools"

#endif

3、用autoTools生成makefile

1、执行autoscan

在源代码目录中执行 autoscan,将会生成configure.scanautoscan.log文件。

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

AM_INIT_AUTOMAKE(hello, 1.0)
AC_CONFIG_FILES([Makefile])

重命名为configure.ac

设置项说明:

AC_CONFIG_SCRDIR:宏用来侦测所指定的源码文件是否存在, 来确定源码目录的有效性
AC_CONFIG_HEADER:宏用于生成config.h文件,以便 autoheader 命令使用
AC_PROG_CC:用来指定编译器,如果不指定,默认gcc
AC_CONFIG_FILES:宏用于生成相应的Makefile文件
AC_OUTPUT:用来设定 configure 所要产生的文件,如果是makefile,configure 会把它检查出来的结果带入makefile.in文件产生合适的makefile

2、执行aclocal

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

3、执行autoconf

执行autoconf,生成configure文件。

4、执行autoheader

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

5、automake /automake --add-missing

先新建文件Makefile.am,添加如下内容:

#SUBDIRS = lib
AUTOMAKE_OPTIONS = foreign #automake的等级,有三种。这里用foreign
bin_PROGRAMS = hello    #指出目标文件的名字,这里为hello
hello_SOURCES = hello.c #指出依赖,可以是多个
#hello_LDADD = lib/libprint.a

执行automake,提示:

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'
Makefile.am: error: required file './depcomp' not found
Makefile.am: 'automake --add-missing' can install 'depcomp'

执行automake --add-missing,再执行automake

此时就会创建Makefile文件了。 

 

Automake工具会根据 configure.in 中的参量把 Makefile.am 转换成 Makefile.in 文件
–add-missing:可以让 Automake 自动添加一些必需的脚本文件

# cat Makefile.am   //automake使用

SUBDIRS = lib
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = hello.c
hello_LDADD = lib/libprint.a

AUTOMAKE_OPTIONS为设置automake的选项。automake提供了3种软件等级:

  • foreign //只检测必须的文件
  • gnu //默认级别
  • gnits

SUBDIRS:子目录选项
bin_PROGRAMS:如果多个执行文件, 用空格隔开
hello_SOURCES:”hello”这个可执行程序所需的原始文件。如果”hello”这个程序是由多个源文件所产生, 所有源文件用空格隔开

# cat lib/Makefile.am

noinst_LIBRARIES = libprint.a

libprint_a_SOURCES = print.c ../include/print.h

在autotools的使用过程中,必须要编辑的文件只有configure.ac 和 Makefile.am

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

执行 make dist

根据configure.ac中

AC_INIT( [ hello ], [ 1.0 ] )

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

autogen.sh、configure、make 三剑客

autogen.sh、configure、make 三剑客 和autotools的关系:

autogen.sh 是一个脚本,它依次运行 autoscan、aclocal、autoconf、autoheader 和 automake,以生成 configure 和 Makefile。

configure 是用于配置构建系统的脚本,它检查系统环境并生成 Makefile。

make 是用于构建软件的工具,它根据 Makefile 文件执行构建任务。

===================================================================

这些工具和命令在Autotools构建系统中扮演了不同的角色。下面是它们的区别和作用:

1. autoscan:
autoscan是Autotools工具集中的一个命令,用于扫描项目源代码目录并生成一个初始的configure.ac文件。它会分析源代码中的Makefile.am、configure.in等文件,识别出可能需要配置的内容,以便后续进行自动化构建。

2. aclocal:
aclocal是Autotools工具集中的一个命令,用于生成aclocal.m4文件。这个文件包含了一些宏定义,这些宏定义是后续配置过程中可能用到的宏命令。aclocal命令会在系统的宏库路径(通常是/usr/share/aclocal或/usr/local/share/aclocal)搜索.m4文件,并根据这些文件生成aclocal.m4。

3. autoconf:
autoconf是Autotools工具集中的一个命令,用于根据configure.ac文件生成configure脚本。configure脚本是用来配置软件包的,它会根据系统环境和用户的配置选项生成适合当前平台的Makefile文件。

4. autoheader:
autoheader是Autotools工具集中的一个命令,用于生成config.h.in文件。这个文件包含了一些宏定义,通常用于在自动生成的configure脚本中替换配置选项并生成最终的config.h文件。

5. automake:
automake是Autotools工具集中的一个命令,用于生成Makefile.in文件。Makefile.in是一个通用的Makefile模板文件,它包含了一些变量和规则,可以根据具体的配置选项和平台特性生成最终的Makefile文件。

而"autogen.sh"、"configure"和"make"是一组用于自动化构建和配置软件的命令。这些命令通常与Autotools工具集一起使用:

1. autogen.sh:
autogen.sh是一个脚本文件,它负责运行autotools工具集中的各种命令(如autoscan、aclocal、autoconf、autoheader、automake等),以生成配置和构建所需的文件。它通常用于在软件源代码发布时自动生成所需的构建文

configure.ac 里面有VERSION信息,configure会把这些信息替换掉xx.in 文件里面的变量生成xxx文件
configure.ac:
AC_INIT([libfabric], [1.14.0], [ofiwg@lists.openfabrics.org])
vim libfabric.pc.in:
Version: @VERSION@

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值