Linux 应用开发 - Autoconf 打包软件
2017-07-25 15:57
387 查看
Autoconf 简介
Autoconf 是一种用于生成shell脚本的工具,可自动配置软件源代码包,以适应多种类型的类似
Posix的系统。你可以简单地把它看为打包源码的工具,例如你在 GNU 官网下载的那些
xxx.tar.gz格式的软件包,我们使用这个工具最终目的就是将源代码打成一个包来提供给别人使用。本次就跟大家分享下如何打包一个基本的
Hello World给别人使用,掌握这个步骤以后就可以类比来打包别的软件包了,首先我们来看看 Autoconf 的基本打包流程。
Autoconf 打包流程
我们使用Autoconf打包一个软件包主要依靠下面这张图:
这张图中主要使用了 5 个与 autoconf 相关的工具,我们分别来了解即可,不需要深入学习。
1. autoscan
autoscan用来扫描源代码目录并生成
configure.scan文件,这个文件包含了系统配置的基本选项,里面都是一些宏定义,在使用的时候需要将这个文件改名为
configure.ac,并修改相关的配置,我们后面在实际例子中介绍。
2. aclocal
aclocal是一个
prel脚本程序,
aclocal根据
configure.ac文件的内容自动生成
aclocal.m4文件,这个文件内容是
configure.ac中的宏展开。
3. autoconf
autoconf用来产生
configure文件,这个文件就是我们在手动编译一个软件是要做的第一步:
./configure。
4. autoheader
autoheader自动生成相关的文件,这个功能在源码需要头文件时才使用。
5. automake
automake可以将
Makefile.am生成
Makfile.in,但是
Makefil.am需要我们手动书写。
了解了这 5 个工具,下面我们就来打包一个实际的 Hello World !
实践:Autoconf 打包 Hello World
我们来以一个实际打包hello.c的例子来介绍 Autoconf 的基本用法。
安装 autoconf
首先确定你的系统有没有安装autoconf,在命令行键入
autoconf,如果提示没有安装,则需要先安装:
sudo apt-get install autoconf
编辑 hello.c
我们编写一个hello.c作为测试:
#include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }
下面就开始正式打包流程。
1. autoscan
从上面的图中可以看到,第一步我们需要使用autoscan来生成
configure.scan文件:
autoscan
执行的结果除了
hello.c还有另外 2 个文件:
2. 修改 configure.ac
之后我们还需要将configure.scan改名为
configure.ac,并修改以下的 3 点内容:
# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) # 1. 修改:可执行文件名称,版本号,bug 邮箱 AC_INIT(hello, 1.0, chenghjy@gmail.com) AC_CONFIG_SRCDIR([hello.c]) AC_CONFIG_HEADERS([config.h]) # 2. 我们后面使用 automake, 所以需要加上这个配置 AM_INIT_AUTOMAKE # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. # 3. 输出文件指定为 Makefile AC_OUTPUT(Makefile)
3. aclocal
从上图可以看到我们生成configure需要
[aclocal.m4]文件,我们使用
aclocal来生成:
aclocal
结果生成了
aclocal.m4文件,如下图所示:
4. autoconf
现在可以使用autoconf来生成
configure啦:
autoconf
结果如下,生成了
configure:
我们直接执行
./configure看看是否能够配置成功:
./configure # 结果 configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."
但是结果提示我们缺少一些 shell 脚本,因此我们还需要进行后面的配置。
5. autoheader
如果在configure.ac中需要头文件,则需要进行这一步,否则不需要,我们配置了所以需要:
autoheader
6. 编写 Makefile.am
上图中,我们如果要使用automake来生成
Makefile.in,则还需要
Makefile.am文件,但是这个文件需要我们手动编写,具体如何编写,可以查看 automake 官方文档,在下面这个图片位置:
因为我们的
hello程序很简单,不需要依赖其他的库,所以这里只需要写 2 行:
bin_PROGRAMS = hello hello_SOURCES = hello.c
7. automake
上面写完了Makefile.am文件,现在就可以使用
automake来生成
Makefile.in啦:
automake # 结果 configure.ac:10: error: required file './compile' not found configure.ac:10: 'automake --add-missing' can install 'compile' configure.ac:8: error: required file './install-sh' not found configure.ac:8: 'automake --add-missing' can install 'install-sh' configure.ac:8: error: required file './missing' not found configure.ac:8: 'automake --add-missing' can install 'missing' Makefile.am: error: required file './INSTALL' not found Makefile.am: 'automake --add-missing' can install 'INSTALL' Makefile.am: error: required file './NEWS' not found Makefile.am: error: required file './README' not found Makefile.am: error: required file './AUTHORS' not found Makefile.am: error: required file './ChangeLog' not found Makefile.am: error: required file './COPYING' not found Makefile.am: 'automake --add-missing' can install 'COPYING' Makefile.am: error: required file './depcomp' not found Makefile.am: 'automake --add-missing' can install 'depcomp'
但是出现了一些错误,从提示信息中发现可以使用
automake --add-missing:
automake --add-missing # 结果 configure.ac:10: installing './compile' configure.ac:8: installing './install-sh' configure.ac:8: installing './missing' Makefile.am: installing './INSTALL' Makefile.am: error: required file './NEWS' not found Makefile.am: error: required file './README' not found Makefile.am: error: required file './AUTHORS' not found Makefile.am: error: required file './ChangeLog' not found Makefile.am: installing './COPYING' using GNU General Public License v3 file Makefile.am: Consider adding the COPYING file to the version control system Makefile.am: for your code, to avoid questions about which license your project uses Makefile.am: installing './depcomp'
还是有错误,但是这个错误很好解决,它提示
NEWS,
README,
AUTHORS,
ChangeLog这 4 个文件没有找到,其实这 4 个文件是一个正规软件发布的时候一般都带有的,我们这里新建这 4 个文件即可,就不写内容了:
touch NEWS README AUTHORS ChangeLog # 再次执行,没有错误信息 automake --add-missing
结果生成了
Makefile.in,这也就是我们配置的最终结果啦:
可以看到目前我们的文件已经很多了,是不是有些正式发布的软件的样子了,其实正式的软件里面的大部分配置文件也是自动生成的。不过我们还差最后一步:打包。
8. 打包
我们使用make dist命令直接打包:
# 先要配置得到 Makefile ./configure # 打包 make dist # ls 结果 hello-1.0.tar.gz
测试 hello-1.0.tar.gz
下面我们就来测试我们打包的hello程序是否可用,我们从配置到最后的卸载一共分为 6 步:
# 1. 配置:./configure ./configure # 2. 编译:make make # 3. 安装:install sudo make install # 4. 运行 hello # 5. 结果,打印 Hello World! 说明成功啦! Hello World! # 6. 卸载:uninstall sudo make uninstall
一路绿灯,说明我们打的包没有问题,那么这个实验也就到此结束了。
结语
本次我们学习如何在 Linux 下通过命令行和 Autoconf 来打包一个程序,我们也知道了从网上下载下的软件包中的那么多文件是怎么来的了,并且我们也实际练习了如何打包一个Hello World!给别人使用了。通过这个例子,希望你能够掌握
Autoconf基本的用法,在以后遇到问题的时候,还望你能主动去 Autoconf 官网 查找资料,做一个主动学习的人。
最后,感谢你的阅读,我们下次再见 :)
推荐关注我的微信公众号 CDeveloper,坚持技术原创,只说真话!
相关文章推荐
- MapReduce应用开发 倒排索引 以及打包在linux下运行
- Android应用开发之软件打包与发布,生成私钥签名你的软件
- 用Gtk+开发Linux上的GUI应用软件
- Manual | BSD手册| Linux手册 | 数据库手册 | 编程开发手册 | WEB开发手册 | 软件应用手册 | 网络技术手册 | GNU手册
- Unix/Linux C++应用开发-远程终端软件使用
- 中国人自己的VB:HB++.(网络)应用软件的专业开发工具
- Linux专区应用软件
- 应用Microsoft Visual SourceSafe组织软件开发项目
- 轻松进入Linux的桌面应用开发.gambas初步认识.
- 新闻:IBM的新工具CHIPHOPPER! 一套能够极大扩展Linux应用范围的新工具、新资源和新软件
- VxWorks操作系统指南(2.5) 应用软件开发指导
- 在Linux下开发多语言软件: Hello GetText!
- AOP在大规模软件开发项目中应用的尝试与思考(转载)
- AOP在大规模软件开发项目中应用的尝试与思考(转载)
- 关于《我从教育网收集了200多篇关于组态软件和人机界面软件开发、应用的文章,想要的朋友给我发邮件呀》的申明
- 应用Microsoft Visual SourceSafe组织软件开发项目
- 志同道合 法中结成联盟开发Linux应用平台
- 哈佛大学教授建议微软去投资Linux应用软件
- 嵌入式Linux开发工具选择和应用分析
- 为您的 Linux 应用开发加油提速 2004: 安装 DB2 Version 8.1.6