Linux下Makefile的自动生成步骤

一、 Makefile 介绍 Makefile 是用于自动编译和链接的 ,一个工程有很多文件组成 ,每一个文件的改变都会导致工程的重新链接 ,但是不是

所有的文件都需要重新编译 ,Makefile 中纪录有文件的信息 ,在 make 时会决定在链接的时候需要重新编译哪些文件。

Makefile 的宗旨就是 :让编译器知道要编译一个文件需要依赖其他的哪些文件。当那些依赖文件有了改变 ,编译器会自动

的发现最终的生成文件已经过时 ,而重新编译相应的模块 。

使用 automake ,程序开发人员只需要写一些简单的含有预定义宏的文件,由 autocon f根据一个宏文件生成 configure ,

由 automake 根据另一个宏文件生成 Makefile.in ,再使用configure 依据 Makefile.in 来生成一个符合惯例的 Makefile 。下面

我们将详细介绍 Makefile的 automake 生成方法。

二、下面以 helloworld 为例

1、在自己的工作目录下建一 个 helloworld 目录,新建三个文件: helloworld.c

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

2、生成 configure

我们使用 autoscan 命令来帮助我们根据目录下的源代码生成一个 configure.in 的模板文件。

命令:

$ autoscan

执行后在 hellowrold 目录下会生成一个文件: configure.scan ,我们可以拿它作 为configure.in 的蓝本。

现在将 configure.scan 改名为 configure.in ,并且编辑它,按下面的内容修改,去掉无关的语句:

======================configure.in 内 容 开 始=================================# -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_INIT(helloworld.c)AM_INIT_AUTOMAKE(helloworld, 1.0)# 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)======================configure.in 内 容 结 束=================================

然后执行命令 aclocal 和 autoconf ,分别会产生 aclocal.m4 及 configure 两个文件:

$ aclocal

$ autoconf

$ ls

aclocal.m4 autom4te.cache configure configure.in helloworld.c

可以看到 configure.in 内容是一些宏定义,这些宏经 autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的 shell 脚本。

autoconf 是用来生成自动配置软件源代码脚本 ( configure ) 的工具。 configure 脚本能独立于 autoconf 运行,且在运行的过程中,不需要用户的干预。

要生成 configure 文件,你必须告诉 autoconf 如何找到你所用的宏。方式是使用 aclocal程序来生成你的 aclocal.m4 。

aclocal 根据 configure.in 文件的内容 , 自动生成 aclocal.m4 文件。 aclocal 是一个 perl 脚本程序,它的定义是 :“ aclocal – create aclocal.m4 by

scanningconfigure.ac ” 。

autoconf 从 configure.in 这个列举编译软件时所需要各种参数的模板文件中创 建configure 。

autoconf 需要 GNU m4 宏处理器来处理 aclocal.m4 ,生成 configure 脚本。

m4 是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏, m4 还有一些内建的函数,用来引用文件,

执行命令,整数运算,文本操作,循环等。 m4 既可以作为编译器的前端,也可以单独作为一个宏处理器。

3、新建 Makefile.am

AUTOMAKE_OPTIONS=foreignbin_PROGRAMS=helloworldhelloworld_SOURCES=helloworld.c

automake 会根据你写的 Makefile.am 来自动生成 Makefile.in 。

Makefile.am 中定义的宏和目标 , 会指导 automake 生成指定的代码。例如,宏bin_PROGRAMS 将导致编译和连接的目标被生成。

4、运行 automake

$ automake –add-missing

configure.in: installing `./install-sh’

configure.in: installing `./mkinstalldirs’

configure.in: installing `./missing’

Makefile.am: installing `./depcomp’

automake 会根据 Makefile.am 文件产生一些文件,包含最重要的 Makefile.in 。

5、执行 configure 生成 Makefile

$ ./configure

checking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables...checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ANSI C... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3configure: creating ./config.statusconfig.status: creating Makefileconfig.status: executing depfiles commands          

$ ls -l Makefile

-rw-rw-r– 1 yutao yutao 15035 Oct 15 10:40 Makefile

$ make

if gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE=

"helloworld"-DVERSION="1.0"-I. -I. -g -O2 -MT helloworld.o -MD -MP -MF ".deps/helloworld.Tpo"-c -o helloworld.o `test -f ‘helloworld.c’ || echo ‘./’`helloworld.c;

then mv -f ".deps/helloworld.Tpo" ".deps/helloworld.Po";else rm -f ".deps/helloworld.Tpo"; exit 1;

fi

gcc -g -O2 -o helloworld helloworld.o

运行 helloworld

$ ./helloworld

Hello World!三、针对上面提到的各个命令,我们再做些详细的介绍。 1 、 autoscan autoscan 是用来扫描源代码目录生成 configure.scan 文件的。autoscan 可以用目录名做为参数,但如果你不使用参数的话,那么 autoscan 将认为使用的是当前目录。

autoscan 将扫描你所指定目录中的源文件,并创建 configure.scan 文件。

2 、 configure.scan

configure.scan 包含了系统配置的基本选项,里面都是一些宏定义。我们需要将它改名为 configure.in

3 、 aclocal

aclocal 是一个perl 脚本程序。aclocal 根据configure.in 文件的内容,自动生成aclocal.m 4文件。aclocal 的定义是:“aclocal – create aclocal.m4 by scanning configure.ac ” 。

4 、 autoconf

autoconf 是用来产生 configure 文件的。configure 是一个脚本,它能设置源程序来适应各种不同的操作系统平台,并且根据不同的系统来产生合适的Makefile ,从而可以使

你的源代码能在不同的操作系统平台上被编译出来。configure.in 文件的内容是一些宏,这些宏经过 autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的 shell

脚本。 configure.in 文件中的宏的顺序并没有规定,但是必须在所有宏的最前面和最后面分别加上 AC_INIT 宏和 AC_OUTPUT 宏。

在 configure.ini 中:

# 号表示注释,这个宏后面的内容将被忽略。

AC_INIT(FILE)

这个宏用来检查源代码所在的路径。

AM_INIT_AUTOMAKE(PACKAGE, VERSION)

这个宏是必须的,它描述了我们将要生成的软件包的名字及其版本号: PACKAGE 是软件包的名字, VERSION 是版本号。当你使用 make dist 命令时,它会给你生成

一个类 似helloworld-1.0.tar.gz 的软件发行包,其中就有对应的软件包的名字和版本号。

AC_PROG_CC 这个宏将检查系统所用的 C 编译器。

AC_OUTPUT(FILE) 这个宏是我们要输出的 Makefile 的名字。

我们在使用 automake 时,实际上还需要用到其他的一些宏,但我们可以用 aclocal 来帮我们自动产生。执行aclocal 后我们会得到 aclocal.m4 文件。

产生了configure.in 和 aclocal.m4 两个宏文件后,我们就可以使用 autoconf 来产生configure 文件了。

5 、Makefile.am

Makefile.am 是用来生成 Makefile.in 的 , 需要你手工书写 。 Makefile.am 中定义了一些内容:

AUTOMAKE_OPTIONS

这个是 automake 的选项。在执行 automake 时,它会检查目录下是否存在标准 GNU 软件包中应具备的各种文件,例如 AUTHORS 、 ChangeLog 、 NEWS 等文件。

我们将其设置 成foreign 时, automake 会改用一般软件包的标准来检查。

bin_PROGRAMS

这个是指定我们所要产生的可执行文件的文件名 。 如果你要产生多个可执行文件 , 那么在各个名字间用空格隔开。

helloworld_SOURCES

这个是指定产生 “ helloworld ” 时所需要的源代码。如果它用到了多个源文件,那么请使用空格符号将它们隔开。比如需要 helloworld.h ,helloworld.c 那么请写成

helloworld_SOURCES= helloworld.h helloworld.c 。

如果你在 bin_PROGRAMS 定义了多个可执行文件 , 则对应每个可执行文件都要定义相对的 filename_SOURCES 。

6 、 automake

我们使用 automake –add-missing 来产生 Makefile.in 。选项 –add-missing 的定义是 “ add missing standard files to package ” , 它会让 automake 加入一个标准的

软件包所必须的一些文件。我们用 automake 产生出来的 Makefile.in 文件是符合 GNU Makefile 惯例的接下来我们只要执行 configure 这个 shell 脚本就可以产生合的

Makefile 文件了。

7 、 Makefile

在符合 GNU Makefiel 惯例的 Makefile 中,包含了一些基本的预先定义的操作:

make

根据 Makefile 编译源代码,连接,生成目标文件,可执行文件。

make clean

清除上次的 make 命令所产生的 object 文件(后缀为 “ .o ” 的文件)及可执行文件。

make install

将编译成功的可执行文件安装到系统目录中,一般为 /usr/local/bin 目录。

make dist

产生发布软件包文件 ( 即 distribution package ) 。 这个命令将会将可执行文件及相关文件打包成一个 tar.gz 压缩的文件用来作为发布软件的软件包。

它会在当前目录下生成一个名字类似“ PACKAGE-VERSION.tar.gz ” 的文件。PACKAG E和 VERSION,是我们在 configure.in 中定义的

AM_INIT_AUTOMAKE(PACKAGE,VERSION) 。

make distcheck

生成发布软件包并对其进行测试检查,以确定发布包的正确性。这个操作将自动把压缩包文件解开,然后执行 configure 命令,并且执行 make,来确认编译不出现错误,

最后提示你软件包已经准备好,可以发布了。

===============================================

helloworld-1.0.tar.gz is ready for distribution

===============================================

make distclean

类似 make clean ,但同时也将 configure 生成的文件全部删除掉,包括 Makefile 。

你的脸是为了呈现上帝赐给人类最贵重的礼物–微笑,一定要成为你工作最大的资产。

Linux下Makefile的自动生成步骤

相关文章:

你感兴趣的文章:

标签云: