;
return 0;
}
完成后保存退出。
现helloworld目录下就应该有一个你自己写helloworld.c了。
3、生成configure
我们使用autoscan命令来帮助我们根据目录下源代码生成一个configure.in模板文件。
命令:
$ autoscan
$ ls
configure.scan helloworld.c
执行后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
$ls
aclocal.m4 configure.in helloworld.c
$ 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 scanning configure.ac”。
autoconf从configure.in这个列举编译软件时所需要各种参数模板文件中创建configure。
autoconf需要GNU m4宏处理器来处理aclocal.m4,生成configure脚本。
m4一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以内嵌,也可以用户定义。除了可以展开宏,m4还有一些内建函数,用来引用文件,执行命令,整数运算,文本操作,循环等。m4既可以作为编译器前端,也可以单独作为一个宏处理器。
4、新建Makefile.am
新建Makefile.am文件,命令:
$ vi Makefile.am
内容如下:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c
automake会根据你写Makefile.am来自动生成Makefile.in。
Makefile.am中定义宏和目标,会指导automake生成指定代码。例如,宏bin_PROGRAMS将导致编译和连接目标被生成。
5、运行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。
6、执行configure生成Makefile
$ ./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... 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
$ ls -l Makefile
-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile
你可以看到,此时Makefile已经产生出来了。
7、使用Makefile编译代码
$ make
if gcc -DPACKAGE_NAME=%26quot;%26quot; -DPACKAGE_TARNAME=%26quot;%26quot; -DPACKAGE_VERSION=%26quot;%26quot; -
DPACKAGE_STRING=%26quot;%26quot; -DPACKAGE_BUGREPORT=%26quot;%26quot; -DPACKAGE=%26quot;helloworld%26quot; -DVERSION=%26quot;1.0%26quot;
-I. -I. -g -O2 -MT helloworld.o -MD -MP -MF %26quot;.deps/helloworld.Tpo%26quot; \
-c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c; \
then mv -f %26quot;.deps/helloworld.Tpo%26quot; %26quot;.deps/helloworld.Po%26quot;; \
else rm -f %26quot;.deps/helloworld.Tpo%26quot;; exit 1; \
fi
gcc -g -O2 -o helloworld helloworld.o
运行helloworld
$ ./helloworld
Hello, Linux World!
这样helloworld就编译出来了,你如果按上面步骤来做话,应该也会很容易地编译出正确helloworld文件。你还可以试着使用一些其他make命令,如make clean,make install,make dist,看看它们会给你什么样效果。感觉如何?自己也能写出这么专业Makefile,老板一定会对你刮目相看。
四、深入浅出
针对上面提到各个命令,我们再做些详细介绍。
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.m4文件。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”文件。PACKAGE和VERSION,我们configure.in中定义AM_INIT_AUTOMAKE(PACKAGE, VERSION)。
make distcheck
生成发布软件包并对其进行测试检查,以确定发布包正确性。《automake/autoconf学习[转]》的文章内容来自于 ChinaUnix博客 。版权归原作者(佚名)所有,文章(automake/autoconf学习[转])的观点并不代表Linux计算机网立场!本站仅仅是对内容及资源进行整理后发布供网友查阅,若您是此文章作者且认为侵犯了您的版权请与管理员联系,文章发布时间 02-03 19:11:27。

