#导入Word文档图片# Linux下automake工具使用(自动构建Makefile文件)

Posted DS小龙哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#导入Word文档图片# Linux下automake工具使用(自动构建Makefile文件)相关的知识,希望对你有一定的参考价值。


一、前言

linux环境下,当项目工程很大的时候,编译的过程很复杂,所以需要使用make工具,自动进行编译安装,但是手写makefile文件比较复杂,所幸在GNU的计划中,设计出了一种叫做Autoconf/Automake的工具,用来自动生成makefile文件,为编译和安装程序提供了一个方便快捷的入口。

无论是在Linux还是在Unix环境中,make都是一个非常重要的编译命令。不管是自己进行项目开发还是安装应用软件,我们都经常要用到make或 make install。利用make工具,我们可以将大型的开发项目分解成为多个更易于管理的模块,对于一个包括几百个源文件的应用程序,使用make和 makefile工具就可以轻而易举的理顺各个源文件之间纷繁复杂的相互关系。

但是如果通过查阅make的帮助文档来手工编写Makefile,对任何程序员都是一场挑战。

下面将介绍如何利用 GNU Autoconf 及 Automake 这两套工具来协助我们自动产生 Makefile文件,并且让开发出来的软件可以像大多数源码包那样,只需"./configure", "make","make install" 就可以把程序安装到系统中。

目前automake支持三种目录层次:flat、shallow和deep。

1) flat指的是所有文件都位于同一个目录中。

就是所有源文件、头文件以及其他库文件都位于当前目录中,且没有子目录。Termutils就是这一类。

2) shallow指的是主要的源代码都储存在顶层目录,其他各个部分则储存在子目录中。

就是主要源文件在当前目录中,而其它一些实现各部分功能的源文件位于各自不同的目录。automake本身就是这一类。

3) deep指的是所有源代码都被储存在子目录中;顶层目录主要包含配置信息。

就是所有源文件及自己写的头文件位于当前目录的一个子目录中,而当前目录里没有任何源文件。

二、安装Automake工具

1. 先检查当前系统里是否安装了工具。

[wbyq@wbyq tmp]$ which autoscan

安装成功的提示:

[wbyq@wbyq tmp]$ which autoscan

/usr/bin/autoscan


2. 如果系统没有安装Automake工具,红帽子系统可以挂载光盘找到安装包进行安装

如果是ubuntu系统可以通过命令在线下载工具: sudo apt-get install autoconf automake libtool

#导入Word文档图片#

#导入Word文档图片#

#导入Word文档图片#

#导入Word文档图片#

#导入Word文档图片#

#导入Word文档图片#

三、Automake工具使用方法

#导入Word文档图片#

3.1 现在一个目录下,编写好一个C代码文

#导入Word文档图片#

3. 2 执行autoscan命令生成configure.scan文件

生成configure.scan文件之后,将文件修改成configure.ac文件,如果没有这个.ac文件,执行aclocal命令的时候会报错。

#导入Word文档图片#

3.3 修改configure.ac文件参数

AC_PROG_RANLIB如果是多线程的程序的话要加入这句话,要不运行automake命令时会出错

每个configure.ac文件都是以AC_INIT开头,以AC_OUTPUT结束。

AC_INIT

测试程序

测试函数库

测试头文件

测试类型定义

测试结构

测试编译器特性

测试库函数

测试系统调用

AC_OUTPUT


#导入Word文档图片#

  • 关键字解释:AC_INIT()中分别的是:软件包的名字版本作者的联系方式(一般是Email)

示例: AC_INIT([app], [1.1.2], [1126626497@qq.com])

在下面再自己添加一行AM_INIT_AUTOMAKE(),里面填入: 程序名字,版本号。

示例: AM_INIT_AUTOMAKE(app,1.1.2)

最后AC_OUTPUT()填写生成的文件名称。

AC_OUTPUT(Makefile)

(1)AC_PREREQ宏声明本文件要求的autoconf版本,本例使用的版本为2.63

(2)AC_INIT宏用来定义软件的名称和版本等信息,”FULL-PACKAGE-NAME”为软件包名称,”VERSION”为软件版本号,”BUG-REPORT-ADDRESS”为BUG报告地址(一般为软件作者邮件地址)。

(3)AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。此处为当前目录下的app.c。

(4)AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

(5)AC_PROG_CC用来指定编译器,如果不指定,选用默认gcc。 比如: AC_PROG_CC(gcc)

(6)AC_OUTPUT用来设定 configure 所要产生的文件,如果是makefile,configure会把它检查出来的结果带入makefile.in文件产生合适的makefile。使用Automake时,还需要一些其他的参数,这些额外的宏用aclocal工具产生。

3.4 执行aclocal命令生成aclocal.m4文件

[wbyq@wbyq tmp]$ aclocal

3.5 使用autoconf工具生成configure文件

[wbyq@wbyq tmp]$ autoconf

3.6 使用autoheader生成config.h.in文件

[wbyq@wbyq tmp]$ autoheader

3.7 手工编辑Makefile.am文件

Automake工具会根据config.in中的参量把Makefile.am转换成Makefile.in文件。在使用Automake之前,要先手动建立Makefile.am文件。

Makefile.am是一种比Makefile更高层次的规则。只需指定要生成什么目标,它由什么源文件生成,要安装到什么目录等构成。

[wbyq@wbyq tmp]$ touch Makefile.am

文件创建之后,需要写三项代码:

第一项:软件规范,有三个候选项:foreign(国外),gnu(GNU),gnits

如果使用foreign等级,它只检测必须的文件

第二项:生成的可执行文件名

第三项:生成可执行文件所需的原始文件,有多个文件时用空格隔开。

  • 示例:

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=app

app_SOURCES=app.c

3.8 使用automake命令生成Makefile.in文件

添加选项--add-missing 可以让automake工具自动添加必要的脚本文件

注意: 不能在共享目录下执行,因为共享目录下是windows文件系统(FA32/NTFS),不支持link操作。

不然会报以下错误:

示例:

[wbyq@wbyq tmp]$ automake --add-missing

3.9 运行configure配置生成最终的Makefile文件

configure脚本为了让一个程序能够在各种不同类型的机器上运行而设计的。在使用make编译源代码之前,configure会根据自己所依赖的库而在目标机器上进行匹配。

约定俗成的,所有的configure脚本都把脚本文件名起为configure,一般来讲都是shell脚本,根据所在的系统环境生成makefile文件。

configure脚本运行时扫描当前环境,生成一个名为config.status的子脚本。子脚本将Makefile.in文件转换为适应于当前系统环境的Makefile文件。

[wbyq@wbyq tmp]$ ./configure

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking for gcc... gcc

checking for C compiler default output file name... a.out

checking whether the C compiler works... yes

checking whether we are cross compiling... no

checking for suffix of executables...

checking for suffix of object files... o

checking whether we are using the GNU C compiler... yes

checking whether gcc accepts -g... yes

checking for gcc option to accept ISO C89... none needed

checking for style of include used by make... GNU

checking dependency style of gcc... gcc3

checking how to run the C preprocessor... gcc -E

checking for grep that handles long lines and -e... /bin/grep

checking for egrep... /bin/grep -E

checking for ANSI C header files... yes

checking for sys/types.h... yes

checking for sys/stat.h... yes

checking for stdlib.h... yes

checking for string.h... yes

checking for memory.h... yes

checking for strings.h... yes

checking for inttypes.h... yes

checking for stdint.h... yes

checking for unistd.h... yes

checking for stdlib.h... (cached) yes

configure: creating ./config.status

config.status: creating Makefile

config.status: creating config.h

config.status: executing depfiles commands

3.10 使用Makefile编译,运行程序

[wbyq@wbyq tmp]$ make //编译程序

[wbyq@wbyq tmp]$ ./app 123 456 789 //运行程序

argv[0]=./app

argv[1]=123

argv[2]=456

argv[3]=789

3.11 Make支持的其他命令

[wbyq@wbyq app-1.1.2]$ make clean //清除目标文件

[wbyq@wbyq app-1.1.2]$ make dist //将源文件打包

[wbyq@wbyq app-1.1.2]$ make //编译源文件生成目标文件

[wbyq@wbyq app-1.1.2]$ make install //将目标文件拷贝到指定目录下

四、configure文件详解

`configure configures app 1.2.3 to adapt to many kinds of systems.


Usage: ./configure [OPTION]... [VAR=VALUE]...


To assign environment variables (e.g., CC, CFLAGS...), specify them as

VAR=VALUE. See below for descriptions of some of the useful variables.


Defaults for the options are specified in brackets.


Configuration:

-h, --help 显示此帮助并退出

--help=short 显示特定于此包的选项

--help=recursive 显示所有包含的简短帮助

-V, --version 显示版本信息并退出

-q, --quiet, --silent 不要打印“检查...”消息

--cache-file=FILE 在FILE中缓存测试结果[已禁用]

-C, --config-cache alias for `--cache-file=config.cache

-n, --no-create do not create output files

--srcdir=DIR find the sources in DIR [configure dir or `..]


Installation directories:

--prefix=PREFIX 在PREFIX中安装与体系结构无关的文件

[/usr/local]

--exec-prefix=EPREFIX 在EPREFIX中安装与体系结构相关的文件

[PREFIX]


By default, `make install will install all the files in

`/usr/local/bin, `/usr/local/lib etc. You can specify

an installation prefix other than `/usr/local using `--prefix,

for instance `--prefix=$HOME.


For better control, use the options below.


Fine tuning of the installation directories:

--bindir=DIR 用户可执行文件 [EPREFIX/bin]

--sbindir=DIR 系统管理可执行文件 [EPREFIX/sbin]

--libexecdir=DIR 程序可执行文件 [EPREFIX/libexec]

--sysconfdir=DIR read-only single-machine data [PREFIX/etc]

--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]

--localstatedir=DIR modifiable single-machine data [PREFIX/var]

--libdir=DIR 目标代码库 [EPREFIX/lib]

--includedir=DIR C header files [PREFIX/include]

--oldincludedir=DIR C header files for non-gcc [/usr/include]

--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]

--datadir=DIR read-only architecture-independent data [DATAROOTDIR]

--infodir=DIR info documentation [DATAROOTDIR/info]

--localedir=DIR locale-dependent data [DATAROOTDIR/locale]

--mandir=DIR man documentation [DATAROOTDIR/man]

--docdir=DIR documentation root [DATAROOTDIR/doc/app]

--htmldir=DIR html documentation [DOCDIR]

--dvidir=DIR dvi documentation [DOCDIR]

--pdfdir=DIR pdf documentation [DOCDIR]

--psdir=DIR ps documentation [DOCDIR]


Program names:

--program-prefix=PREFIX prepend PREFIX to installed program names

--program-suffix=SUFFIX append SUFFIX to installed program names

--program-transform-name=PROGRAM run sed PROGRAM on installed program names


Optional Features:

--disable-option-checking ignore unrecognized --enable/--with options

--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)

--enable-FEATURE[=ARG] include FEATURE [ARG=yes]

--disable-dependency-tracking speeds up one-time build

--enable-dependency-tracking do not reject slow dependency extractors


Some influential environment variables:

CC C compiler command

CFLAGS C compiler flags

LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a

nonstandard directory <lib dir>

LIBS libraries to pass to the linker, e.g. -l<library>

CPPFLAGS C/C++/Objective C preprocessor flags, e.g. -I<include dir> if

you have headers in a nonstandard directory <include dir>

CPP C preprocessor


Use these variables to override the choices made by `configure or to help

it to find libraries and programs with nonstandard names/locations.


Report bugs to <1126626497@qq.com>.

五、多个文件生成Makefile示例(同级目录)

  • 在目录下有3个.c、2个头文件#导入Word文档图片#
  • configure.ac文件内容: #导入Word文档图片#
  • Makefile.am文件内容:

#导入Word文档图片#


六、Makefile.am格式详解

6.1 Makefile.am中可用的全局变量

变量

含义

示例

INCLUDES

链接所需要的头文件


LDADD

链接所需要的库文件


LDFLAGS

链接所需要的库文件选项标志


EXTRA_DIST

配置打包时需要打包的其他文件


SUBDIRS

设置处理本目录之前需要递归处理的子目录


示例:

SUBDIRS=src/lib src/ModuleA/apple/shell src/ModuleA/apple/core

CURRENTPATH=$(shell /bin/pwd)

INCLUDES=-I$(CURRENTPATH)/src/include -I$(CURRENTPATH)/src/ModuleA/apple/include

export INCLUDES


6.2 Makefile.am中可用的路径变量

在Makefile.am中尽量使用相对路径,系统预定义了两个基本路径:

路径变量

含义

$(top_srcdir)

工程最顶层目录,用于引用源程序

$(top_builddir)

定义了生成目标文件最上层目录,用于引用.o 等一些编译出来的目标文件

#导入Word文档图片# Linux下目录编程

#导入Word文档图片# Linux下线程编程

#导入Word文档图片# Linux下文件目录权限操作

#导入Word文档图片# Linux下FrameBuffe(LCD)驱动编写

#导入Word文档图片# Linux下时间处理相关函数介绍

#导入Word文档图片# Linux下网络编程(socket)

(c)2006-2024 SYSTEM All Rights Reserved IT常识