创建autotool项目

GNU Autotools是linux系统一套自动化编译工具,生成的项目可移植,通过configure && make即可生成目标程序。GNU Autotools组件有:autoscan, aclocal, autoconf, automake,autoheader等。
不用管这些工具的原理,只要知道他们都是干什么的就行。更不需要了解perl、m4语法,只需要了解autoconf、automake语法即可。

构建项目

Here are the steps to generate an autoconf C language project:

  1. 创建项目目录,创建子目录src,进入src目录创建main.cpp

  2. 运行autoscan命令生成configure.scan文件。 该文件包含稍后将生成的configure脚本的模板。

autoscan
  1. configure.scan重命名为configure.ac.
mv configure.scan configure.ac

自动生成的configure.ac内容如下:

#                                               -*- 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([src/main.cpp])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT
  1. 在项目根目录创建Makefile.am,内容如下:
#有几个子目录就添加几个
SUBDIRS = src
  1. 在src目录创建Makefile.am,内容如下:
#将test改为你要生成的可执行文件名
bin_PROGRAMS = test
test_SOURCES = main.cpp
  1. 修改configure.ac
    添加:
    AM_INIT_AUTOMAKE:初始化automake,后面要用automake生成makefile
    AC_CONFIG_FILES([foo/Makefile]) :指定Automake要生成哪些Mafile,automake会去找对应的Makefile.am,生成对应的Makefile.in文件。
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/main.cpp])
AC_CONFIG_HEADERS([config.h])
#有几个目录就添加几个
AC_CONFIG_FILES([src/Makefile])
AC_CONFIG_FILES([Makefile])

# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

如果此时执行autoconf,会报错:

Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
  1. 执行aclocal命令生成aclocal.m4文件。该文件包含configure 脚本使用的宏。
aclocal
  1. 执行autoconf命令,autoconf读取configure.ac 文件,生成configure脚本
autoconf
  1. 添加必要的说明文件
    如果不添加这些文件automake会报错。
touch NEWS README ChangeLog AUTHORS
  1. 执行automake命令,automake读取Makefile.am生成Makefile.in文件
automake --add-missing
  1. 执行autoheader命令,该命令生成config.h.in文件。
    configure脚本会用config.h.in生成config.h头文件,编译时会将config.h添加到你程序所有头文件里。

  2. 执行configure脚本。
    configure脚本会从Makefile.in文件生成Makefile。

  3. 执行make命令。

语法

autotool的语法可以在GNU网站搜索。
automake:https://www.gnu.org/software/automake/manual/automake.html
autoconf:https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.71/html_node/index.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值