使用autotools建立Makefile简单实例解析

本文中使用的实例仍为《手动建立makefile简单实例解析》中的5文件工程实例。

对于一个较大的项目而言,完全手动建立Makefile是一件费力而又容易出错的工作。autotools系列工具只需用户输入简单的目标文件、依 赖文件、文件目录等就可以比较轻松地生成Makefile了。现在Linux上的软件开发一般都是用autotools来制作Makefile。

autotools工具主要有:aclocal、autoscan、autoconf、autoheader、automake。使用autotools主要就是利用各个工具的脚本文件来生成最后的Makefile。下面结合实例来介绍具体的流程。

第一步 autoscan

使用autoscan在给定目录及其子目录树中检查源文件,如果没有给出目录,就在当前目录及其子目录树中进行检查。最终生成两个文件:configure.scan、autoscan.log

[armlinux@lqm autotools - easy]$ tree
.
|--  main.c
|--  mytool1.c
|--  mytool1.h
|--  mytool2.c
`
--  mytool2.h

0  directories,  5  files
[armlinux@lqm autotools
- easy]$ autoscan
[armlinux@lqm autotools
- easy]$ tree
.
|--  autoscan.log
|--  configure.scan
|--  main.c
|--  mytool1.c
|--  mytool1.h
|--  mytool2.c
`
--  mytool2.h

0  directories,  7  files

 其中,configure.scan是configure.in的原型文件。而configure.in是autoconf的脚本配置文件。所以下一步的工作就是要对configure.scan进行修改,将其转化为configure.in。

第二步   autoconf

configure.scan文件内容如下:

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

AC_PREREQ(
2.57 )
AC_INIT(FULL
- PACKAGE - NAME, VERSION, BUG - REPORT - ADDRESS)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([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_OUTPUT

说明:

1、以“#”号开始的是行为注释。
2、AC_PREREQ宏声明本文件要求的autoconf版本。
3、AC_INIT宏用来定义软件的名称和版本等信息,这里的BUG-REPORT-ADDRESS可以省略。
4、AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。这个参数一般不需要修改。
5、AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

修改时需要增加一个宏AM_INIT_AUTOMAKE(PACKAGE,VERSION),还要把AC_CONFIG_HEADER更改为AM_CONFIG_HEADER。具体如下:

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

AC_PREREQ(
2.57 )
#AC_INIT(FULL
- PACKAGE - NAME, VERSION, BUG - REPORT - ADDRESS)
AC_INIT(main,
1.0 )
AM_INIT_AUTOMAKE(main,
1.0 )
AC_CONFIG_SRCDIR([main.c])
AM_CONFIG_HEADER([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_OUTPUT(Makefile)

第三步  autoheader

首先把configure.scan更改名称为configure.in。然后执行aclocal,autoconf,autoheader。

[armlinux@lqm autotools - easy]$ mv configure.scan configure. in
[armlinux@lqm autotools
- easy]$ ls
autoscan.log  configure.
in   main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
[armlinux@lqm autotools
- easy]$ aclocal
[armlinux@lqm autotools
- easy]$ ls
aclocal.m4  autoscan.log  configure.
in   main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
[armlinux@lqm autotools
- easy]$ autoconf
[armlinux@lqm autotools
- easy]$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.
in   main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
[armlinux@lqm autotools
- easy]$ autoheader

第四步  automake

这是很重要的一步。automake需要的脚本配置文件是Makefile.am,这个文件需要自己建立。

AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS
= main
main_SOURCES
= main.c mytool1.c mytool1.h mytool2.c mytool2.h

 AUTOMAKE_OPTIONS为设置automake的选项。automake提供了3种软件等级:foreign、gnu、gnits,让用户选择使用,默认等级是gnu。现在使用的foreign只是检测必要的文件。

bin_PROGRAMS定义了要产生的执行文件名。如果产生多个可执行文件,每个文件名用空格隔开。

file_SOURCES定义file这个执行程序的依赖文件。同样的,对于多个执行文件,那就要定义相应的file_SOURCES。

接下来就是使用automake对其生成configure.in文件。这里可以使用选项--adding-missing让automake自动添加一些必要的脚本文件。

第五步  运行configure

[armlinux@lqm autotools - easy]$ automake  -- add - missing
[armlinux@lqm autotools
- easy]$ . / 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: creating config.h
config.status: config.h 
is  unchanged
config.status: executing depfiles commands

这样就完成了Makefile的制作。这是具有的功能:make、make install、make uninstall、make clean、make distclean、make dist。

1、键入make默认执行make all。其目标体为all。

[armlinux@lqm autotools - easy]$ make
make  all
- am
make[
1 ]: Entering directory ` / home / armlinux / program / autotools - easy '
source = ' main.c '   object = ' main.o '  libtool = no 
depfile
= ' .deps/main.Po '  tmpdepfile = ' .deps/main.TPo '  
depmode
= gcc3  / bin / sh . / depcomp 
gcc 
- DHAVE_CONFIG_H  - I.  - I.  - I.      - - O2  - c `test  - ' main.c '   ||  echo  ' ./ ' `main.c
source
= ' mytool1.c '   object = ' mytool1.o '  libtool = no 
depfile
= ' .deps/mytool1.Po '  tmpdepfile = ' .deps/mytool1.TPo '  
depmode
= gcc3  / bin / sh . / depcomp 
gcc 
- DHAVE_CONFIG_H  - I.  - I.  - I.      - - O2  - c `test  - ' mytool1.c '   ||  echo  ' ./ ' `mytool1.c
source
= ' mytool2.c '   object = ' mytool2.o '  libtool = no 
depfile
= ' .deps/mytool2.Po '  tmpdepfile = ' .deps/mytool2.TPo '  
depmode
= gcc3  / bin / sh . / depcomp 
gcc 
- DHAVE_CONFIG_H  - I.  - I.  - I.      - - O2  - c `test  - ' mytool2.c '   ||  echo  ' ./ ' `mytool2.c
gcc  
- - O2    - o main  main.o mytool1.o mytool2.o  
make[
1 ]: Leaving directory ` / home / armlinux / program / autotools - easy '

2、make install(uninstall)

3、make clean (distclean)

make clean仅仅是清除之前编译的可执行文件及配置文件。而make distclean要清除所有生成的文件。

[armlinux@lqm autotools - easy]$ make clean
test 
- " main "   ||  rm  - f main
rm 
- * .o core  * .core
[armlinux@lqm autotools
- easy]$ make distclean
test 
- " main "   ||  rm  - f main
rm 
- * .o core  * .core
rm 
- * .tab.c
rm 
- rf . / .deps
rm 
- f Makefile 
rm 
- f config.h stamp - h1
rm 
- f TAGS ID GTAGS GRTAGS GSYMS GPATH
rm 
- f config.status config.cache config.log configure.lineno

4、make dist

将所有的程序和相关的文档打包为一个压缩文件以供发布。

[armlinux@lqm autotools - easy]$ make dist
{ test 
!   - d main - 1.0   ||  { find main - 1.0   - type d  !   - perm  - 200   - exec chmod u + w {}  ' ; '   &&  rm  - fr main - 1.0 ; }; }
mkdir main
- 1.0
find main
- 1.0   - type d  !   - perm  - 777   - exec chmod a + rwx {} ;  -
  
!   - type d  !   - perm  - 444   - links  1   - exec chmod a + r {} ;  -
  
!   - type d  !   - perm  - 400   - exec chmod a + r {} ;  -
  
!   - type d  !   - perm  - 444   - exec  / bin / sh  / home / armlinux / program / autotools - easy / install - sh  - - m a + r {} {} ; 
||  chmod  - R a + r main - 1.0
/ bin / sh  / home / armlinux / program / autotools - easy / missing  -- run tar chof  -  main - 1.0   |  GZIP =-- best gzip  - > main - 1.0 .tar.gz
{ test 
!   - d main - 1.0   ||  { find main - 1.0   - type d  !   - perm  - 200   - exec chmod u + w {}  ' ; '   &&  rm  - fr main - 1.0 ; }; }
[armlinux@lqm autotools
- easy]$ ls
aclocal.m4      config.h     config.status  depcomp     main
- 1.0 .tar.gz  Makefile     missing        mytool1.h  mytool2.h
autom4te.cache  config.h.
in   configure      install - sh  main.c           Makefile.am  mkinstalldirs  mytool1.o  mytool2.o
autoscan.log    config.log   configure.
in    main        main.o           Makefile. in   mytool1.c      mytool2.c  stamp - h1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值