autoconf安装自动编译工具介绍(3)

《linux高级程序设计》第2章Linux下C语言开发工具,这一章主要介绍Linux下进行C语言程序开发所必备的工具。本节为Autoconf/Automake工具组简介。
首页 新闻资讯 行业资讯 autoconf安装自动编译工具介绍(3)

autoconf安装自动编译工具介绍(3)

7.使用Automake生成Makefile.in文件

下面使用Automake生成"Makefile.in"文件,使用选项"--add-missing"可以让Automake自动添加一些必需的脚本文件。如下所示:


复制

[root@localhost hello]# automake --add-missing
configure.ac: installing './install-sh'    //创建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'
Makefile.am: installing './depcomp'
[root@localhost hello]# automake --add-missing //再运行一次,可以辅助生成几个必要的文件
Makefile.am: required file './NEWS' not found //没有找到NEWS文件
Makefile.am: required file './README' not found
Makefile.am: required file './AUTHORS' not found
Makefile.am: required file './ChangeLog' not found 
[root@localhost hello]# touch NEWS    //创建NEWS文件,如果没有自动生成,手工创建
[root@localhost hello]# touch README   //创建README文件
[root@localhost hello]# touch AUTHORS   //创建AUTHORS文件
[root@localhost hello]# touch ChangeLog   //创建ChangeLog文件
[root@localhost hello]# automake --add-missing //再运行一次
[root@localhost hello]# ls      //生成必要的文件
aclocal.m4      ChangeLog     configure.scan  INSTALL      missing
AUTHORS         config.h.in   COPYING         install-sh   NEWS
autom4te.cache  configure     depcomp         Makefile.am  README
autoscan.log    configure.ac  hello.c         Makefile.in
[root@localhost hello]# ls configure.in -l
-rw-r--r--  1 root root 536 Dec 27 04:29 configure.in
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.


8.autoconf安装配置

运行自动配置设置文件configure,把Makefile.in变成最终的Makefile。


复制

[root@localhost hello]# ./configure    //配置,生成Makefile文件
……
config.status: creating Makefile
config.status: executing depfiles commands
[root@localhost hello]# ls
aclocal.m4      ChangeLog    config.status   COPYING  install-sh   missing
AUTHORS         config.h     configure       depcomp  Makefile     NEWS
autom4te.cache  config.h.in  configure.ac    hello.c  Makefile.am  README
autoscan.log    config.log   configure.scan  INSTALL  Makefile.in  stamp-h1
[root@localhost hello]# ls  -l  Makefile*
-rw-r--r--  1 root root 16876 Dec 27 04:51 Makefile
-rw-r--r--  1 root root    68 Dec 27 04:46 Makefile.am
-rw-r--r--  1 root root 17180 Dec 27 04:50 Makefile.in
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.


9.autoconf安装测试

运行make命令进行编译,下面的示例中Make的主要编译命令为"gcc  -g -O2   -o hello  hello.o",此句对应本节前面介绍的手工编辑的Makefile文件内容。
 


复制

[root@localhost hello]# cd ../hello
[root@localhost hello]# make     //执行make命令
make  all-am
……
gcc  -g -O2   -o hello  hello.o      //编译指令
……
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.


编译成功后,将在当前目录下生成并运行可执行程序hello。测试源代码是否正确:

 


复制

[root@localhost hello_2]# ./hello 
hello!GNU
  • 1.

  • 2.

此方法生成的makefile文件很全面。使用"make install"命令把目标文件安装在系统中。


 


复制

[root@localhost hello]# make install      //安装
make[1]: Entering directory '/root/book/ch02/ch0206/hello'
test -z "/usr/local/bin" || mkdir -p -- "/usr/local/bin"
  /usr/bin/install -c 'hello' '/usr/local/bin/hello'   //安装目标文件
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory '/root/book/ch02/ch0206/hello'
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.


使用"make uninstall"命令把目标文件从系统中卸载。

 


复制

[root@localhost hello]# make uninstall      //卸载命令
 rm -f '/usr/local/bin/hello'        //从系统中卸载
  • 1.

  • 2.


使用"make clean"命令清除编译时的obj文件。


复制

[root@localhost hello]# make clean
test -z "hello" || rm -f hello
rm -f *.o            //删除obj文件
  • 1.

  • 2.

  • 3.


使用"make dist"命令将程序和相关的文档打包为一个压缩文档以供发布。


复制

[root@localhost hello]# make dist
……
[root@localhost hello]# ls         //查看生成的文件
aclocal.m4      config.h.in    configure.scan    install-sh   README
AUTHORS         config.h.in~   COPYING           Makefile     stamp-h1
autom4te.cache  config.log     depcomp           Makefile.am
Changelog       config.status  hello-1.0.tar.gz  Makefile.in
ChangeLog       configure      hello.c           missing
config.h        configure.ac   INSTALL           NEWS
[root@localhost hello]# ls hello-1.0.tar.gz -l    //打包文件
-rw-r--r--  1 root root 67699 Dec 27 06:33 hello-1.0.tar.gz
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.


【编辑推荐】

  1. Linux 查看磁盘空间实现代码介绍

  2. Linux操作系统需要微软的十大帮助

  3. 探寻Linux到底需要多低的配置

  4. Linux测试工具tcpdump监视TCP/IP连接命令介绍

  5. Linux流量控制实例应用介绍

14    2010-06-22 15:26:58    autoconf安装