Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130167
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 633
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-05 11:44
文章存档

2014年(14)

我的朋友

分类: LINUX

2014-07-07 00:41:08

使用autotools自动生成Makefile文件

autotools系列工具:
aclocal
autoscan
autoconf
autoheader
automake

操作步骤:
1、autoscan
它会在给定目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目录树中进行检查。
它会搜索源文件以寻找一般的移植性问题并创建一个文件“configure.scan”,该文件就是接下来autoconf
要用到的“configure.in”原型。
root@vm:/home/test# autoscan
root@vm:/home/test# ls
autoscan.log  configure.scan  hello.c  hello.c~
root@vm:/home/test# 

2、autoconf
重命名configure.scan为configure.in,configure.in 是autoconf 的脚本配置文件。
修改configure.in文件内容
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
#AC_PREREQ 宏声明本文件要求的autoconf 版本
AC_PREREQ([2.68])
#AC_INIT 宏用来定义软件的名称和版本等信息
#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT(hello, 1.0)
#AM_INIT_AUTOMAKE是automake 所必备的宏,使automake 自动生成
#makefile.in,也同前面一样,PACKAGE 是所要产生软件套件的名称,VERSION 是版本编号。
AM_INIT_AUTOMAKE(hello,1.0)
#AC_CONFIG_SRCDIR 宏用来检查所指定的源码文件是否存在,以及确定源码目录的有效性
AC_CONFIG_SRCDIR([hello.c])
#AC_CONFIG_HEADER 宏用于生成config.h 文件,以便autoheader 使用。
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_CONFIG_FILES([makefile])
AC_OUTPUT

接下来首先运行 aclocal,生成一个“aclocal.m4”文件,该文件主要处理本地的宏定义。
root@vm:/home/test# aclocal
root@vm:/home/test# ls
aclocal.m4  autom4te.cache  autoscan.log  configure.in  configure.in~  hello.c
root@vm:/home/test# 

再接着运行 autoconf,生成“configure”可执行文件。
root@vm:/home/test# autoconf
root@vm:/home/test# ls
aclocal.m4      autoscan.log  configure.in   hello.c
autom4te.cache  configure     configure.in~
root@vm:/home/test# 

3、autoheader
接着使用autoheader 命令,它负责生成config.h.in 文件。该工具通常会从“acconfig.h”文件中复制用户附
加的符号定义,因为这里没有附加符号定义,所以不需要创建“acconfig.h”文件。
root@vm:/home/test# autoheader
root@vm:/home/test# ls
aclocal.m4      autoscan.log  configure     configure.in~
autom4te.cache  config.h.in   configure.in  hello.c
root@vm:/home/test# 

4、automake
这一步是创建makefile 很重要的一步,automake 要用的脚本配置文件是makefile.am,用户需要自己创建相
应的文件。之后,automake 工具转换成makefile.in。
makefile.am文件内容如下:
#AUTOMAKE_OPTIONS 为设置automake 的选项。GNU 对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING 等,否则automake 执行时会报错。automake 提供了3种软件等级:foreign、gnu 和gnits,让用户选择采用,默认等级为gnu。在本示例中采用foreign等级,它只检测必须的文件。
AUTOMAKE_OPTIONS=foreign
#bin_PROGRAMS 定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。
bin_PROGRAMS= hello
#hello_SOURCES 定义“hello”这个执行程序所需要的原始文件。如果“hello”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用空格隔开。
hello_SOURCES= hello.c

接下来可以使用 automake 命令来生成“configure.in”文件,在这里使用选项“-a”(或者“—adding-missing”)
可以让automake 自动添加一些必需的脚本文件。
root@vm:/home/test# automake -a
configure.in:10: installing `./install-sh'
configure.in:10: installing `./missing'
makefile.am: installing `./depcomp'
root@vm:/home/test# ls
aclocal.m4      config.h.in   configure.in~  install-sh    makefile.in
autom4te.cache  configure     depcomp        makefile.am   missing
autoscan.log    configure.in  hello.c        makefile.am~
root@vm:/home/test# 

5、运行configure
在这一步中,通过运行自动配置设置文件configure,把makefile.in 变成了最终的makefile。
root@vm:/home/test# ./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... no
checking for mawk... mawk
checking whether make sets $(MAKE)... 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 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: executing depfiles commands
root@vm:/home/test# ls
aclocal.m4      config.h.in    configure.in   install-sh    makefile.in
autom4te.cache  config.log     configure.in~  makefile      missing
autoscan.log    config.status  depcomp        makefile.am   stamp-h1
config.h        configure      hello.c        makefile.am~
root@vm:/home/test# 

autotools 生成的makefile 除具有普通的编译功能外,还具有以下主要功能
1、make
键入make 默认执行“make all”命令,即目标体为all
2、make install
此时,会把该程序安装到系统目录中去
3、make clean
此时,make 会清除之前所编译的可执行文件及目标文件(object file, *.o)
4、make dist
此时,make 将程序和相关的文档打包为一个压缩文档以供发布
阅读(2337) | 评论(0) | 转发(0) |
1

上一篇:USB键盘驱动

下一篇:goahead移植

给主人留下些什么吧!~~