Linux工具之autoconf和automake

先搬一段别人的文章来说明为什么需要这两个工具:

无论是在Linux还是在Unix环境中,make都是一个非常重要的编译命令。不管是自己进行项目开发还是安装应用软件,我们都经常要用到make或 make install。利用make工具,我们可以将大型的开发项目分解成为多个更易于管理的模块,对于一个包括几百个源文件的应用程序,使用make和 makefile工具就可以轻而易举的理顺各个源文件之间纷繁复杂的相互关系。

但是如果通过查阅make的帮助文档来手工编写Makefile,对任何程序员都是一场挑战。幸而有GNU 提供的Autoconf及Automake这两套工具使得编写makefile不再是一个难题。

本文将介绍如何利用 GNU Autoconf 及 Automake 这两套工具来协助我们自动产生 Makefile文件,并且让开发出来的软件可以像大多数源码包那样,只需"./configure", "make","make install" 就可以把程序安装到系统中。

 说明autoconf automake这两个工具是帮助我们快捷规范的生成Makefile的,既然这两个小家伙这么有用,接下来看看怎么用

来个最简单的程序:

hello.c

#include <stdio.h>

int main()
{
	printf("hello.\n");
	
	return 0;
}

当前目录是这样的:

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 12
drwxrwxr-x 2 osrc osrc 4096 2月  25 18:43 ./
drwxrwxr-x 8 osrc osrc 4096 2月  25 10:20 ../
-rw-rw-r-- 1 osrc osrc   69 2月  25 18:40 hello.c
 

1. 在当前目录运行autoscan运行后是这样子的:

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# autoscan 
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/\${ <-- HERE [^\}]*}/ at /usr/bin/autoscan line 361.
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 16
drwxrwxr-x 2 osrc osrc 4096 2月  25 18:45 ./
drwxrwxr-x 8 osrc osrc 4096 2月  25 10:20 ../
-rw-r--r-- 1 root root    0 2月  25 18:45 autoscan.log
-rw-r--r-- 1 root root  467 2月  25 18:45 configure.scan
-rw-rw-r-- 1 osrc osrc   69 2月  25 18:40 hello.c
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# 
 

可以看到autoscan生成了  autoscan.logconfigure.scan 这两个文件;

2. 接下来将configure.scan重命名为configure.ac,并修改 configure.ac,

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# mv configure.scan configure.ac
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# gedit configure.ac 

修改如下,

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AM_INIT_AUTOMAKE

AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([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_CONFIG_FILES([Makefile]) 
AC_OUTPUT

configure.acconfigure.scan的基础上加了两条 :

AM_INIT_AUTOMAKE

AC_CONFIG_FILES([Makefile])

3.现在,我们再在当前目录下添加一个文件:Makefile.am

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# gedit Makefile.am

Makefile.am文件如下 ,可以看到文件指定了源文件hello.c和目标文件helloworld

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=hello.c

4. 执行aclocal   

 root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# aclocal  
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 68
drwxrwxr-x 3 osrc osrc  4096 2月  25 19:14 ./
drwxrwxr-x 8 osrc osrc  4096 2月  25 10:20 ../
-rw-r--r-- 1 root root 42141 2月  25 19:14 aclocal.m4
drwxr-xr-x 2 root root  4096 2月  25 19:14 autom4te.cache/
-rw-r--r-- 1 root root     0 2月  25 18:45 autoscan.log
-rw-r--r-- 1 root root   513 2月  25 19:03 configure.ac
-rw-rw-r-- 1 osrc osrc    69 2月  25 18:40 hello.c
-rw-r--r-- 1 root root    76 2月  25 19:04 Makefile.am

可以看到执行aclocal    后生成了aclocal.m4文件和  autom4te.cache/文件夹

5.执行autoheader

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# autoheader
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 72
drwxrwxr-x 3 osrc osrc  4096 2月  25 19:17 ./
drwxrwxr-x 8 osrc osrc  4096 2月  25 10:20 ../
-rw-r--r-- 1 root root 42141 2月  25 19:14 aclocal.m4
drwxr-xr-x 2 root root  4096 2月  25 19:17 autom4te.cache/
-rw-r--r-- 1 root root     0 2月  25 18:45 autoscan.log
-rw-r--r-- 1 root root   625 2月  25 19:17 config.h.in
-rw-r--r-- 1 root root   513 2月  25 19:03 configure.ac
-rw-rw-r-- 1 osrc osrc    69 2月  25 18:40 hello.c
-rw-r--r-- 1 root root    76 2月  25 19:04 Makefile.am
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work#

可以看到执行autoheader后生成了config.h.in文件

6.执行autoconf

 root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# autoconf
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 216
drwxrwxr-x 3 osrc osrc   4096 2月  25 19:19 ./
drwxrwxr-x 8 osrc osrc   4096 2月  25 10:20 ../
-rw-r--r-- 1 root root  42141 2月  25 19:14 aclocal.m4
drwxr-xr-x 2 root root   4096 2月  25 19:17 autom4te.cache/
-rw-r--r-- 1 root root      0 2月  25 18:45 autoscan.log
-rw-r--r-- 1 root root    625 2月  25 19:17 config.h.in
-rwxr-xr-x 1 root root 146266 2月  25 19:19 configure*
-rw-r--r-- 1 root root    513 2月  25 19:03 configure.ac
-rw-rw-r-- 1 osrc osrc     69 2月  25 18:40 hello.c
-rw-r--r-- 1 root root     76 2月  25 19:04 Makefile.am
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work#

可以看到执行autoconf后生成了configure*文件

7.执行automake --add-missing

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# automake --add-missing
configure.ac:12: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './depcomp'

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 240
drwxrwxr-x 3 osrc osrc   4096 2月  25 19:21 ./
drwxrwxr-x 8 osrc osrc   4096 2月  25 10:20 ../
-rw-r--r-- 1 root root  42141 2月  25 19:14 aclocal.m4
drwxr-xr-x 2 root root   4096 2月  25 19:21 autom4te.cache/
-rw-r--r-- 1 root root      0 2月  25 18:45 autoscan.log
lrwxrwxrwx 1 root root     32 2月  25 19:21 compile -> /usr/share/automake-1.15/compile*
-rw-r--r-- 1 root root    625 2月  25 19:17 config.h.in
-rwxr-xr-x 1 root root 146266 2月  25 19:19 configure*
-rw-r--r-- 1 root root    513 2月  25 19:03 configure.ac
lrwxrwxrwx 1 root root     32 2月  25 19:21 depcomp -> /usr/share/automake-1.15/depcomp*
-rw-rw-r-- 1 osrc osrc     69 2月  25 18:40 hello.c
lrwxrwxrwx 1 root root     35 2月  25 19:21 install-sh -> /usr/share/automake-1.15/install-sh*
-rw-r--r-- 1 root root     76 2月  25 19:04 Makefile.am
-rw-r--r-- 1 root root  23923 2月  25 19:21 Makefile.in
lrwxrwxrwx 1 root root     32 2月  25 19:21 missing -> /usr/share/automake-1.15/missing*
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work#  

可以看到执行automake --add-missing后生成了compile depcomp install-sh missing文件。

到这里基本上就完成了,现在我们可以像安装应用程序那样安装我们自己的程序了;

测试如下:

8.    ./configure 

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ./configure 
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
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 ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

 

9.    make

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# make
make  all-am
make[1]: Entering directory '/home/osrc/driver_test/autoconf_work'
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc  -g -O2   -o helloworld hello.o  
make[1]: Leaving directory '/home/osrc/driver_test/autoconf_work'
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 340
drwxrwxr-x 4 osrc osrc   4096 2月  25 19:25 ./
drwxrwxr-x 8 osrc osrc   4096 2月  25 10:20 ../
-rw-r--r-- 1 root root  42141 2月  25 19:14 aclocal.m4
drwxr-xr-x 2 root root   4096 2月  25 19:21 autom4te.cache/
-rw-r--r-- 1 root root      0 2月  25 18:45 autoscan.log
lrwxrwxrwx 1 root root     32 2月  25 19:21 compile -> /usr/share/automake-1.15/compile*
-rw-r--r-- 1 root root    824 2月  25 19:25 config.h
-rw-r--r-- 1 root root    625 2月  25 19:17 config.h.in
-rw-r--r-- 1 root root   9660 2月  25 19:25 config.log
-rwxr-xr-x 1 root root  32695 2月  25 19:25 config.status*
-rwxr-xr-x 1 root root 146266 2月  25 19:19 configure*
-rw-r--r-- 1 root root    513 2月  25 19:03 configure.ac
lrwxrwxrwx 1 root root     32 2月  25 19:21 depcomp -> /usr/share/automake-1.15/depcomp*
drwxr-xr-x 2 root root   4096 2月  25 19:25 .deps/
-rw-rw-r-- 1 osrc osrc     69 2月  25 18:40 hello.c
-rw-r--r-- 1 root root   6688 2月  25 19:25 hello.o
-rwxr-xr-x 1 root root  11424 2月  25 19:25 helloworld*
lrwxrwxrwx 1 root root     35 2月  25 19:21 install-sh -> /usr/share/automake-1.15/install-sh*
-rw-r--r-- 1 root root  23865 2月  25 19:25 Makefile
-rw-r--r-- 1 root root     76 2月  25 19:04 Makefile.am
-rw-r--r-- 1 root root  23923 2月  25 19:21 Makefile.in
lrwxrwxrwx 1 root root     32 2月  25 19:21 missing -> /usr/share/automake-1.15/missing*
-rw-r--r-- 1 root root     23 2月  25 19:25 stamp-h1
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# 

 

可以看到helloworld可执行程序已经生成了 , 执行一下

root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ./helloworld 
hello.
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# 

搞定!

关于这两个工具比较详细的介绍可以参考这篇文章https://www.ibm.com/developerworks/cn/linux/l-makefile/

 

参考链接:

https://www.ibm.com/developerworks/cn/linux/l-makefile/ 

https://blog.csdn.net/commshare/article/details/39964797

https://www.ibm.com/developerworks/cn/linux/l-makefile/

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值