automake连载--Linux下使用automake入门
http://blog.csdn.net/shanzhizi/article/details/30246587
近来重要要总结一下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.in的内容:
------------------------------------------------
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
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
------------------------------------------------
解读以上的文件:
------------------------------------------------
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
# AC_PREREQ:
# 确保使用的是足够新的Autoconf版本。如果用于创建configure的Autoconf的版
# 本比version 要早,就在标准错误输出打印一条错误消息并不会创建configure。
AC_PREREQ(2.61)
#
# 初始化,定义软件的基本信息,包括设置包的全称,版本号以及报告BUG时需要用的邮箱地址
#
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
#
# 用来侦测所指定的源码文件是否存在,来确定源码目录的有效性
#
AC_CONFIG_SRCDIR([main.c])
#
# 用于生成config.h文件,以便autoheader使用
#
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.
#
# 创建输出文件。在`configure.in'的末尾调用本宏一次。
#
AC_OUTPUT
------------------------------------------------
修改动作:
1.修改AC_INIT里面的参数: AC_INIT(main,1.0, pgpxc@163.com)
2.添加宏AM_INIT_AUTOMAKE, 它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号。
3.在AC_OUTPUT后添加输出文件Makefile
修改后的结果:
------------------------------------------------
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT(main, 1.0, pgpxc@163.com)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(main,1.0)
# 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])
------------------------------------------------
第四步:
运行 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=foreign
bin_PROGRAMS=main
main_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!
总结:
使用automake自动生成makefile的过程主要有八个步骤:
1、建立好源文件以后到源文件所在目录
2、autoscan命令 将configure.scan文件修改为configure.in
修改configure.in文件中的内容:
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)修改为AC_INIT(main, 1.0, pgpxc@163.com)
在AC_CONFIG_HEADER([config.h])后面添加AM_INIT_AUTOMAKE(main,1.0)
在最后添加AC_OUTPUT([Makefile])
3、运行aclocal
4、运行autoconf
5、运行autoheader
6、创建Makefile.am文件,内容为
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main 如果有多个用空格分开
main_SOURCES=main.c 定义main所需源文件,多个可执行文件分别定义
7、运行automake --add-missing
8、运行./configure
9、运行make
在第六步中需要自己写Makefile.am文件,特别是其中的main_SOURCES需要把生成main所以来的文件都包含进来。并且那些间接依赖的文件也需要包含进来。比如说我有三个文件:main.cpp Add.cpp Add.h Num.h Num.cpp其中在main.cpp中包含了Add.h 在Add.cpp中包含了Num.h这样在完成main的依赖文件时就需要包含以上所有的问个文件main.cpp Add.cpp Add.h Num.h Num.cpp才可以。
automake连载--Linux下使用automake入门的更多相关文章
- automake连载--Linux下使用autoconfig automake进阶
http://blog.csdn.net/shanzhizi/article/details/30247325 前言: 这次task,我大概用了4天的时间去完成.四天的时间内,我不停地去查 ...
- Linux下pwn从入门到放弃
Linux下pwn从入门到放弃 0x0 简介 pwn,在安全领域中指的是通过二进制/系统调用等方式获得目标主机的shell. 虽然web系统在互联网中占有比较大的分量,但是随着移动端,ioT的逐渐流行 ...
- Linux下串口编程入门
简介: Linux操作系统从一开始就对串行口提供了很好的支持,本文就Linux下的串行口通讯编程进行简单的介绍. 串口简介 串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用.常用 ...
- Linux下C编程入门(7)
Linux下项目同步工具介绍git和github 一.远程仓库工具github 1. 一.本地操作工具git 1.
- Linux下C编程入门(1)
Linux系统的介绍(以下以Manjaro最新版为例子): 一.系统的安装: 1.可以直接使用U盘做一个live usb的启动盘,在bios中设置从U盘启动即可拥有linux系统,如果是新式bios需 ...
- linux下使用automake工具自动生成makefile文件
linux环境下,当项目工程很大的时候,编译的过程很复杂,所以需要使用make工具,自动进行编译安装,但是手写makefile文件比较复杂,所幸在GNU的计划中,设计出了一种叫做Autoconf/Au ...
- Linux下使用automake、autoconf生成configure文件
一.生成configure过程中各文件之间的关系图 二.详细介绍 autoscan: 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是con ...
- Linux下使用GDAL进行开发(automake使用)
首先写三个源代码文件,分别是GDALTest.cpp.Fun.cpp和Fun.h,将这三个存放在一个叫GDALTest的文件夹中,然后打开终端,切换到该目录,如下图所示(注:这个图是最后截图的,所以文 ...
- linux下automake用法
linux下automake用法 2017年02月06日 09:21:14 阅读数:3684 标签: makemakefilegnulinux 作为Linux下的程序开发人员,大家一定都遇到过Ma ...
随机推荐
- JSON字符串转对象
JSON(JavaScript Object Notation) JavaScript对象标记法:JSON是与JavaScript高度契合的:JSON 语法: --数组(Array)用" ...
- JDK Windows安装
进入至JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 点击下载后,会进入下载列表 点击下载后,就等 ...
- java原理—反射机制
http://www.cnblogs.com/forlina/archive/2011/06/21/2085849.html 一.什么是反射:反射的概念是由Smith在1982年首次提出的,主要是指程 ...
- 第1天:Ansible安装部署
Ansible介绍 Ansible是一个简单的自动化引擎,可完成配置管理.应用部署.服务编排以及各种IT需求.它是一款使用Python语言开发实现的开源软件,其依赖Jinjia2.paramiko和P ...
- Python开发基础-Day3-列表、元组和字典
列表 列表定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: 1.可存放多个值 2.可修改指定索引位置对应的值,可变 3.按照从左到右的顺序定义列表元素,下标从0开始顺序 ...
- phongap开发中安卓平台上如何调用第三方播放器来播放HLS视频
前文曾经讲了关于在安卓平台上利用phonegap开发播放HLS的解决方案,其实最好的方案就是自己针对HLS视频开发自己的播放器,但是开发播放器是一个浩大的工程,必须对原生安卓开发非常熟悉,并且对视频播 ...
- 【位运算】【BFS】移动玩具
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2246 Solved: 1246[Submit][Stat ...
- 【容斥原理】【分解质因数】poj1091 跳蚤
题意转化为求一个线性组合a1*x1+a2*x2+...+an*xn+m*xn+1=1在什么时候可以有解.(ai在1~m的范围内任取) 易得当且仅当gcd(a1,a2,...,an)=1时可能有解. 然 ...
- 微信小程序 Session 失效
微信小程序 Session 失效 微信小程序,前端请求后端,中间多了个微信服务器,所以请求的流程就是 页面--微信服务器--目标服务器 这就导致了一个问题 session 每次请求都是一个新的会话 解 ...
- 一步一步搭建springCloud
一.spring cloud简介Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...