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构建运行,并发送 ...
随机推荐
- 【译】在ASP.Net和IIS中删除不必要的HTTP响应头
引入 每次当浏览器向Web服务器发起一个请求的时,都会伴随着一些HTTP头的发送.而这些HTTP头是用于给Web服务器提供一些额外信息以便于处理请求.比如说吧.如果浏览器支持压缩功能,则浏览器会发送A ...
- Fiddler-009-AutoResponder 简单的 MOCK SERVER 应用实例
在我们日常的测试中经常需要测试特定的响应对应的客户端展示样式是否正确无误,实现测试方法一般有如下三种: 创建新的测试数据(工作量较大) 修改已有测试数据(例如修改对应的状态码,若是最终需要测试的按钮状 ...
- [转]Android View.onMeasure方法的理解
转自:http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html Android View.onMeasure方法的理解 View在屏幕上显示出来要先经过 ...
- 利用vs2013简单初探Python
最近无聊,某天无意在vs2013上发现了Python...... Python介绍:可以自己搜索一下. 接下来,准备工具: Win7搭建开发环境.需要准备Python.PTVS2013. 1.http ...
- 我的新发现:AVL树旋转的一个特性
关于AVL树旋转的代码网络上铺天盖地. 一些经典的实现方法如下: AVLTree SingleLeftRotation(AVLTree A) { AVLTree B = A->left; A-& ...
- Oracle配置本地网络服务名
Oracle安装完成后,可以使用客户端自带的的网络配置向导(Net Configuration Assistant)进行配置 1.启动Net Configuration Assistant.选择&qu ...
- MVC 的各个部分都有那些技术来实现?如何实现?
MVC 的各个部分都有那些技术来实现?如何实现? MVC 是 Model-View-Controller 的简写 "Model" 代表的是应用的业务逻辑(通过JavaBean,EJ ...
- 中国电信大亚DP607光猫破解,设置路由,wifi!关闭远程管理,改连接限制,SN码查询!
破解方法:以下为破解方法(一):1.进入192.168.1.12.用户名输入anonymous,密码不用,登陆3.输入网址192.168.1.1/backupsettings.conf4.用记事本打开 ...
- BJFU 1057
描述 斐波那契额数列,我们都知道.现在qingyezhu想求斐波那契的某项值对2的某次方的结果.你可以帮一下他吗?他好可怜哦!计算了N的N次方次都错了,也挨了ben大哥的N的N次方次的训了.我想你是个 ...
- python核心编程学习记录之条件和循环