使用过开源C/C++项目的同学都知道,标准的编译过程已经变成简单的三部曲:./configure /make/make install,使用起来很方便,不像平时自己写代码,要手写一堆复杂的makefile,而且换个编译环境,makefile还需修改。因此,本文将介绍如何使用 autoconf 和

automake 两个工具来帮助我们自动地生成符合自由软件惯例的makefile了。

一.执行步骤

1.建立目录

在自己的工作目录下建立一个helloworld目录,用来存放helloworld程序及相关文件

$ mkdir helloworld

$ cd helloworld

2.编写helloworld.c

$ vim helloworld.c

#include <stdio.h>

int main (int argc, char **argv)
{
    printf("Hello Linux World!\n ");
    return 0;
}

$ ls

helloworld.c

3.运行autoscan 生成configure.scan文件

使用autoscan 命令来帮助我们根据目录下的源代码生成一个 configure.in的模板文件

$ autoscan

$ ls

autom4te.cache  autoscan.log  configure.scan  helloworld.c

4.该写成configure.in(后来用.ac 后缀)

将configure.scan该名为configure.in ,并且如下修改。

$ mv configure.scan configure.in

$ vim configure.in

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

AC_INIT(helloworld.c)
AM_INIT_AUTOMAKE(helloworld,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)

$ ls

autom4te.cache  autoscan.log  configure.in  helloworld.c

5.运行aclocal 生成aclocal.m4 文件

$ aclocal

$ ls

aclocal.m4  autom4te.cache  autoscan.log  configure.in  helloworld.c

6.运行autoconf 生成 configure 文件

$ autoconf

$ ls

aclocal.m4  autom4te.cache  autoscan.log  configure  configure.in  helloworld.c

7.新建 Makefile.am

$ vim Makefile.am

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c

8.运行automake 生成 Makefile.in

$ automake --add-missing

configure.in:5: installing `./install-sh'
configure.in:5: installing `./missing'
Makefile.am: installing `./depcomp'

$ ls

aclocal.m4      autoscan.log  configure.in  helloworld.c  Makefile.am  missing
autom4te.cache  configure     depcomp       install-sh    Makefile.in

9.执行 configure 生成 Makefile

$ ./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: executing depfiles commands

$ ls

aclocal.m4      autoscan.log  config.status  configure.in  helloworld.c  Makefile     Makefile.in
autom4te.cache  config.log    configure      depcomp       install-sh    Makefile.am  missing

10.使用make命令编译源文件

$ make

gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"helloworld\" -DVERSION=\"1.0\" -I.     -g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo -c -o helloworld.o helloworld.c
mv -f .deps/helloworld.Tpo .deps/helloworld.Po
gcc  -g -O2   -o helloworld helloworld.o

$ ls

aclocal.m4      autoscan.log  config.status  configure.in  helloworld    helloworld.o  Makefile     Makefile.in
autom4te.cache  config.log    configure      depcomp       helloworld.c  install-sh    Makefile.am  missing

11.运行 helloworld

$ ./helloworld

Hello Linux World!

$ make clean

test -z "helloworld" || rm -f helloworld
rm -f *.o

二.深入浅出

1. autoscan

autoscan 是用来扫描源代码目录,生成configure.scan 文件,该文件包含了由宏定义的系统配置的基本选项。

2. aclocal

aclocal 是一个perl 脚本程序,它根据 configure.in 文件的内容,自动生成 aclocal.m4 文件。

3. autoconf

autoconf 能根据configure.in 和aclocal.m4 产生configure 文件。configure 是一个脚本,它能设置源程序来适应各种不同的操作系统平台,并且根据不同的系统来产生合适的Makefile,从而可以使你的源代码能在不同的操作系统平台被编译出来。

configure.in 文件的内容是一些宏,这些宏经过autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数 的shell 脚本--

configure 。configure.in 文件中的宏的顺序并没有规定,但必须在所有宏的最前面和最后面分别加上AC_INIT 宏和AC_OUTPUT 宏。

在configure.in 中:

#    表示注释,这个宏后面的内容将被忽略

AC_INIT(FILE)    这个宏用来检查源代码所在的路径。

AM_INIT_AUTOMAKE(PACKAGE, VERSION)   这个宏描述了我们将要生成的软件包的名字及其版本号:PACKAGE 是软件包的名字,VERSION 是版本号。当你使用make dist 命令时,它会生成一个类似 helloworld-1.0.tar.gz 的软件发行包,其中就对应了软件包的名字和版本号。

AC_PROG_CC   这个宏检查系统所用的C编译器

AC_OUTPUT(FILE)   这个宏是我们要输出的Makefile 的名字

其实,我们使用automake 时,实际上还需要用到其他的一些宏,但我们可以用aclocal 来帮我们产生,即aclocal.m4 文件。

4.Makefile.am

Makefile.am 是用来生成 Makefile.in 的,需要手工书写。Makefile.am 定义了如下内容:

AUTOMAKE_OPTIONS   这个是automake 的选项。在执行automake 时,它会检查目录下是否存在标准GNU 软件包中应具备的各种文件,例如 AUTHORS、ChangeLog、NEWS等文件。我们将其设置成foreign 时,automake 会改用一般软件包的标准来检查。

bin_PROGRAMS    这个是指定我们所要产生的可执行文件的文件名。

helloworld_SOURCES   这个是指定产生“helloworld”时所需要的源代码。如果用到了多个源文件,可用空格符号将其隔开。如果在bin_PROGRAMS 中定义了多个可执行文件,则对应每个可执行文件都要定义相应的filename_SOURCES。

5.automake

automake 可以根据configure.in 和Makefile.am 来产生 Makefile.in 。

选项 --add-missing 的定义是”add missing standard files to package“,它会让automake 加入一个标准的软件包所必须的一些文件。

6.Makefile

make    根据Makefile 编译源代码,链接生成目标文件和可执行文件。

make clean   清除上次make 命令所产生的object 文件及可执行文件

make distclean   类似make clean,但同时也将configure 生成的文件全部删除掉,包括Makefile。

make install   将编译成功的可执行文件安装到系统目录中,一般为 /usr/local/bin 目录。

make dist   产生发布软件包文件(distribution package)。

make distcheck   生成发布软件包并对其进行测试检查,以确定发布包的正确性。这个操作将自动把压缩包文件解压,然后执行configure 命令,并且执行make ,来确认编译不出现错误,最后提示你的软件包已经准备好了,可以发布了。

三.过程图示

Linux下使用autoconf 和 automake 编译简单的HelloWorld的更多相关文章

  1. (转帖整理)Linux下的Autoconf和AutoMake(理论篇) 1

    在搜索网上资料过程中,这是感觉最简洁有效的一篇文章,特进行转帖记录,并根据情况对部分内容进行了修改.原帖传送门:Linux下的Autoconf和AutoMake 1.工具安装在开始使用autoconf ...

  2. CentOS下的Autoconf和AutoMake(完善篇) 3

    在<实践篇>之后,由于需求不断修正,所以这篇是针对<实践篇>的一些完善.(以后内容会不定期增加完善) 1.不想链接到math的动态库,想连接到静态库①使用命令ldd ./mys ...

  3. CentOS下的Autoconf和AutoMake(实践篇) 2

    阅读过<Linux下的Autoconf和AutoMake(理论篇)>之后,进入到实践环节.实验环境:CentOS release 6.7 (Final) x64 1.检查一下这4个工具是否 ...

  4. 在Linux下如何使用GCC编译程序、简单生成 静态库及动态库

      最近在编写的一个Apache  kafka 的C/C++客户端,,在看他写的 example中,他的编译是用librdkafka++.a和librdkafka.a    静态库编译的,,,而我们这 ...

  5. Linux下搭建 Cocos2d-x-2.1.4 编译环境

    [tonyfield 2013.09.04 ] 参考 Linux下搭建 Cocos2d-x-2.1.4 编译环境 导入 HelloCpp 例程 1. Java 入口 HelloCpp.java Hel ...

  6. 笔记-linux下Qt5.3.2 静态编译

    这里主要讲linux下的编译,windows下面比较简单 参考:http://qt-project.org/wiki/Building-Qt-5-from-Git 依赖 sudo apt-get in ...

  7. 用C写一个web服务器(三) Linux下用GCC进行项目编译

    .container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...

  8. Linux下LANMP集成环境中编译增加pdo_odbc模块

    linux版本为CentOs6.5,php集成环境为lanmp_v3.1,集成环境中默认的pdo扩展为:mysql, sqlite, sqlite2,现在有需求想链接微软的Access数据库,所以需要 ...

  9. 【Jenkins】linux下Jenkins集成ant进行编译并发送结果

    三个文章吧: 1 如何使用ant编译执行jmeter测试用例,并生成html报告 2 如何在Linux下搭建jenkins环境. 3 如何在Linux下搭建的jenkins中执行ant构建运行,并发送 ...

随机推荐

  1. linux信号机制与python信号量

    1.信号本质 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.在软件层次上是对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是进程间 ...

  2. yum下载rpm

    yum下载rpm yum update --downloadonly --downloaddir=/root/rpm_package/   python-pip

  3. Linux:实现Hadoop集群Master无密码登录(SSH)各个子节点

    以下所介绍的安装方式都是在线安装方式,如果你需要连网请参考:Linux:宿主机通过桥接方式连接的VMware内部Linux14.04虚拟机(静态IP)实现上网方案 环境: OS:Linux Ubunt ...

  4. Web3D编程入门总结——WebGL与Three.js基础介绍

    /*在这里对这段时间学习的3D编程知识做个总结,以备再次出发.计划分成“webgl与three.js基础介绍”.“面向对象的基础3D场景框架编写”.“模型导入与简单3D游戏编写”三个部分,其他零散知识 ...

  5. Python 迭代器&生成器

    1.内置参数     Built-in Functions     abs() dict() help() min() setattr() all() dir() hex() next() slice ...

  6. DruidDataSource配置属性列表

    DruidDataSource配置兼容DBCP,但个别配置的语意有所区别. 配置 缺省值 说明 name   配置这个属性的意义在于,如果存在多个数据源,监控的时候可以通过名字来区分开来.如果没有配置 ...

  7. mfc ui 3 swf

    引用:http://zhidao.baidu.com/question/420956871.html 作为一个自定义的资源导入,然后用LoadResource载入导入的资源.MAKEINTRESOUR ...

  8. php递归无限极分类实例

    无限级分类原理简介 无限分类看似"高大上",实际上原理是非常简单的 .无限分类不仅仅需要代码的巧妙性,也要依托数据库设计的合理性.要满足无限级分类,数据库需要有两个必须的字段,id ...

  9. 机器学习:异常检测算法Seasonal Hybrid ESD及R语言实现

    Twritters的异常检测算法(Anomaly Detection)做的比较好,Seasonal Hybrid ESD算法是先用STL把序列分解,考察残差项.假定这一项符合正态分布,然后就可以用Ge ...

  10. ScrollView和listview的冲突问题,关于宽度,和滑动

    只需要重新listview即可 package com.exmple.listscrow; import java.util.logging.LogManager; import android.co ...