学习GNU/LINUX开发的编程人员,上手以后不久就会在编译开源软件的时候碰到configure脚本,过段时间还会知道configure脚本是 autoconf生成的;可是真正想用起来autoconf,倒是要弄明白config.h,configure.in,Makfile.am等一大堆的 文件,这可能要花些功夫。让咱们从一个例子开始,争取为你们省点力气。html
咱们用个小程序做例子,计算一个整数的开方,建个工做目录:sqrt。程序很简单:
#include
#include
int main()
{
int i=0;
scanf("%d",&i);
printf("sqrt(%d)=%f\n",i,sqrt(i));
}
接下来咱们要编译这个程序:
$cc -X -lm -o sqrt sqrt.c
由于咱们使用了数学库,因此要给连接器传递一个参数-lm。
程序就完成了。
咱们想专业一点,给这个程序增长Make文件,不用再输入那么长的命令,同时还想让这个程序成为可移植的程序。这个时候,咱们就须要用到autotools了。
autotools包含了几个部分,最经常使用到的是autoconf和automake。
咱们先加入autoconf。
autoconf须要一个configure.ac文件,幸运的是,咱们不须要本身写这个文件,咱们可使用autoscan来生成这个文件。执行autoscan。
$autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
错误信息先不用管,目录下多了几个文件: autoscan.log configure.scan。咱们要关心的是configure.scan,这是一个原始版本的configure.ac。打开这个文件,把下面这一行修改一下:
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
改为
AC_INIT([sqrt], [0.1.0], [you@mail.address])
参数的意思一目了然,不罗嗦了。接下来将文件另存为:configure.ac。执行:
$autoconf
咱们看到,目录下又多了一些内容,咱们关心的是configure这个脚本文件,执行一下试试吧!
不过如今这个configure还没什么用,要发挥configure的真正目的——识别编译环境,配置编译选项的话,还要进行一些操做。编程
首先编辑configure.ac文件,在咱们以前改动的AC_INIT...一行下面,加入以下一行内容:
AM_INIT_AUTOMAKE
再执行一次autoconf试试?很不幸,咱们遇到了错误:
configure.ac:6: error: possibly undefined macro: AM_INIT_AUTOMAKE...
由于找不到AM_INIT_AUTOMAKE宏,不要担忧,由于咱们少作了一步,先要把这些宏生成一下,固然是自动的。
$aclocal
$autoconf
如今的autoconf没有报错。这个时候再看看目录下面,发现多了一个aclocal.m4文件,这就是aclocal声称的宏命令文件,autoconf会使用它来生成新的configure脚本。
是否是如今就可以自动搞定Makefile了?咱们如今再执行一下configure,看看输出:
configure: error: cannot find install-sh or install.sh in . ./.. ./../..
和咱们想的有点不一样,咱们还要用到automake命令作一些其它的事情,咱们先执行一下:
$automake
configure.ac: required file `./install-sh' not found
configure.ac: required file `./missing' not found
automake: no `Makefile.am' found for any configure output
咱们注意到最后一行,知道了还须要一个`Makefile.am`文件,这个文件咱们要写一下,编辑一个文件,增长:
bin_PROGRAMS = sqrt
sqrt_SOURCES = sqrt.c
sqrt_LDADD = $(LIBOJBS)
执行automake试试?
configure.ac: required file `./install-sh' not found
configure.ac: required file `./missing' not found
automake: no `Makefile.am' found for any configure output
automake: Did you forget AC_CONFIG_FILES([Makefile]) in configure.ac?
哦,不行,还要install-sh,missing文件,错误信息中,还提到AC_CONFIG_FILES([Makefile]),是的,咱们还要修改一下configure.ac,在最后一行AC_OUTPUT前面增长一行:
AC_CONFIG_FILES([Makefile])
如今再执行一次automake吧,可是咱们要加一个参数:
$automake --add-missing
configure.ac: installing `./install-sh'
configure.ac: installing `./missing'
Makefile.am: installing `./INSTALL'
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
Makefile.am: installing `./COPYING'
configure.ac:8: required file `config.h.in' not found小程序
前面缺的四个文件简单,咱们按照本身的状况编辑保存便可,config.h.in从哪里来呢? 如今让咱们把config.h.in搞出来,这个要用到autoheader,咱们执行命令:
$autoheader
config.h.in文件就生成好了。准备好了其它几个文本文件,咱们再执行一次,此次不用加参数了,不过咱们还要再执行一次autoconf,由于咱们修改了configure.ac以后尚未执行过autoconf。
$autoconf
$automake
咱们再执行一次./configure:
$./configure
...
config.status: creating Makefile
config.status: creating config.h
...
让咱们执行一下make吧。
$make
...
/home/nevernew/sqrt/sqrt.c:7: undefined reference to `sqrt'
...
是由于咱们没有把数学库加入连接,修改Makefile.am,将对应行修改成:
sqrt_LDADD = $(LIBOBJS) -lm
更新一下文件:
$autoconf
$automake
$./configure
$make
$./sqrt学习
http://blog.chinaunix.net/uid-26133817-id-4281323.htmlui