Linux Makefile:使用Autotools(autoconf/automake)自动生成Makefile(入门篇)

  Autotools包含一系列工具:automake、autoconf、autoscan、aclocal等工具组成,使用这组工具可以生成一个完整项目的Makefile。还是看流程图一目了然:
在这里插入图片描述

1、准备源文件

以最简单的helloworld为例:

book@Ubuntu:~/work/Autotools$ ls
hello.c
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ cat hello.c
#include <stdio.h>

int main(int argc, char *argv[])
{
    
    
	printf("Hello, world!\n");
	return 0;
}

2、使用autoscan工具生成configure.scan文件

book@Ubuntu:~/work/Autotools$ autoscan 
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/\${ <-- HERE [^\}]*}/ at /usr/bin/autoscan line 361.
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ ls
autoscan.log  configure.scan  hello.c

configure.scan内容解析:

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

AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([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_OUTPUT
  • AC_PREREQ:声明本文件要求的autoconf版本,这里是2.69;
  • AC_INIT:用于定义软件的名称和信息,依次为“软件名”、“软件版本”、“Bug反馈邮箱”;
  • AC_CONFIG_SRCDIR:用于检测指定的源文件是否存在,确定源码目录的有效性,这里只有一个hello.c文件;
  • AC_CONFIG_HEADERS:用于生成conifg.h文件,以便autoheader使用;
  • AC_PROG_CC:用于指定编译器,如果不指定则默认为gcc;
  • AC_OUTPUT:用于设定config要生成的文件,如果是Makefile,config就会把它检查出来的结果带入Makefile.in,最终生成Makefile。

3、重命名configure.scan为configure.ac并修改内容

book@Ubuntu:~/work/Autotools$ mv configure.scan configure.ac
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ vi configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([hello], [1.0], [[email protected]])   # 1、modify it
AM_INIT_AUTOMAKE(hello, 1.0) 				  # 2、added by yourself
AC_CONFIG_SRCDIR([hello.c])
# AC_CONFIG_HEADERS([config.h])               # 3、comment out

# 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)							  # 4、modify it

解析:
  按照上一小节configure.scan要求修改,然后需要删除或注释掉AC_CONFIG_HEADERS(否则需要autoheader工具生成config.h.in文件)手动添加AM_INIT_AUTOMAKE定义,它是automake所必备的宏。

4、使用aclocal工具生成aclocal.m4文件

book@Ubuntu:~/work/Autotools$ aclocal
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  hello.c

5、使用autoconf工具生成configure文件

book@Ubuntu:~/work/Autotools$ autoconf 
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  hello.c

6、创建Makefile.am文件

book@Ubuntu:~/work/Autotools$ vi Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCE=hello.c

解析:

  • AUTOMAKE_OPTIONS:automake选项,有三种等级:foreign、gnu(默认等级)、gnits。这里foreign只检测必须的文件;
  • bin_PROGRAMS:要生成的可执行文件名,如果生成多个可用空格隔开;
  • hello_SOURCE:表示要生成hello程序所需要的原始文件,如果生成多个可用空格隔开。

7、使用automake --add-missing命令生成Makefile.in文件

book@Ubuntu:~/work/Autotools$ automake --add-missing
configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './depcomp'
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ ls
aclocal.m4  autom4te.cache  autoscan.log  compile  configure  configure.ac  depcomp  hello.c  install-sh  Makefile.am  Makefile.in  missing

解析:
  使用“–add-missing”或“-a”选项让automake自动添加一些必要的脚本。

8、使用:配置configure把Makefile.in生成Makefile文件

book@Ubuntu:~/work/Autotools$ ./configure 
# 省略...
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ ls
aclocal.m4      autoscan.log  config.log     configure     depcomp  install-sh  Makefile.am  missing
autom4te.cache  compile       config.status  configure.ac  hello.c  Makefile    Makefile.in
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ make
# 省略...
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ ls 
aclocal.m4      autoscan.log  config.log     configure     depcomp  hello.c  install-sh  Makefile.am  missing
autom4te.cache  compile       config.status  configure.ac  hello    hello.o  Makefile    Makefile.in
book@Ubuntu:~/work/Autotools$ 
book@Ubuntu:~/work/Autotools$ ./hello 
Hello, world!

configure其他用法:

  • 指定程序运行环境(交叉编译):./configure --host=arm-linux-gnueabihf
  • 查看configure文件内容即可知道,包括安装路径等

make其他用法:

  • make:编译生成可执行文件;
  • make clean:清理编译产生的.o文件和可执行程序;
  • make install:安装到系统目录中,如果configure不指定--prefix目录则默认/usr/local/bin目录;
  • make dist:生成发布软件包,命名方式从configure.acAM_INIT_AUTOMAKE(hello,1.0)获取;
  • make distcheck:对发布的软件包自动进行解压、configure配置、make编译确保不会出现错误;
  • make distclean:将configure配置和make编译生成的文件全部删除,包括Makefile。

参考
  例解 autoconf 和 automake 生成 Makefile 文件 – IBM
  自动生成Makefile的全过程详解 – 博客园
  自动生成Makefile文件 – 百度经验

猜你喜欢

转载自blog.csdn.net/weixin_44498318/article/details/111414759