Tài liệu Autotools: A Practioner''''s Guide to GNU Autoconf, Automake, and Libtool - Pdf 10

www.nostarch.com
THE FINEST IN GEEK ENTERTAINMENT

SHELVE IN:
COMPUTERS/PROGRAMMING
$44.95 ($56.95 CDN)
CREATING
PORTABLE
SOFTWARE JUST
GOT EASIER
CREATING
PORTABLE
SOFTWARE JUST
GOT EASIER
“I LIE FLAT.”
This book uses RepKover—a durable binding that won’t snap shut.
The GNU Autotools make it easy for developers to
create software that is portable across many Unix-like
operating systems. Although the Autotools are used
by thousands of open source software packages, they
have a notoriously steep learning curve. And good luck
to the beginner who wants to find anything beyond a
basic reference work online.
Autotools is the first book to offer programmers a tutorial-
based guide to the GNU build system. Author John
Calcote begins with an overview of high-level concepts
and a quick hands-on tour of the philosophy and design
of the Autotools. He then tackles more advanced details,
like using the M4 macro processor with Autoconf,
extending the framework provided by Automake, and
building Java and C# sources. He concludes the book

GNU AUTOCONF, AUTOMAKE, AND LIBTOOL
JOHN CALCOTE
CALCOTE
AUTOTOOLS
AUTOTOOLS
www.it-ebooks.info

www.it-ebooks.info
AUTOTOOLS
Autotools_02.book Page i Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
Autotools_02.book Page ii Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
AUTOTOOLS
A Practitioner’s Guide to
GNU Autoconf, Automake,
and Libtool
by John Calcote
San Francisco
Autotools_02.book Page iii Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
AUTOTOOLS. Copyright © 2010 by John Calcote.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior
written permission of the copyright owner and the publisher.
14 13 12 11 10 1 2 3 4 5 6 7 8 9
ISBN-10: 1-59327-206-5
ISBN-13: 978-1-59327-206-7
Publisher: William Pollock
Production Editor: Ansel Staton

Autotools_02.book Page iv Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
For Michelle
But to see her was to love her;
Love but her, and love forever.
—Robert Burns
Autotools_02.book Page v Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
Autotools_02.book Page vi Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
BRIEF CONTENTS
Foreword by Ralf Wildenhues xv
Preface xvii
Introduction xxi
Chapter 1: A Brief Introduction to the GNU Autotools 1
Chapter 2: Understanding the GNU Coding Standards 19
Chapter 3: Configuring Your Project with Autoconf 57
Chapter 4: More Fun with Autoconf: Configuring User Options 89
Chapter 5: Automatic Makefiles with Automake 119
Chapter 6: Building Libraries with Libtool 145
Chapter 7: Library Interface Versioning and Runtime Dynamic Linking 171
Chapter 8: FLAIM: An Autotools Example 195
Chapter 9: FLAIM Part II: Pushing the Envelope 229
Chapter 10: Using the M4 Macro Processor with Autoconf 251
Chapter 11: A Catalog of Tips and Reusable Solutions for Creating Great Projects 271
Index 313
Autotools_02.book Page vii Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
Autotools_02.book Page viii Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info

Libtool 11
libtool 12
libtoolize 12
ltdl, the Libtool C API 12
Building Your Package 13
Running configure 13
Running make 15
Installing the Most Up-to-Date Autotools 16
Summary 18
Autotools_02.book Page ix Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
x Contents in Detail
2
UNDERSTANDING THE GNU CODING STANDARDS 19
Creating a New Project Directory Structure 20
Project Structure 21
Makefile Basics 22
Commands and Rules 23
Variables 24
A Separate Shell for Each Command 25
Variable Binding 26
Rules in Detail 27
Resources for Makefile Authors 32
Creating a Source Distribution Archive 32
Forcing a Rule to Run 34
Leading Control Characters 35
Automatically Testing a Distribution 36
Unit Testing, Anyone? 37
Installing Products 38
Installation Choices 40

AC_INIT 76
AC_CONFIG_SRCDIR 77
Autotools_02.book Page x Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
Contents in Detail xi
The Instantiating Macros 78
AC_CONFIG_HEADERS 83
Using autoheader to Generate an Include File Template 84
Back to Remote Builds for a Moment 87
Summary 88
4
MORE FUN WITH AUTOCONF:
CONFIGURING USER OPTIONS 89
Substitutions and Definitions 90
AC_SUBST 90
AC_DEFINE 91
Checking for Compilers 91
Checking for Other Programs 93
A Common Problem with Autoconf 95
Checks for Libraries and Header Files 98
Is It Right or Just Good Enough? 101
Printing Messages 106
Supporting Optional Features and Packages 107
Coding Up the Feature Option 109
Formatting Help Strings 112
Checks for Type and Structure Definitions 112
The AC_OUTPUT Macro 116
Summary 117
5
AUTOMATIC MAKEFILES

Abstraction at Runtime 151
Installing Libtool 152
Adding Shared Libraries to Jupiter 152
Using the LTLIBRARIES Primary 153
Public Include Directories 153
Customizing Libtool with LT_INIT Options 157
Reconfigure and Build 161
So What Is PIC, Anyway? 164
Fixing the Jupiter PIC Problem 167
Summary 170
7
LIBRARY INTERFACE VERSIONING AND
RUNTIME DYNAMIC LINKING 171
System-Specific Versioning 172
Linux and Solaris Library Versioning 172
IBM AIX Library Versioning 173
HP-UX/AT&T SVR4 Library Versioning 176
The Libtool Library Versioning Scheme 176
Library Versioning Is Interface Versioning 177
When Library Versioning Just Isn’t Enough 180
Using libltdl 181
Necessary Infrastructure 181
Adding a Plug-In Interface 183
Doing It the Old-Fashioned Way 184
Converting to Libtool’s ltdl Library 188
Preloading Multiple Modules 192
Checking It All Out 193
Summary 194
8
FLAIM: AN AUTOTOOLS EXAMPLE 195

Building the C# Sources 239
Manual Installation 242
Cleaning Up Again 243
Configuring Compiler Options 243
Hooking Doxygen into the Build Process 245
Adding Nonstandard Targets 247
Summary 250
10
USING THE M4 MACRO PROCESSOR WITH AUTOCONF 251
M4 Text Processing 252
Defining Macros 253
Macros with Arguments 255
The Recursive Nature of M4 256
Quoting Rules 258
Autoconf and M4 259
The Autoconf M4 Environment 260
Writing Autoconf Macros 260
Simple Text Replacement 260
Documenting Your Macros 263
M4 Conditionals 264
Diagnosing Problems 268
Summary 269
11
A CATALOG OF TIPS AND REUSABLE SOLUTIONS
FOR CREATING GREAT PROJECTS 271
Item 1: Keeping Private Details out of Public Interfaces 272
Solutions in C 273
Solutions in C++ 273
Autotools_02.book Page xiii Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info

ways: They were either written several years ago and are starting to show their
age, contain at least some inaccuracies, or tend to be incomplete for typical
beginner’s tasks. On the other hand, the GNU manuals for these programs
are fairly large and rather technical, and as such, they may present a signifi-
cant entry barrier to learning your ways around the Autotools.
John Calcote began this book with an online tutorial that shared at least
some of the problems facing other tutorials. Around that time, he became a
regular contributor to discussions on the Autotools mailing lists, too. John
kept asking more and more questions, and discussions with him uncovered
some bugs in the Autotools sources and documentation, as well as some
issues in his tutorial.
Autotools_02.book Page xv Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
xvi Foreword
Since that time, John has reworked the text a lot. The review uncovered
several more issues in both software and book text, a nice mutual benefit. As
a result, this book has become a great introductory text that still aims to be
accurate, up to date with current Autotools, and quite comprehensive in a
way that is easily understood.
Always going by example, John explores the various software layers, port-
ability issues and standards involved, and features needed for package build
development. If you’re new to the topic, the entry path may just have become
a bit less steep for you.
Ralf Wildenhues
Bonn, Germany
June 2010
Autotools_02.book Page xvi Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
PREFACE
I’ve often wondered during the last ten years how it

What we need is a cookbook-style approach with the recipes covering
real problems found in real projects. First the basics are covered, sauces and
reductions, followed by various cooking techniques. Finally, master recipes
are presented for culinary wonders. As each recipe is mastered, the reader
makes small intuitive leaps—I call them minor epiphanies. Put enough of these
under your belt and overall mastery of the Autotools is ultimately inevitable.
Let me give you an analogy. I’d been away from math classes for about
three years when I took my first college calculus course. I struggled the entire
semester with little progress. I understood the theory, but I had trouble
with the homework. I just didn’t have the background I needed. So the
next semester, I took college algebra and trigonometry back to back as half-
semester classes. At the end of that semester, I tried calculus again. This time
I did very well—finishing the class with a solid A grade. What was missing the
first time? Just basic math skills. You’d think it wouldn’t have made that much
difference, but it really does.
The same concept applies to learning to properly use the Autotools. You
need a solid understanding of the tools upon which the Autotools are built
in order to become proficient with the Autotools themselves.
Why Use the Autotools?
In the early 1990s, I was working on the final stages of my bachelor’s degree
in computer science at Brigham Young University. I took an advanced com-
puter graphics class where I was introduced to C++ and the object-oriented
programming paradigm. For the next couple of years, I had a love-hate rela-
tionship with C++. I was a pretty good C coder by that time, and I thought I
could easily pick up C++, as close in syntax as it was to C. How wrong I was!
I fought with the C++ compiler more often than I’d care to recall.
The problem was that the most fundamental differences between C
and C++ are not obvious to the casual observer, because they’re buried
deep within the C++ language specification rather than on the surface in
the language syntax. The C++ compiler generates an amazing amount of

installed cleanly on his system.
Most packages are built using the
make utility. It’s very easy to type make,
but that’s not the problem. The problem crops up when the package doesn’t
build successfully because of some unanticipated difference between the user’s
system and the developer’s system. Thus was born the ubiquitous
configure
script—initially a simple shell script that configured the end user’s environ-
ment so that
make could successfully find the required external resources
on the user’s system. Hand-coded configuration scripts helped, but they
weren’t the final answer. They fixed about 65 percent of the problems result-
ing from system configuration differences—and they were a pain in the neck
to write properly and to maintain. Dozens of changes were made incremen-
tally over a period of years, until the script worked properly on most of the
systems anyone cared about. But the entire process was clearly in need of an
upgrade.
Do you have any idea of the number of build-breaking differences there
are between existing systems today? Neither do I, but there are a handful of
developers in the world who know a large percentage of these differences.
Between them and the open source software community, the GNU Autotools
were born. The Autotools were designed to create configuration scripts and
makefiles that work correctly and provide significant chunks of valuable
end-user functionality under most circumstances, and on most systems—
even on systems not initially considered (or even conceived of) by the pack-
age maintainer.
With this in mind, the primary purpose of the Autotools is not to make
life simpler for the package maintainer (although it really does in the long
run). The primary purpose of the Autotools is to make life simpler for the end user.
Autotools_02.book Page xix Tuesday, June 15, 2010 2:38 PM

Elk Ridge, Utah
June 2010
Autotools_02.book Page xx Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
INTRODUCTION
Few software developers would deny that
GNU Autoconf, Automake, and Libtool
(the
Autotools) have revolutionized the open
source software world. But while there are many
thousands of Autotools advocates, there are also many
developers who hate the Autotools—with a passion.
The reason for this dread of the Autotools, I think, is that when you use the
Autotools, you have to understand the underlying infrastructure that they
manage. Otherwise, you’ll find yourself fighting against the system.
This book solves this problem by first providing a framework for under-
standing the underlying infrastructure of the Autotools and then building
on that framework with a tutorial-based approach to teaching Autotools
concepts in a logically ordered fashion.
Autotools_02.book Page xxi Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
xxii Introduction
Who Should Read This Book
This book is for the open source software package maintainer who wants to
become an Autotools expert. Existing material on the subject is limited to
the GNU Autotools manuals and a few Internet-based tutorials. For years
most real-world questions have been answered on the Autotools mailing lists,
but mailing lists are an inefficient form of teaching because the same answers
to the same questions are given time and again. This book provides a cook-
book style approach, covering real problems found in real projects.

Chapter 5 discusses converting the Jupiter project Makefile.in templates
into Automake Makefile.am files. Here you’ll discover that Automake is to
makefiles what Autoconf is to configuration scripts. This chapter presents
the major features of Automake in a manner that will not become outdated
as new versions of Automake are released.
Autotools_02.book Page xxii Tuesday, June 15, 2010 2:38 PM
www.it-ebooks.info
Introduction xxiii
Chapters 6 and 7 explain basic shared-library concepts and show how
to build shared libraries with Libtool—a stand-alone abstraction for shared
library functionality that can be used with the other Autotools. Chapter 6
begins with a shared-library primer and then covers some basic Libtool
extensions that allow Libtool to be a drop-in replacement for the more
basic library generation functionality provided by Automake. Chapter 7
covers library versioning and runtime dynamic module management fea-
tures provided by Libtool.
Chapters 8 and 9 show the transformation of an existing, fairly complex,
open source project (FLAIM) from using a hand-built build system to using
an Autotools build system. This example will help you to understand how you
might autoconfiscate one of your own existing projects.
Chapter 10 provides an overview of the features of the M4 macro proces-
sor that are relevant to obtaining a solid understanding of Autoconf. This
chapter also considers the process of writing your own Autoconf macros.
Chapter 11 is a compilation of tips, tricks, and reusable solutions to
Autoconf problems. The solutions in this chapter are presented as a set of
individual topics or items. Each item can be understood without context
from the surrounding items.
Most of the examples shown in listings in this book are available for
download from />Conventions Used in This Book
This book contains hundreds of program listings in roughly two categories:


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status