建议阅读GNU Automake 官方文档,系统学习 automake 的用法。

在这里

---------------------------------------------------------------------------------------------(以下内容仅供参考)

一般而言,对于小项目或玩具程序,手动编写 Makefile 即可。但对于大型项目,手动编写维护 Makefile 成为一件费时费力的无聊工作。

本文介绍 autotools 工具集自动生成符合 Linux 规范的 Makefile 文件。

如果读者没有安装 autotools 工具集,安装命令如下,

$ sudo apt-get install automake

安装完成之后,会有如下工具可用,

aclocal
     autoscan
     autoconf
     autoheader
     automake

一般大型项目,代码组织结构分为两种,一种是所有文件都在同一个目录下的 flat 结构,另一种是按层次组织的多文件夹形式。先来看第一种,

flat 结构的项目使用 autotools 工具集

本篇测试代码如下,

入口代码  int_arithmetic.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sum.h"
#include "sub.h"
#include "mul.h"
#include "div.h" int main()
{
printf("======== < Integer Arithmethic > ========\n");
int x, y;
printf("Enter two integer: ");
scanf("%d%d", &x, &y); int sm = sum(x, y);
printf("sum is: %d\n", sm);
int sb = sub(x, y);
printf("sub is: %d\n", sb);
int ml = mul(x, y);
printf("mul is: %d\n", ml);
int dv = divide(x, y);
printf("div is: %d\n", dv); return ;
}

辅助代码,头文件,

sum.h

#ifndef SUM_H_
#define SUM_H_ int sum(int x, int y); #endif

sub.h

#ifndef SUB_H_
#define SUB_H_ int sub(int x, int y); #endif

mul.h

#ifndef MUL_H_
#define MUL_H_ int mul(int x, int y); #endif

div.h

#ifndef DIV_H_
#define DIV_H_ int divide(int x, int y); #endif

辅助代码,实现文件,

sum.c

#include "sum.h"

int sum(int x, int y)
{
return x + y;
}

sub.c

#include "sub.h"

int sub(int x, int y)
{
return x - y;
}

mul.c

#include "mul.h"

int mul(int x, int y)
{
return x * y;
}

div.c

#include "div.h"
#include <stdio.h> int divide(int x, int y)
{
if(x % y != )
printf("\nWarning: Integer Division May Have Accuracy Loss.\n"); return x / y;
}

1) 在项目目录下,运行 autoscan 命令,生成 configure.scan 文件,内容如下,

#                                               -*- 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([int_arithmetic.c])
AC_CONFIG_HEADERS([config.h]) # Checks for programs.
AC_PROG_CC # Checks for libraries. # Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions.
AC_OUTPUT

重命名 configure.scan 为 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_INIT(int_arithmetic, 0.1, ggao@micron.com)
AM_INIT_AUTOMAKE(int_arithmetic, 0.1)
AC_CONFIG_SRCDIR([int_arithmetic.c])
AC_CONFIG_HEADERS([config.h]) # Checks for programs.
AC_PROG_CC # Checks for libraries. # Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions.
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

上述 configure.ac 中宏定义意义如下,

AC_PREREQ         : 声明 autoconf 的版本号
AC_INIT : 声明软件名称,版本号及 bug report 地址
AM_INIT_AUTOMAKE : automake 需要的信息,参数为软件名和版本号
AC_CONFIG_SRCDIR : autoscan 侦测的源文件名,用来确定目录的有效性
AC_CONFIG_HEADERS : autoscan 定义要生成的头文件,后续 autoheader 要使用
AC_PROG_CC : 指定编译器,默认为 gcc
AC_CHECK_HEADERS : autoscan 侦测到的头文件
AC_CONFIG_FILES : 指定生成 Makefile,如果是多目录结构,可指定生成多个Makefile,以空格分隔,例如,AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT : autoscan 输出

2) 运行 aclocal,根据 configure.ac 生成 aclocal.m4 文件,该文件主要处理各种宏定义

3) 运行 autoconf,将 configure.ac 中的宏展开,生成 configure 脚本,这过程中可能会用到 aclocal.m4

4) 执行 autoheader,生成 config.h.in 文件,该命令通常会从 "acconfig.h” 文件中复制用户附加的符号定义。该例子中没有附加的符号定义, 所以不需要创建 "acconfig.h” 文件

5) 创建 Makefile.am 文件,automake工具会根据 configure.in 中的参量把 Makefile.am 转换成 Makefile.in 文件,最终通过 Makefile.in 生成 Makefile

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=int_arithmetic
int_arithmetic_SOURCES=int_arithmetic.c sum.c sub.c mul.c div.c
include_HEADERS=sum.h sub.h mul.h div.h

对上述 makefile.am 中各标签的解释,

AUTOMAKE_OPTIONS       : 由于 GNU 对自己发布的软件有严格的规范, 比如必须附带许可证声明文件 COPYING 等,否则 automake 执行时会报错。 
                automake 提供了3中软件等级: foreign, gnu, gnits, 默认级别是gnu, 在本例中,使用 foreign 等级,它只检测必须的文件。
bin_PROGRAMS : 要生成的可执行文件名称,如果要生成多个可执行文件,用空格隔开。
int_arithmetic_SOURCES : 可执行文件依赖的所有源文件。

6) 手动添加必要的文件 NEWS,README,AUTHORS,ChangeLog

7) 执行 automake --add-missing ,该命令生成 Makefile.in 文件。使用选项 "--add-missing" 可以让 automake 自动添加一些必需的脚本文件。

8) 执行 ./configure 生成 Makefile

====>>> 至此 Makefile 生成完毕。

如果要继续安装,

9) $ make

10) $ sudo make install  即可将可执行文件安装在 /usr/local/bin/ 目录下,以后就可以直接使用啦

11)  $ sudo make uninstall 即可将安装的可执行文件从 /usr/local/bin 目录下移除

如果要发布你的软件,

12) $ make dist  即可打包生成 xxx-version.tar.gz 文件

如果要清理中间文件,

13) make clean

14) make distclean

层次结构的项目使用 autotools 工具集

当前项目层次结构如下图,

主入口函数 int_arithmetic.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "include/sum.h"
#include "include/sub.h"
#include "include/mul.h"
#include "include/div.h" int main()
{
printf("======== < Integer Arithmethic > ========\n");
int x, y;
printf("Enter two integer: ");
scanf("%d%d", &x, &y); int sm = sum(x, y);
printf("sum is: %d\n", sm);
int sb = sub(x, y);
printf("sub is: %d\n", sb);
int ml = mul(x, y);
printf("mul is: %d\n", ml);
int dv = divide(x, y);
printf("div is: %d\n", dv); return ;
}

头文件,

sum.h

#ifndef SUM_H_
#define SUM_H_ int sum(int x, int y); #endif

sub.h

#ifndef SUB_H_
#define SUB_H_ int sub(int x, int y); #endif

mul.h

#ifndef MUL_H_
#define MUL_H_ int mul(int x, int y); #endif

div.h

#ifndef DIV_H_
#define DIV_H_ int divide(int x, int y); #endif

实现文件,

sum.c

#include "../include/sum.h"

int sum(int x, int y)
{
return x + y;
}

sub.c

#include "../include/sub.h"

int sub(int x, int y)
{
return x - y;
}

mul.c

#include "../include/mul.h"

int mul(int x, int y)
{
return x * y;
}

div.c

#include "../include/div.h"
#include <stdio.h> int divide(int x, int y)
{
if(x % y != )
printf("\nWarning: Integer Division May Have Accuracy Loss.\n"); return x / y;
}

1) 在项目顶层目录,建立文件 Makefile.am, 内容如下,

AUTOMAKE_OPTIONS=foreign     		   # 软件等级
SUBDIRS=src    # 先扫描子目录
bin_PROGRAMS=int_arithmetic    # 软件生成后的可执行文件名称
int_arithmetic_SOURCES=int_arithmetic.c # 当前目录源文件
int_arithmetic_LDADD=src/libsrc.a          # 静态连接方式,连接 src 下生成的 libsrc.a 文件
#LIBS = -l xxx -l xxx  # 添加必要的库

在 src 目录,建立文件 Makefile.am,内容如下,

noinst_LIBRARIES=libsrc.a                  # 生成的静态库文件名称,noinst加上之后是只编译,不安装到系统中
libsrc_a_SOURCES=sum.c sub.c mul.c div.c  # 这个静态库文件需要用到的依赖
include_HEADERS=../include/sum.h ../include/sub.h ../include/mul.h ../include/div.h # 导入需要依赖的头文件

2) 执行 autoscan 生成 configure.scan 文件, 如下,

#                                               -*- 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([int_arithmetic.c])
AC_CONFIG_HEADERS([config.h]) # Checks for programs.
AC_PROG_CC # Checks for libraries. # Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_CONFIG_FILES([Makefile
src/Makefile])
AC_OUTPUT

重命名 configure.scan 为 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_INIT(int_arithmetic, 0.1, ggao@micron.com)
AM_INIT_AUTOMAKE(int_arithmetic, 0.1)
# Generate static lib
AC_PROG_RANLIB AC_CONFIG_SRCDIR([int_arithmetic.c])
AC_CONFIG_HEADERS([config.h]) # Checks for programs.
AC_PROG_CC # Checks for libraries. # Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_CONFIG_FILES([Makefile
src/Makefile])
AC_OUTPUT

3) 执行 aclocal

4) 运行 autoconf

5) 运行 autoheader

6) 手动添加必要的文件 NEWS,README,AUTHORS,ChangeLog

7) 执行 automake --add-missing

8) 执行 ./configure 生存 Makefile

====>>> 至此 Makefile 生成完毕。

如果要继续安装,

9) $ make

10) $ sudo make install  即可将可执行文件安装在 /usr/local/bin/ 目录下,以后就可以直接使用啦

11)  $ sudo make uninstall 即可将安装的可执行文件从 /usr/local/bin 目录下移除

如果要发布你的软件,

12) $ make dist  即可打包生成 xxx-version.tar.gz 文件

如果要清理中间文件,

13) make clean

14) make distclean

====>>> 感谢原创作者的分享  http://blog.csdn.net/initphp/article/details/43705765

完。

automake - 使用 autotools 工具集的更多相关文章

  1. Open Source 开发工具集

    Open Source 开发工具集 转自:http://www.linuxforum.net原作者:gogoliu(Pooh-Bah) 编辑器: vi:老牌编辑器,在各个unix和unix-like平 ...

  2. (6)autotools工具的使用

       autotools是专门用来生成Makefile的一系列工具,包括autoscan.aclocal.autoheader.autoconf.automake等.     (1)autotools ...

  3. 使用autotools工具用configure、make、make install编译安装linux工程的详细步骤

    使用autotools工具用configure.make.make install编译安装linux工程的详细步骤 转载tmxkwzy 最后发布于2016-11-24 10:20:15 阅读数 324 ...

  4. 价值1400美元的CEH(道德黑客)认证培训课程长啥样?(3)工具集

    美元的CEH(道德黑客)认证培训课程长啥样?(3)工具集 这是我收到的CEH官方发来的邮件,参加CEH认证培训原价为1424.25刀,可以给我便宜到1282刀.只有一个感觉,心在流血.站在这价值120 ...

  5. Apache Commons 工具集

    一.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是 ...

  6. kali linux 渗透测试视频教程 第五课 社会工程学工具集

    第五课 社会工程学工具集 文/玄魂 教程地址:http://edu.51cto.com/course/course_id-1887.html   目录 第五课社会工程学工具集 SET SET的社会工程 ...

  7. 如何判断平台工具集去做条件编译(VC++目录、预处理器定义、$(PlatformToolsetVersion))

    作者:zyl910 从VS2010开始,提供了一个平台工作集(Platform ToolSet)选项用于配制vc编译版本.到了VS2012,更是因为默认平台工具集不支持WindowsXP,导致经常需要 ...

  8. VS平台工具集版本

    参考:http://blog.csdn.net/hillseas/article/details/47373313 VS从2010之后开始支持使用之前的版本进行编译,可以在工程属性->常规中进行 ...

  9. Android虚拟环境的工具集Genymotion完整安装教程

    Genymotion提供Android虚拟环境的工具集.相信很多Android开发者一定受够了速度慢.体验差效率及其地下的官方模拟器了.如果你没有物理机器,又不想忍受官方模拟器的折磨,Genymoti ...

随机推荐

  1. 大事记 - 安卓微信浏览器 video 标签层级过高

    // 为什么叫<大事记>? // 以前总有面试官问这样一个问题:“你在项目中遇到过最头疼的问题是什么,是怎么解决的?” // 当时总觉得,已解决的问题都算不上头疼,所以回答总是不尽人意. ...

  2. IO学习二(节点流)

    1.流的分类 按照数据流向的不同:输入流和输出流 按照处理数据的单位不同:字节流((非文本文件)视频.音频.图像).字符流(文本文件) 按照角色的不同:节点流和处理流 2.IO体系 抽象基类 节点流 ...

  3. JavaScript易错知识点整理[转]

    前言 本文是我学习JavaScript过程中收集与整理的一些易错知识点,将分别从变量作用域,类型比较,this指向,函数参数,闭包问题及对象拷贝与赋值这6个方面进行由浅入深的介绍和讲解,其中也涉及了一 ...

  4. web框架的本质

    一 web框架的本质及自定义web框架 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端,基于请求做出响应,客户都先请求,服务端做出对应的响 ...

  5. tsung HTTP协议统计报告分析

    tsung HTTP协议统计报告分析 by:授客 QQ:1033553122 1.   Main Static l  higest 10sec mean: 基于每10s的统计,最大耗时 l  lowe ...

  6. IDEA基于Maven Struts2搭建配置及示例

    1.web.xml加载struts框架即过滤器,要注意struts版本不同过滤器配置也不同. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems ...

  7. hdu-2018题(母牛问题)

    HDU-2018题/*有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛?Input输入数据由多个测试实例组成,每个测试实 ...

  8. linux 下svn操作

    * 前言: linux下的svn相比于gitlab,配置要求第一点:gitlab需要4G的内存,如果使用swap+内存的替代方案,理论上是可行的,但是实际操作中各种坑:     所以,由于条件限制,使 ...

  9. 洗礼灵魂,修炼python(86)--全栈项目实战篇(12)—— 利用socket实现文件传输/并发式聊天

    由于本篇博文的项目都很简单,所以本次开个特例,本次解析两个项目,但是都很简单的 项目一:用socket实现文件传输 本项目很简单,作为小项目的预热的,前面刚学完socket,这里马上又利用socket ...

  10. EOS智能合约开发(一):EOS环境搭建和启动节点

    EOS和以太坊很像,EOS很明确的说明它就是一个区块链的操作系统,BM在博客中也是说过的. 可以这样比喻,EOS就相当于内置激励系统的Windows/Linux/MacOS,这是它的一个定位. 包括以 ...