此篇为转载好文:原文链接:
linux下automake用法 - Rixu Blog (日需博客) - C++博客
http://www.cppblog.com/gezidan/archive/2011/08/08/152772.html

由于原文发布时间较早2012年,这里在原文的基础加点修正,以适合现在的automake版本。

作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便。一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Makefile就不那么容易了。

在本文中,将给大家介绍如何使用 autoconf和automake两个工具来帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常见的GNU程序一样,只要使用“./configure”,“make”,“make install”就可以把程序安装到Linux系统中去了。这将特别适合想做开放源代码软件的程序开发人员,又或如果你只是自己写些小的Toy程序,那么这个文章对你也会有很大的帮助。

编译一个简单的源文件main.c,需要自动生成一个makefile,以下是步骤:

第一步:


在/root/project/main目录下创建一个文件main.c,其内容如下:


#include <stdio.h>int main(int argc, char** argv)
{printf("Hello, Auto Makefile!\n");return 0;
}

此时状态如下:

[root@localhost main]# pwd

/root/project/main

[root@localhost main]# ls

main.c

[root@localhost main]#

第二步:


运行 autoscan , 自动创建两个文件: autoscan.log configure.scan

此时状态如下:

[root@localhost main]# autoscan

[root@localhost main]# ls

autoscan.log configure.scan main.c

[root@localhost main]#

第三步:


修改configure.scan的文件名为 configure.in
在新版本中,需要 修改configure.scan的文件名为 configure.ac
查看 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])
AC_CONFIG_SRCDIR([mian.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

解读以上的文件:


# -*- Autoconf -*-# Process this file with autoconf to produce a configure script.# AC_PREREQ:# 确保使用的是足够新的Autoconf版本。如果用于创建configure的Autoconf的版# 本比version 要早,就在标准错误输出打印一条错误消息并不会创建configure。AC_PREREQ([2.69])## 初始化,定义软件的基本信息,包括设置包的全称,版本号以及报告BUG时需要用的邮箱地址#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])## 用来侦测所指定的源码文件是否存在,来确定源码目录的有效性#AC_CONFIG_SRCDIR([mian.c])## 用于生成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.## 创建输出文件。在`configure.ac'的末尾调用本宏一次。#AC_CONFIG_FILES([Makefile])
AC_OUTPUT

修改动作:

1.修改AC_INIT里面的参数: AC_INIT(main,1.0, youremail@163.com)

2.添加宏AM_INIT_AUTOMAKE, 它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号。
2.新版本中 AM_INIT_AUTOMAKE 不需要指定参数 PACKAGE VERSION

3.在AC_OUTPUT后添加输出文件Makefile
3.新版本中不需要

修改后的结果:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.AC_PREREQ([2.69])
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([config.h])AC_INIT(main, 1.0)
AM_INIT_AUTOMAKE# 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

第四步:

运行 aclocal, 生成一个“aclocal.m4”文件和一个缓冲文件夹autom4te.cache,该文件主要处理本地的宏定义。

此时的状态是:

[root@localhost main]# aclocal

[root@localhost main]# ls

aclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ main.c

[root@localhost main]#

第五步:

运行 autoconf, 目的是生成 configure

此时的状态是:

[root@localhost main]# autoconf

[root@localhost main]# ls

aclocal.m4 autoscan.log configure.in main.c

autom4te.cache configure configure.in~

[root@localhost main]#

第六步:

运行 autoheader,它负责生成config.h.in文件。该工具通常会从“acconfig.h”文件中复制用户附加的符号定义,因此此处没有附加符号定义,所以不需要创建“acconfig.h”文件。

此时的状态是:

[root@localhost main]# autoheader

[root@localhost main]# ls

aclocal.m4 autoscan.log configure configure.in~

autom4te.cache config.h.in configure.in main.c

[root@localhost main]#

第七步:

下面即将运行 automake, 但在此之前应该做一下准备工作!

首先

创建一个 Makefile.am.这一步是创建Makefile很重要的一步,automake要用的脚本配置文件是 Makefile.am,用户需要自己创建相应的文件。之后,automake 工具转换成 Makefile.in。

这个Makefile.am的内容如下:


AUTOMAKE_OPTIONS=foreignbin_PROGRAMS=mainmain_SOURCES=main.c

下面对该脚本文件的对应项进行解释。

其中的AUTOMAKE_OPTIONS为设置automake的选项。由于GNU(在第1章中已经有所介绍)对自己发布的软件有严格的规范,比如必须附 带许可证声明文件COPYING等,否则automake执行时会报错。automake提供了三种软件等级:foreign、gnu和gnits,让用 户选择采用,默认等级为gnu。在本例使用foreign等级,它只检测必须的文件。

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

main_SOURCES定义“main”这个执行程序所需要的原始文件。如果”main”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原 始文件都列出来,并用空格隔开。例如:若目标体“main”需要“main.c”、“sunq.c”、“main.h”三个依赖文件,则定义 main_SOURCES=main.c sunq.c main.h。要注意的是,如果要定义多个执行文件,则对每个执行程序都要定义相应的file_SOURCES。

其次

使用automake对其生成“configure.in”文件,在这里使用选项“—adding-missing”可以让automake自动添加一些必需的脚本文件。

运行后的状态是:


[root@localhost main]# automake --add-missing

configure.in:8: installing `./missing’

configure.in:8: installing `./install-sh’

Makefile.am: installing `./depcomp’

[root@localhost main]# ls

aclocal.m4 config.h.in configure.in~ main.c Makefile.in

autom4te.cache configure depcomp Makefile.am missing

autoscan.log configure.in install-sh Makefile.am~

[root@localhost main]#


第八步

运行configure,在这一步中,通过运行自动配置设置文件configure,把Makefile.in变成了最终的Makefile。

运行的结果如下:


[root@localhost main]# ./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 for gcc… gcc

checking for C compiler default output file name… 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 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@localhost main]# ls

aclocal.m4 config.h.in configure.in main.c Makefile.in

autom4te.cache config.log configure.in~ Makefile missing

autoscan.log config.status depcomp Makefile.am stamp-h1

config.h configure install-sh Makefile.am~

[root@localhost main]#


第九步

运行 make,对配置文件Makefile进行测试一下

此时的状态如下:


[root@localhost main]# make

cd . && /bin/sh /root/project/main/missing --run aclocal-1.10

cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign

cd . && /bin/sh /root/project/main/missing --run autoconf

/bin/sh ./config.status --recheck

running CONFIG_SHELL=/bin/sh /bin/sh ./configure --no-create --no-recursion

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 for gcc… gcc

checking for C compiler default output file name… 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 ISO C89… none needed

checking for style of include used by make… GNU

checking dependency style of gcc… gcc3

configure: creating ./config.status

/bin/sh ./config.status

config.status: creating Makefile

config.status: creating config.h

config.status: config.h is unchanged

config.status: executing depfiles commands

cd . && /bin/sh /root/project/main/missing --run autoheader

rm -f stamp-h1

touch config.h.in

make all-am

make[1]: Entering directory `/root/project/main’

gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c

mv -f .deps/main.Tpo .deps/main.Po

gcc -g -O2 -o main main.o

cd . && /bin/sh ./config.status config.h

config.status: creating config.h

config.status: config.h is unchanged

make[1]: Leaving directory `/root/project/main’

[root@localhost main]# ls

aclocal.m4 autoscan.log config.h.in config.status configure.in depcomp main main.o Makefile.am Makefile.in stamp-h1

autom4te.cache config.h config.log configure configure.in~ install-sh main.c Makefile Makefile.am~ missing

[root@localhost main]#


第十步

运行生成的文件 main:


[root@localhost main]# ./main

Hello, Auto Makefile!

[root@localhost main]#


我用的是ubuntu

以上就是全文了.

个人心得:
要使用automake ,关键在于两个文件 configure.ac , Makefile.am 。
其中 configure.ac 可以使用 autoscan 生成的 configure.scan 上加以修改得到。
Makefile.am 需要自己添加编辑。用于生成makefile.in 。

原文链接:
linux下automake用法 - Rixu Blog (日需博客) - C++博客
http://www.cppblog.com/gezidan/archive/2011/08/08/152772.html

同类文章:
例解 autoconf 和 automake 生成 Makefile 文件
https://www.ibm.com/developerworks/cn/linux/l-makefile/index.html

linux下automake用法相关推荐

  1. Linux下wine用法

    Linux下wine用法 下面是wine的使用方法. 一.要在Linux下运行,wine是必不可少的,所以,请将wine安装到你的系统里. 二.将你的Windows所在的分区加载到某个目录.(本文假定 ...

  2. automake linux,Linux下automake软件编译与发布快速入门

    Linux下automake软件编译与发布快速入门 2008-04-22 eNet&Ciweek 进入编辑界面,输入内容如下: AUTOMAKE_OPTIONS=foreign bin_PRO ...

  3. linux下rdesktop用法

    我自己用的参数rdesktop -g workarea -D -r clipboard:PRIMARYCLIPBOARD -a 16 -x lan 192.168.1.2 rdesktop是Linux ...

  4. linux下grep用法

    linux下grep用法 [root@kylin10 ~]# nl test.txt1 I love china2 I love chinese3 My lover is flower 1.grep ...

  5. Linux下automake应用

    Linux下automake应用   linux 环境下,当项目工程很大的时候,编译的过程很复杂,所以需要使用 make 工具,自动进行编译安装,但是手写 makefile 文件比较复杂,所幸在 GN ...

  6. linux下DNW用法

    1.把linux下的DNW应用程序放到linux下的/bin目录下 sudo chmod +x  /bin/dnw sudo chmod +s  /bin/dnw 2.插入USB线,用USB连接开发板 ...

  7. Linux下gunicorn用法

    最近在linux下用到了gunicorn,做下记录 1.gunicorn是一个python Wsgi http server.Gunicorn使用prefork master-worker模型(在gu ...

  8. linux下snprintf用法,关于snprintf,_snprintf,_snprintf_s操作

    一.snprintf与snprintf_s的区别 众所周知,sprintf不能检查目标字符串的长度,可能造成众多安全问题,所以都会推荐使用snprintf. snprintf函数在C++11之前 并不 ...

  9. 在linux下nice用法,Linux下nice/renice命令小结

    1. nice命令 内核根据进程的nice值决定进程需要多少处理器时间. nice值的取值范围是是: -20到20. 一个具有-20 的 nice 值的进程有很高的优先级. 一个 nice 值为 20 ...

最新文章

  1. springMVC--(讲解3)数据处理
  2. 安装Debian 7.8 过程,以及该系统的配置过程
  3. 设计模式(1)--简单工厂模式、策略模式
  4. PS教程第二课:PS安装
  5. 在UE中自由绘制基本图元的几种方法
  6. php安装redis扩展报错,CentOS 67下php5+安装redis扩展组件
  7. qrcode-php生成二维码
  8. 如何和在桌面上添加计算机,怎么在电脑桌面上添加便签?
  9. [心情]一落千丈的反差
  10. 澳洲计算机博士怎么样,留学美国的一位计算机博士的感悟
  11. MFC MDI 多视图选项卡式风格
  12. Qt Https http 请求案例
  13. 微信企业号开发(第一篇)
  14. 学习笔记之MOOC《计算机程序设计C++》第5周编程作业
  15. 认证 (authentication) 和授权 (authorization)小记
  16. Android获取软键盘输入内容
  17. 借助ADB冻结与卸载Android系统应用(免ROOT)
  18. col-xs , col-sm , col-md , col-lg是什么意思?什么时候用?
  19. Eclipse Spring Boot STS安装及下载地址整理
  20. 开源贴片机OpenPnp使用体验

热门文章

  1. Charles 抓取安卓手机https包(安卓<华为>客户端应用,windows环境)
  2. python全栈开发实践入门_讲书3分钟丨《Python全栈开发实践入门》 -讲书人 谢瑛俊...
  3. 根据/proc/partitions获取插入的U盘设备名称
  4. 一个会烤吐司的AI,小扎用后表示非常不错!
  5. 惠普DL388 G7安装win Server 2003
  6. 【线程池】线程池创建的参数的作用new ThreadPoolExecutor()
  7. 【设计模式系列20】解释器模式原理及其在JDK和Spring源码中的体现
  8. 现实迷途 第三十四章 响亮一巴(下)
  9. 商务直播需要解决的难题有哪些
  10. HTML+CSS+JS作品展示(仿写携程网移动端首页②)