【Makefile】自动生产makefile的工具autotools(autoconf和automake)

目录

前言

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的源码包文件。

posted on 2022-10-04 01:25  bdy  阅读(41)  评论(0编辑  收藏  举报

导航