自动生成Linux下Makefile全攻略(automake原理)

清泛原创

本文假定大家对Makefile自动化编译有基本的了解(不了解的最好先熟悉下,参见《Makefile经典教程(入门必备)》),Linux编译安装软件的方法的主要步骤及输出结果如下:

1、./configure   输出Makefile文件
2、make   输出二进制
3、make install   安装二进制到指定目录

make及make install的规则都需要参照Makefile文件,而Makefile是自动生成的,大家有兴趣可以查看下Makefile,代码很长很复杂。当然我们可以直接在Makefile文件中写编译规则,只是这样复杂度很高,且修改起来不灵活。

为了解决这个难题,GNU推出了automake,根据简单的规则自动生成复杂的Makefile,automake help文档如下:
Usage: /usr/bin/automake [OPTION] ... [Makefile]...

Generate Makefile.in for configure from Makefile.am.

Operation modes:
      --help               print this help, then exit
      --version            print version number, then exit
  -v, --verbose            verbosely list files processed
      --no-force           only update Makefile.in's that are out of date
  -W, --warnings=CATEGORY  report the warnings falling in CATEGORY

Dependency tracking:
  -i, --ignore-deps      disable dependency tracking code
      --include-deps     enable dependency tracking code

Flavors:
      --cygnus           assume program is part of Cygnus-style tree
      --foreign          set strictness to foreign
      --gnits            set strictness to gnits
      --gnu              set strictness to gnu

Library files:
  -a, --add-missing      add missing standard files to package
      --libdir=DIR       directory storing library files
  -c, --copy             with -a, copy missing files (default is symlink)
  -f, --force-missing    force update of standard files

Warning categories include:
  `gnu'           GNU coding standards (default in gnu and gnits modes)
  `obsolete'      obsolete features or constructions
  `override'      user redefinitions of Automake rules or variables
  `portability'   portability issues (default in gnu and gnits modes)
  `syntax'        dubious syntactic constructs (default)
  `unsupported'   unsupported or incomplete features (default)
  `all'           all the warnings
  `no-CATEGORY'   turn off warnings in CATEGORY
  `none'          turn off all the warnings
  `error'         treat warnings as errors

Files which are automatically distributed, if found:
  ABOUT-GNU           README              config.rpath        ltcf-gcj.sh
  ABOUT-NLS           THANKS              config.sub          ltconfig
  AUTHORS             TODO                configure           ltmain.sh
  BACKLOG             acconfig.h          configure.ac        mdate-sh
  COPYING             aclocal.m4          configure.in        missing
  COPYING.DOC         ansi2knr.1          depcomp             mkinstalldirs
  COPYING.LESSER      ansi2knr.c          elisp-comp          py-compile
  COPYING.LIB         compile             install-sh          stamp-vti
  ChangeLog           config.guess        libversion.in       texinfo.tex
  INSTALL             config.h.bot        ltcf-c.sh           ylwrap
  NEWS                config.h.top        ltcf-cxx.sh

Report bugs to <bug-automake@gnu.org>.
使用automake我们只需要写Makefile.am文件,指定那些目录或文件需要参与编译,生成哪些内容等:
AUTOMAKE_OPTIONS=foreign

lib_LTLIBRARIES= liblog.la

noinst_HEADERS = \
				logging/LogClass.h \
                logging/Ilog.h \

liblogging_la_SOURCES = logging/LogClass.cc
AUTOMAKE_OPTIONS=foreign

#子目录
SUBDIRS = src sdk
规则是不是很简单呢?
除此之外,我们还需要一个configure.in文件,规则也很简单:
#指定项目的一个源文件
AC_INIT(dir1/code2.c)
#指定项目名称和版本号
AM_INIT_AUTOMAKE(prog1, 0.0.1)
#检查编译器
AC_PROG_CC
#输出Makefile文件
AC_OUTPUT(Makefile dir1/Makefile)
那么它是怎样生成Makefile的呢?下面通过一个Shell脚本简要说明一下执行步骤及输出
#!/bin/sh

# configure.in -> aclocal.m4
aclocal

# aclocal.m4 -> configure
autoconf

# avoid: required file `build/ltmain.sh' not found
# --copy            copy files rather than symlinking them
# --debug           enable verbose shell tracing
# --force           replace existing files
libtoolize --automake --copy --force

# Makefile.am + configure.in -> Makefile.in
automake --add-missing

#./configure

图示如下:

接下来,通过一个经典的例子来实战一下。
详细步骤请参见:https://www.tsingfun.com/it/cpp/1823.html

Makefile 自动编译 automake Makefile.am

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部