文档章节

Autoconf and Automake Tutorial

cyper
 cyper
发布于 2014/05/13 13:19
字数 856
阅读 63
收藏 0
I wrote a charming little utility for linux called getkey, that captures the keystrokes and displays them on the screen using OSD. Its called gekey and has a couple of .c files and a hand crafted Makefile to go along with it.

But I wanted to use the auto-tools (autoconf and automake) to create a configure script and a Makefile so that my utility could by installed like any other GNU tool using the famous:
./configure && make && make install

When I went hunting for autotools tutorials, they were too elementary (based on a hello.c, with no dependency check) or too long, 100 page books on autotools.

My program has a couple of dependencies (xosd, Xtst, X11) and it is small enough to understand. So I decided to use this program to write a quick and dirty tutorial for autotools, with dependency checks.

These are the steps I followed to achieve that and here are the files that I'll be using for this example. getkey_example.tar.gz

Required Tools:

Make sure the following tools are available on the system, before getting started.

tar
make
aclocal
autoconf
automake

This tutorial is based on autoconf ver 2.63.

Setup:

In order to generate the configure script and the Makefiles, we need to define a set of rules and dependencies. This is done via two files ' configure.ac' and ' Makefile.am'.

Generating the configure script:

1. Leave the default Makefile in the project folder and run ' autoscan'. It will create a vanilla configure.scan file.

2. Now move the Makefile to a different place (so it doesn't interfere with the autogenerated files in future).

mv Makefile ../.

3. Rename configure.scan to configure.ac (this used to be configure.in for old versions, it is deprecated now).

4. Edit configure.ac and modify these lines.

WAS:
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
IS:
AC_INIT([getkey], [0.1], [email-address])
WAS:
# FIXME: Replace `main' with a function in `-lX11':
AC_CHECK_LIB([X11], [main])
# FIXME: Replace `main' with a function in `-lXtst':
AC_CHECK_LIB([Xtst], [main])
# FIXME: Replace `main' with a function in `-lxosd':
AC_CHECK_LIB([xosd], [main])
Chose one function from each of the libraries used in the program and replace ' main' with the function name. Any one function is enough to verify a library.

IS:
AC_CHECK_LIB([X11], [XOpenDisplay], [],[
         echo "X11 library is required for this program"
         exit -1])
AC_CHECK_LIB([Xtst], [XRecordCreateContext], [],[
         echo "Xtst devel library is missing. Xtst is required for this program"
         exit -1])
AC_CHECK_LIB([xosd], [xosd_create], [],[
         echo "xosd devel library is missing. xosd is required for this program"
         exit -1])
SYNTAX:
AC_CHECK_LIB([libname],[func-name],[action-if-found],[action-if-not-found])
The line breaks are essential in the fourth parameter to the function AC_CHECK_LIB(). This is to make sure that configure script will exit when it can't find a dependency.

5. In configure.ac check for the line that has AC_CONFIG_FILES(). This tells the autoconf tool what are the output files. It should typically be

AC_CONFIG_FILES([Makefile])

6. Run autoconf.

autoconf

This will create the first version of configure script.

Generating the Makefile:

1. Add this line to the configure.ac
AM_INIT_AUTOMAKE([getkey], [0.1])
This instructs automake to generate makefiles after the configure script is run.
This will also define two constants PACKAGE(=getkey) and VERSION(=0.1) during make. This can be used in the code to display version specific print outs.
If you don't have a config.h file for configuration, then comment out the line: (I didn't use this in getkey)

AC_CONFIG_HEADERS([config.h])
2. Create a file named Makefile.am and add these lines.
AUTOMAKE_OPTIONS = foreign

CFLAGS=-Wall -I/usr/local/include -DVERSION=\"$(VERSION)\" -DPROG="\"$(PACKAGE)\""
LDFLAGS=-L/usr/local/lib -lX11 -lXtst -lxosd

bin_PROGRAMS = getkey
getkey_SOURCES = getkey.c circ_list.c

The first line AUTOMAKE_OPTIONS = foreign is to let automake know that it is not a standard GNU package.
A standard GNU package requires certain folders and files to be present, such as doc, src, INSTALL, COPYING, etc.

The CFLAGS and LDFLAGS is copied from the current Makefile that we moved to a different location.
bin_PROGRAMS = getkey is the name of the resulting executable.
getkey_SOURCES = getkey.c circ_list.c, this is the list of sources that will compile to result in getkey. List all the source files here.

If you have more than one resulting executable you can do
bin_PROGRAMS = prog1 prog2 prog3
LDFLAGS=-L/usr/local/lib -lX11 -lXtst -lxosd

bin_PROGRAMS = getkey
getkey_SOURCES = getkey.c circ_list.c
3. For our grand finale we run these commands

aclocal
autoconf
automake -a

aclocal is a tools that generates aclocal.m4 based on the configure.ac file which is used as an input to the autoconf tool.
autoconf will generate the configure script and automake takes Makefile.am as input and creates Makefile.in.
Now we have the final version of our configure script.

Ready to go:

1. Test out the configure script and the Makefile.

./configure
make

2. Create a package. Now that everything is working as expected, we can create a package for distribution.

make dist

This will create a tar.gz file with the package name and version number. Now it is ready for release to the world. Here you go getkey-0.1.tar.gz.

References:
http://sourceware.org/autobook/autobook/autobook_toc.html#SEC_Contents
http://www.amath.washington.edu/~lf/tutorials/autoconf/toolsmanual_toc.html
http://www.developingprogrammers.com/index.php/2006/01/05/autotools-tutorial/
Posted3rd May 2009by Amjith Ramanujam

本文转载自:http://amjith.blogspot.com/2009/04/autoconf-and-automake-tutorial.html

cyper

cyper

粉丝 59
博文 685
码字总数 143352
作品 0
武汉
前端工程师
私信 提问
加载中

评论(1)

cyper
cyper 博主
这里有个hellofuck18:
http://mij.oltrelinux.com/devel/autoconf-automake/

这里有个长篇大论:http://www.galassi.org/mark/mydocs/autoconf_tutorial_toc.html
GNU 构建系统 - Automake, Autoconf, Libtool, Gettext

需要整理关于 'GNU构建系统' 的文章 Autoconf, Automake and Libtool http://sources.redhat.com/autobook/autobook/autobook_toc.html http://sources.redhat.com/autobook/autobook/autob......

Start-up
2012/06/14
1K
0
详解linux下auto工具制作Makefile源码包(工具安装篇)

详解linux下auto工具制作Makefile源码包(工具安装篇) 水木杨 一、引子 最近学习安装源码包,然后公司开发也需要制作源码包,所以就稍微学习了一下automake与autoconf制作configure以及Mak...

慎道
2011/07/14
296
0
GNU Automake 1.13.1 发布

GNU Automake 1.13.1 更新对 Python 的支持,移除一些已经废弃的特性,修复小 bug。 GNU Automake是一种编程工具,可以产生供make程式使用的Makefile,用来编译程式。它是自由软件基金会发起...

oschina
2013/01/06
1K
0
使用Automake,Autoconf生成Makefile

在Unix上写过程序的人尤其是用 C 来开发程序的人一般都遇到过 Makefile,用 make 来开发和编译程序的确很方便,可是要写出一个Makefile就不那么简单了。GNU Make 那份几百页的文件,让许多人...

AlphaJay
2012/02/21
445
0
nouveau mesa 安装 libtool undifined AC_PROG_AM

nouveau mesa 安装 sh autogen.sh libtool undifined AC_PROG_AM libtool automake autoconf已安装 但是automake --copy不能完成 makefile ....报错 libtool undifined AC_PROG_AM 系统是中标......

xzby
2014/04/30
187
0

没有更多内容

加载失败,请刷新页面

加载更多

Rust 的 GUI 框架生态概览

本文比较全面比较了目前主流的 Rust 的 GUI 框架的表现。其中 ++ 表示非常好,-- 表示非常差,而 o 表示处于平均水平。 参与对比的框架共有 8 个,详细的比较结果如下: Electron + Neon El...

红薯
9分钟前
2
0
golang微服务框架go-micro 入门笔记2.1 micro工具之micro api

micro api micro 功能非常强大,本文将详细阐述micro api 命令行的功能 重要的事情说3次 本文全部代码https://idea.techidea8.com/open/idea.shtml?id=6 本文全部代码https://idea.techidea8....

非正式解决方案
今天
2
0
Spring Context 你真的懂了吗

今天介绍一下大家常见的一个单词 context 应该怎么去理解,正确的理解它有助于我们学习 spring 以及计算机系统中的其他知识。 1. context 是什么 我们经常在编程中见到 context 这个单词,当...

Java知其所以然
昨天
2
0
Spring Boot + Mybatis-Plus 集成与使用(二)

前言: 本章节介绍MyBatis-Puls的CRUD使用。在开始之前,先简单讲解下上章节关于Spring Boot是如何自动配置MyBatis-Plus。 一、自动配置 当Spring Boot应用从主方法main()启动后,首先加载S...

伴学编程
昨天
7
0
用最通俗的方法讲spring [一] ──── AOP

@[TOC](用最通俗的方法讲spring [一] ──── AOP) 写这个系列的目的(可以跳过不看) 自己写这个系列的目的,是因为自己是个比较笨的人,我曾一度怀疑自己的智商不适合干编程这个行业.因为在我...

小贼贼子
昨天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部