Automake使用(初级)

工程地址

https://github.com/silvermagic/automakeDev.git

最初工程目录结构

$ vim main.cpp
$ vim src/main.cpp
 ls -l
total 12
drwxrwxr-x. 2 fedora fedora 4096 Jun 23 15:38 build-aux
drwxrwxr-x. 2 fedora fedora 4096 Jun 23 15:38 m4
drwxrwxr-x. 2 fedora fedora 4096 Jun 23 15:39 src
[fedora@bogon simple]$ ls -l src/
total 4
-rw-rw-r--. 1 fedora fedora 97 Jun 23 15:39 main.cpp

源文件剖析

$ cat src/main.cpp
#include <stdio.h>
#include "config.h"

int main()
{
    printf("Hello world!");
    return 0;
}

将工程改造成automake

运行autoscan

生成configure.scan文件,然后将此文件后缀改为.ac

$ autoscan
$ mv configure.scan configure.ac

修改configure.ac文件

后面autoconf将根据此文件生成最终的configure脚本

$ vim configure.ac
$ cat configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

# autoconf版本
AC_PREREQ([2.69])
# 初始化包信息,将会自动生成PACKAGE_NAME、PACKAGE_VERSION、PACKAGE_BUGREPORT宏
AC_INIT([simple], [1.0], [fwdssg@gmail.com])
# 通过检测目录中必定存在的文件来判断目录是否存在
AC_CONFIG_SRCDIR([src/main.cpp])
# 生成config.h文件保存configure.ac定义的宏,此文件可被源文件包含
AC_CONFIG_HEADERS([config.h])
# 用来存储本地宏文件,.m4的文件都将被保存进此目录,acloacl命令会自动创建此目录
AC_CONFIG_MACRO_DIRS([m4])
# 用来存储一些辅助脚本文件
AC_CONFIG_AUX_DIR([build-aux])
# 初始化automake
AM_INIT_AUTOMAKE([subdir-objects -Wno-portability])

# Checks for programs.
AC_PROG_CXX
# 初始化要连接的obj目录
AC_CONFIG_LIBOBJ_DIR([src])

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

# 设置运行configure后需要生成的文件,需要编写对应的.in文件,即需要生成Makefile,则必须存在Makefile.in文件,它们将被config.status使用用来生成Makefile
AC_CONFIG_FILES([ Makefile
    src/Makefile
])
AC_OUTPUT

运行aclocal

生成autoconf所需的宏文件aclocal.m4

$ aclocal

运行autoconf

根据configure.ac生成configure脚本

$ autoconf

运行autoheader

生成 config.h.in 文件,用于生成 config.h 文件,此文件保存了configure.ac中定义的宏,可被程序使用

$ autoheader

编写Makefile.am

顶层目录的Makefile.am
  • Makefile.ambuild-aux/gitlog-to-changelog 以及 AUTHORS.in 取自 Virt Viewer ,可以当做模板来使用

  • 一般情况下我们只需要根据 configure.acAC_CONFIG_FILES 来修改 Makefile.am 模板的 SUBDIRS 来决定需要生成Makefile的子目录,每个Makefile.am只需要包含子目录即可,如果有嵌套目录需要生成Makefile,则在子目录的Makefile.am设置 SUBDIRS

    $ ls Makefile.am build-aux/gitlog-to-changelog AUTHORS.in
    AUTHORS.in build-aux/gitlog-to-changelog Makefile.am

src目录的Makefile.am
$ git add src/Makefile.am

创建automake必要文件

COPYING 文件可以从一些开源项目下面复制过来,其他文件使用 touch 命令生成

$ ls ChangeLog AUTHORS NEWS README COPYING
AUTHORS  ChangeLog  COPYING  NEWS  README

运行automake

生成 Makefile.in 文件

$ automake -a

执行autogen.sh

  • autogen.sh 取自 Virt Viewer ,可以当做模板来使用
  • 根据实际情况修改 echo "Now type 'make' to compile Gettext." 即可
$ ./autogen.sh

运行make

$ make

执行程序

$ cd src/
$ ls
main.cpp  Makefile  Makefile.am  Makefile.in  Simple  Simple-main.o
$ ./Simple
Hello world!

清理环境

$ make maintainer-clean
posted @ 2017-10-14 10:20  银魔术师  阅读(825)  评论(0编辑  收藏  举报