使用过开源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. 关于echarts的疑问

    echarts-例子--待解决:模拟迁徙里面的 var planePath = 'path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.0 ...

  2. mesos+marathon+zookeeper的docker管理集群亲手搭建实例(环境Centos6.8)

    资源:3台centos6.8虚拟机 4cpu 8G内存 ip 10.19.54.111-113 1台centos6.8虚拟机2cpu 8G ip 10.19.53.55 1.System Requir ...

  3. Windows Server 2008 R2 DNS服务器迁移

    一.实验模拟环境: Zhuyu公司有一个DNS服务器,因DNS服务器比较老旧,准备迁移至新的DNS服务器上(DNS备份也可以这么操作). 旧DNS服务器: 主机名: test-zhuAD        ...

  4. Qt之自定义信号和槽函数

    自定义信号和槽函数: 1.类的声明和实现分别放在.h和.cpp文件中: 2.类声明包含Q_OBJECT宏: 3.信号只要声明不要设计其的实现函数 4.发射信号用emit关键字 5.自定义槽的实现与普通 ...

  5. [xampp]在Crunch Bang下安装xampp1.8.3

    1.下载linux下 的xampp安装包 xampp-linux-1.8.3-5-installer.run 2.终端下, 给执行权限 sudo chmod +x ./xampp-linux-1.8. ...

  6. Linux 的字符串截取方法(转)

    Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符. echo ${var#*//} ...

  7. mysql 配置文件 value

    在xml配置文件中配置数据库utl时,要使用&的转义字符也就是& 例如:<property name="url" value="jdbc:mysql ...

  8. 相似性度量(Similarity Measurement)与“距离”(Distance)

    在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间的“距离”(Distance).采用什么样的方法计算距离是很讲究,甚至关 ...

  9. 《R语言实战》读书笔记-- 第六章 基本图形

    首先写第二部分的前言. 第二部分用来介绍获取数据基本信息的图形技术和统计方法. 本章主要内容 条形图.箱型图.点图 饼图和扇形图 直方图和核密度图 分析数据第一步就是要观察它,用可视化的方式是最好的. ...

  10. 微信小程序注册app

    App() App() 函数用来注册一个小程序.接受一个 object 参数,其指定小程序的生命周期函数等. object参数说明 onLaunch   Function 生命周期函数--监听小程序初 ...