Linux下使用autoconf 和 automake 编译简单的HelloWorld
使用过开源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的更多相关文章
- (转帖整理)Linux下的Autoconf和AutoMake(理论篇) 1
在搜索网上资料过程中,这是感觉最简洁有效的一篇文章,特进行转帖记录,并根据情况对部分内容进行了修改.原帖传送门:Linux下的Autoconf和AutoMake 1.工具安装在开始使用autoconf ...
- CentOS下的Autoconf和AutoMake(完善篇) 3
在<实践篇>之后,由于需求不断修正,所以这篇是针对<实践篇>的一些完善.(以后内容会不定期增加完善) 1.不想链接到math的动态库,想连接到静态库①使用命令ldd ./mys ...
- CentOS下的Autoconf和AutoMake(实践篇) 2
阅读过<Linux下的Autoconf和AutoMake(理论篇)>之后,进入到实践环节.实验环境:CentOS release 6.7 (Final) x64 1.检查一下这4个工具是否 ...
- 在Linux下如何使用GCC编译程序、简单生成 静态库及动态库
最近在编写的一个Apache kafka 的C/C++客户端,,在看他写的 example中,他的编译是用librdkafka++.a和librdkafka.a 静态库编译的,,,而我们这 ...
- Linux下搭建 Cocos2d-x-2.1.4 编译环境
[tonyfield 2013.09.04 ] 参考 Linux下搭建 Cocos2d-x-2.1.4 编译环境 导入 HelloCpp 例程 1. Java 入口 HelloCpp.java Hel ...
- 笔记-linux下Qt5.3.2 静态编译
这里主要讲linux下的编译,windows下面比较简单 参考:http://qt-project.org/wiki/Building-Qt-5-from-Git 依赖 sudo apt-get in ...
- 用C写一个web服务器(三) Linux下用GCC进行项目编译
.container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...
- Linux下LANMP集成环境中编译增加pdo_odbc模块
linux版本为CentOs6.5,php集成环境为lanmp_v3.1,集成环境中默认的pdo扩展为:mysql, sqlite, sqlite2,现在有需求想链接微软的Access数据库,所以需要 ...
- 【Jenkins】linux下Jenkins集成ant进行编译并发送结果
三个文章吧: 1 如何使用ant编译执行jmeter测试用例,并生成html报告 2 如何在Linux下搭建jenkins环境. 3 如何在Linux下搭建的jenkins中执行ant构建运行,并发送 ...
随机推荐
- 引入RequireJS(一)
例子 http://www.utlcenter.com/user/index.aspx 一.文件引用 <script src="Js/require.js" defer as ...
- Android studio -VSN 使用笔记
1.Android studio 安装 中文组官网:http://www.android-studio.org/ 常见问题参考: 分支冲突合并 http://blog.csdn.net/tearsmo ...
- ECSHOP 用户中心 我的订单前台显视订单每张商品图片及收货人
先在网站根目录打开文件 includes/lib_transaction.php 查找代码 $sql = "SELECT order_id, order_sn, order_status, ...
- Win8.1密钥
Win8.1 在线永久激活密钥一枚! 78BHN-M3KRH-PCP9W-HQJYR-Q9KHD [剩余次数:7K多+] 继续增加 [Key]:HPCJW-VGYW4-CR7W2-JG6Q7-K4Q ...
- Android 自定义RecyclerView 实现真正的Gallery效果
http://blog.csdn.net/lmj623565791/article/details/38173061
- .NET学习记录2
前面回忆了一些C#基础语法,简单整理了一下笔记,要想深入研究的话,那就得找一本比较好的书了.接下来继续回忆C#语法知识. 方法 功能:用来复用代码的.当我们在一个程序 中反复的写了同样的代码,那么一般 ...
- Unity操作
聚焦到游戏物体: Hierarchy界面选中需要聚焦的物体,双击或者使用快捷键“F”: 在Scene面板中选中物体,使用快捷键“F” 放大缩小物体: alt+鼠标右键:鼠标滑轮 从各个角度观察 ...
- 161221、bootstrap table 实例
bootstrap table 封装了一套完善的数据表格组件,把下面的代码复制一下估计你需要的基本功能都有了,没有的再看看手册对比着我给的实例也能很快的熟悉了 客户端 <!DOCTYPE htm ...
- C#对多个集合和数组的操作(合并,去重,判断)
在开发过程中.数组和集合的处理是最让我们担心.一般会用for or foreach 来处理一些操作.这里介绍一些常用的集合跟数组的操作函数. 首先举例2个集合A,B. List<int> ...
- 如何利用php array_multisort函数 对数据库排序
数据库中有4个字段分别是id,volume,edition,name. 要求对查询结果按照volume+edition从大到小排序.下面将一下array_multisort函数array_multis ...