读书人

应用Autotools编译项目(上)

发布时间: 2012-07-04 19:33:54 作者: rapoo

使用Autotools编译项目(上)
首先创建工作目录:

mkdir try-autotools


然后进入工作目录,创建helloworld.c:

int main() {  printf("Hello, world!");}


然后,输入下面的命令:

% autoscan


获得configure.scan:

% lsautoscan.log   configure.scan helloworld.c


然后将configure.scan重命名为configure.in:

% mv configure.scan configure.in


接下来执行aclocal:

% aclocal


执行结束后系统多出个autom4te.cache目录:

%  lsautom4te.cache autoscan.log   configure.in   helloworld.c


此时执行autoheader:

% autoheader


生成config.h.in:

% lsautom4te.cache config.h.in    configure.inautoscan.log   configure      helloworld.c


创建Makefile.am,内容如下:

bin_PROGRAMS=helloworldhelloworld_SOURCES=helloworld.c


接下来打开configure.in,在AC_INIT后面添加:

AM_INIT_AUTOMAKEAC_CONFIG_FILES([Makefile])


保存后退出。接下来使用touch命令创建如下文件:

% touch AUTHORS ChangeLog INSTALL NEWS README


然后执行:

% automake -a


会有一些告警日志,不管它。此时执行autoreconf,下面是执行结果及日志:

% autoreconf -vfi autoreconf: Entering directory `.'autoreconf: configure.in: not using Gettextautoreconf: running: aclocal --force autoreconf: configure.in: tracingautoreconf: configure.in: not using Libtoolautoreconf: running: /usr/bin/autoconf --forceautoreconf: running: /usr/bin/autoheader --forceautoreconf: running: automake --add-missing --copy --force-missingconfigure.in:6: installing `./missing'configure.in:6: installing `./install-sh'autoreconf: Leaving directory `.'


此时目录中内容如下:

% lsAUTHORS        Makefile.am    aclocal.m4     config.h.in~   helloworld.cCOPYING        Makefile.in    autom4te.cache configure      install-shChangeLog      NEWS           autoscan.log   configure.in   missingINSTALL        README         config.h.in    depcomp


工作基本到此结束了。我们现在可以使用生成好的configure命令试试看:

% ./configure


可以看到目录中生成了Makefile,执行make:

% makemake  all-amgcc -DHAVE_CONFIG_H -I.     -g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo -c -o helloworld.o helloworld.chelloworld.c: In function 'main':helloworld.c:2: warning: incompatible implicit declaration of built-in function 'printf'mv -f .deps/helloworld.Tpo .deps/helloworld.Pogcc  -g -O2   -o helloworld helloworld.o  


此时helloworld已经被编译了:

% ./helloworld Hello, world!


最后我们清除中间文件:

% make distclean


再删除autom4te的cache:

% rm -rf autom4te.cache


就得到了干净的工程目录:

% lsAUTHORS      INSTALL      NEWS         config.h.in  depcomp      missingCOPYING      Makefile.am  README       configure    helloworld.cChangeLog    Makefile.in  aclocal.m4   configure.in install-sh


将目录打包,就可以进行软件分发了。

读书人网 >开源软件

热点推荐