利用autoconf和automake自动生成Makefile文件
2012-11-20 20:59
302 查看
众所周知,在Linux操作系统环境下,以源代码方式安装GNU软件的通常做法是第一步执行./configure,产生Makefile文件;第二步执行make,利用Makefile提供的信息对源代码进行编译,最好执行make install进行安装,将编译出来的可执行文件和库文件安装到默认或者指定的目录中去。
在一个大型的项目中,Makefile用手写不是不可以,但是不是那么太靠谱,有点不那么地道,利用autoconf和automake可以自动产生Makefile文件,相对来说要靠谱的多。
以妇孺皆知的helloworld程序为例,演示一下如果完成整个过程。
一、系统环境
操作系统:CentOS 5.5
Linux localhost.localdomain 2.6.18-194.el5#1 SMP Fri Apr 2 14:58:35 EDT 2010 i686 i686 i386 GNU/Linux
软件:
[bruce@localhost gnu]$ rpm -qa|grepautoconf
autoconf-2.59-12
[bruce@localhost gnu]$ rpm -qa|grepautomake
automake17-1.7.9-7.el5.2
automake16-1.6.3-8.el5.1
automake15-1.5-16.el5.2
automake14-1.4p6-13.el5.1
automake-1.9.6-2.3.el5
二、具体步骤
利用autoconf产生configure文件
1、编写Helloworld.c程序
2、执行autoscan命令,autoscan是用来扫描源代码目录生成configure.scan文件的。autoscan可以用目录名做为参数,但如果你不使用参数的话,那么 autoscan将认为使用的是当前目录。autoscan将扫描你所指定目录中的源文件,并创建configure.scan文件。
3、修改configure.scan文件为configure.in,configure.scan包含了系统配置的基本选项,里面都是一些宏定义。我们需要将它改名为configure.in
4、执行aclocal和autoconf命令,产生configure文件;aclocal是一个perl 脚本程序。aclocal根据configure.in文件的内容,自动生成aclocal.m4文件。aclocal的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。
autoconf是用来产生configure文件的。configure是一个脚本,它能设置源程序来适应各种不同的操作系统平台,并且根据不同的系统来产生合适的Makefile,从而可以使你的源代码能在不同的操作系统平台上被编译出来。configure.in文件的内容是一些宏,这些宏经过autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。
=======================================================================================================================
利用automake生成Makefile文件
1、新建Makefile.am文件,Makefile.am是用来生成Makefile.in的,需要你手工书写。
2、执行automake命令,我们使用automake --add-missing来产生Makefile.in。 选项--add-missing的定义是“add missing standard files to package”,它会让automake加入一个标准的软件包所必须的一些文件。
3、执行autoconf产生的configre可执行文件,生成Makefile文件
===================================================================================================================
1、执行make命令,对源代码进行编译
2、执行helloworld程序
==============================================================================
总结:
1、 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文件了。
2、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。
3、在符合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”的文件。PACKAGE和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。
现在就很清楚了,使用autoconf可以产生configure文件,使用automake产生Makefile,configure使用Makefile完成源代码的编译和安装。
在一个大型的项目中,Makefile用手写不是不可以,但是不是那么太靠谱,有点不那么地道,利用autoconf和automake可以自动产生Makefile文件,相对来说要靠谱的多。
以妇孺皆知的helloworld程序为例,演示一下如果完成整个过程。
一、系统环境
操作系统:CentOS 5.5
Linux localhost.localdomain 2.6.18-194.el5#1 SMP Fri Apr 2 14:58:35 EDT 2010 i686 i686 i386 GNU/Linux
软件:
[bruce@localhost gnu]$ rpm -qa|grepautoconf
autoconf-2.59-12
[bruce@localhost gnu]$ rpm -qa|grepautomake
automake17-1.7.9-7.el5.2
automake16-1.6.3-8.el5.1
automake15-1.5-16.el5.2
automake14-1.4p6-13.el5.1
automake-1.9.6-2.3.el5
二、具体步骤
利用autoconf产生configure文件
1、编写Helloworld.c程序
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
2、执行autoscan命令,autoscan是用来扫描源代码目录生成configure.scan文件的。autoscan可以用目录名做为参数,但如果你不使用参数的话,那么 autoscan将认为使用的是当前目录。autoscan将扫描你所指定目录中的源文件,并创建configure.scan文件。
NAME autoscan - Generate a preliminary configure.in SYNOPSIS autoscan [OPTION] ... [SRCDIR] DESCRIPTION Examine source files in the directory tree rooted at SRCDIR, or the current directory if none is given. Search the source files for common portability problems, check for incom- pleteness of ‘configure.ac’, and create a file ‘configure.scan’ which is a preliminary ‘configure.ac’ for that package.
[bruce@localhost helloworld]$ autoscan autom4te: configure.ac: no such file or directory autoscan: /usr/bin/autom4te failed with exit status: 1 [bruce@localhost helloworld]$ ll total 20 -rw-rw-r-- 1 bruce bruce 0 Nov 20 20:02 autoscan.log -rw-rw-r-- 1 bruce bruce 462 Nov 20 20:02 configure.scan -rw-rw-r-- 1 bruce bruce 76 Nov 20 20:02 helloworld.c [bruce@localhost helloworld]$
3、修改configure.scan文件为configure.in,configure.scan包含了系统配置的基本选项,里面都是一些宏定义。我们需要将它改名为configure.in
[bruce@localhost helloworld]$ mv configure.scan configure.in [bruce@localhost helloworld]$ ll total 20 -rw-rw-r-- 1 bruce bruce 0 Nov 20 20:02 autoscan.log -rw-rw-r-- 1 bruce bruce 462 Nov 20 20:02 configure.in -rw-rw-r-- 1 bruce bruce 76 Nov 20 20:02 helloworld.c [bruce@localhost helloworld]$ vi configure.in [bruce@localhost helloworld]$ cat configure.in # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ(2.59) AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS) AC_CONFIG_SRCDIR([helloworld.c]) AC_CONFIG_HEADER([config.h]) # 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 [bruce@localhost helloworld]$ vi configure.in [bruce@localhost helloworld]$ cat 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)
4、执行aclocal和autoconf命令,产生configure文件;aclocal是一个perl 脚本程序。aclocal根据configure.in文件的内容,自动生成aclocal.m4文件。aclocal的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。
autoconf是用来产生configure文件的。configure是一个脚本,它能设置源程序来适应各种不同的操作系统平台,并且根据不同的系统来产生合适的Makefile,从而可以使你的源代码能在不同的操作系统平台上被编译出来。configure.in文件的内容是一些宏,这些宏经过autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。
[bruce@localhost helloworld]$ aclocal [bruce@localhost helloworld]$ ll total 64 -rw-rw-r-- 1 bruce bruce 31120 Nov 20 20:11 aclocal.m4 drwxr-xr-x 2 bruce bruce 4096 Nov 20 20:11 autom4te.cache -rw-rw-r-- 1 bruce bruce 0 Nov 20 20:02 autoscan.log -rw-rw-r-- 1 bruce bruce 394 Nov 20 20:09 configure.in -rw-rw-r-- 1 bruce bruce 76 Nov 20 20:02 helloworld.c [bruce@localhost helloworld]$ autoconf [bruce@localhost helloworld]$ ll total 192 -rw-rw-r-- 1 bruce bruce 31120 Nov 20 20:11 aclocal.m4 drwxr-xr-x 2 bruce bruce 4096 Nov 20 20:12 autom4te.cache -rw-rw-r-- 1 bruce bruce 0 Nov 20 20:02 autoscan.log -rwxrwxr-x 1 bruce bruce 121800 Nov 20 20:12 configure -rw-rw-r-- 1 bruce bruce 394 Nov 20 20:09 configure.in -rw-rw-r-- 1 bruce bruce 76 Nov 20 20:02 helloworld.c [bruce@localhost helloworld]$
=======================================================================================================================
利用automake生成Makefile文件
1、新建Makefile.am文件,Makefile.am是用来生成Makefile.in的,需要你手工书写。
[bruce@localhost helloworld]$ vi Makefile.am [bruce@localhost helloworld]$ cat Makefile.am AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=helloworld helloworld_SOURCES=helloworld.c
2、执行automake命令,我们使用automake --add-missing来产生Makefile.in。 选项--add-missing的定义是“add missing standard files to package”,它会让automake加入一个标准的软件包所必须的一些文件。
[bruce@localhost helloworld]$ automake --add-missing configure.in: installing `./install-sh' configure.in: installing `./missing' Makefile.am: installing `./depcomp' [bruce@localhost helloworld]$ ll total 236 -rw-rw-r-- 1 bruce bruce 31120 Nov 20 20:11 aclocal.m4 drwxr-xr-x 2 bruce bruce 4096 Nov 20 20:20 autom4te.cache -rw-rw-r-- 1 bruce bruce 0 Nov 20 20:02 autoscan.log -rwxrwxr-x 1 bruce bruce 121800 Nov 20 20:12 configure -rw-rw-r-- 1 bruce bruce 394 Nov 20 20:09 configure.in lrwxrwxrwx 1 bruce bruce 31 Nov 20 20:20 depcomp -> /usr/share/automake-1.9/depcomp -rw-rw-r-- 1 bruce bruce 76 Nov 20 20:02 helloworld.c lrwxrwxrwx 1 bruce bruce 34 Nov 20 20:20 install-sh -> /usr/share/automake-1.9/install-sh -rw-rw-r-- 1 bruce bruce 81 Nov 20 20:19 Makefile.am -rw-rw-r-- 1 bruce bruce 16657 Nov 20 20:20 Makefile.in lrwxrwxrwx 1 bruce bruce 31 Nov 20 20:20 missing -> /usr/share/automake-1.9/missing
3、执行autoconf产生的configre可执行文件,生成Makefile文件
[bruce@localhost helloworld]$ ./configure checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes 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 ANSI C... none needed checking for style of include used by make... GNU checking dependency style of gcc... gcc3 configure: creating ./config.status config.status: creating Makefile config.status: executing depfiles commands [bruce@localhost helloworld]$ ll total 308 -rw-rw-r-- 1 bruce bruce 31120 Nov 20 20:11 aclocal.m4 drwxr-xr-x 2 br 4000 uce bruce 4096 Nov 20 20:20 autom4te.cache -rw-rw-r-- 1 bruce bruce 0 Nov 20 20:02 autoscan.log -rw-rw-r-- 1 bruce bruce 7701 Nov 20 20:21 config.log -rwxrwxr-x 1 bruce bruce 28780 Nov 20 20:21 config.status -rwxrwxr-x 1 bruce bruce 121800 Nov 20 20:12 configure -rw-rw-r-- 1 bruce bruce 394 Nov 20 20:09 configure.in lrwxrwxrwx 1 bruce bruce 31 Nov 20 20:20 depcomp -> /usr/share/automake-1.9/depcomp -rw-rw-r-- 1 bruce bruce 76 Nov 20 20:02 helloworld.c lrwxrwxrwx 1 bruce bruce 34 Nov 20 20:20 install-sh -> /usr/share/automake-1.9/install-sh -rw-rw-r-- 1 bruce bruce 16460 Nov 20 20:21 Makefile -rw-rw-r-- 1 bruce bruce 81 Nov 20 20:19 Makefile.am -rw-rw-r-- 1 bruce bruce 16657 Nov 20 20:20 Makefile.in lrwxrwxrwx 1 bruce bruce 31 Nov 20 20:20 missing -> /usr/share/automake-1.9/missing [bruce@localhost helloworld]$
===================================================================================================================
1、执行make命令,对源代码进行编译
[bruce@localhost helloworld]$ 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 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 [bruce@localhost helloworld]$ ll total 332 -rw-rw-r-- 1 bruce bruce 31120 Nov 20 20:11 aclocal.m4 drwxr-xr-x 2 bruce bruce 4096 Nov 20 20:20 autom4te.cache -rw-rw-r-- 1 bruce bruce 0 Nov 20 20:02 autoscan.log -rw-rw-r-- 1 bruce bruce 7701 Nov 20 20:21 config.log -rwxrwxr-x 1 bruce bruce 28780 Nov 20 20:21 config.status -rwxrwxr-x 1 bruce bruce 121800 Nov 20 20:12 configure -rw-rw-r-- 1 bruce bruce 394 Nov 20 20:09 configure.in lrwxrwxrwx 1 bruce bruce 31 Nov 20 20:20 depcomp -> /usr/share/automake-1.9/depcomp -rwxrwxr-x 1 bruce bruce 7194 Nov 20 20:25 helloworld -rw-rw-r-- 1 bruce bruce 76 Nov 20 20:02 helloworld.c -rw-rw-r-- 1 bruce bruce 4172 Nov 20 20:25 helloworld.o lrwxrwxrwx 1 bruce bruce 34 Nov 20 20:20 install-sh -> /usr/share/automake-1.9/install-sh -rw-rw-r-- 1 bruce bruce 16460 Nov 20 20:21 Makefile -rw-rw-r-- 1 bruce bruce 81 Nov 20 20:19 Makefile.am -rw-rw-r-- 1 bruce bruce 16657 Nov 20 20:20 Makefile.in lrwxrwxrwx 1 bruce bruce 31 Nov 20 20:20 missing -> /usr/share/automake-1.9/missing
2、执行helloworld程序
[bruce@localhost helloworld]$ ./helloworld Hello, World!
==============================================================================
总结:
1、 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文件了。
2、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。
3、在符合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”的文件。PACKAGE和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。
现在就很清楚了,使用autoconf可以产生configure文件,使用automake产生Makefile,configure使用Makefile完成源代码的编译和安装。
相关文章推荐
- 利用Linux下自动生成makefile的工具: automake, autoconf 生成makefile的一般过程
- 利用autoconf和automake来自动生成MakeFile
- 利用 autoconf 和 automake 自动生成Makefile
- 如何使用autoconf,automake,libtool自动生成Makefile文件
- Autoconf和Automake,自动生成Makefile
- automake 和 autoconf自动生成makefile 根据编译器进行宏定义
- Autoconf和Automake,自动生成Makefile
- 【探索wireshark】 使用autoconf, automake等自动生成Makefile
- 运用autoconf和automake自动生成Makefile实例讲解
- 利用automake工具自动生成makefile[总结]
- 用autoconf和automake自动生成makefile
- 使用automake 与 autoconf 自动生成 Makefile
- 运用 autoconf 和 automake 自动生成 Makefile 实例讲解
- 运用autoconf和automake自动生成Makefile实例
- 运用 autoconf 和 automake 自动生成 Makefile 实例讲解
- 编译安装thrift和使用autoconf,automake生成Makefile文件
- 运用autoconf和automake自动生成Makefile实例讲解
- 使用autoconf、automake自动生成Makefile实例讲解
- autoconf 和automake生成Makefile文件
- 运用autoconf和automake自动生成Makefile实例讲解