1 \input texinfo @c -*-texinfo-*-
2 @comment $Id: gnulib.texi,v 1.18 2005-09-19 15:38:33 haible Exp $
3 @comment %**start of header
4 @setfilename gnulib.info
8 @comment %**end of header
10 @set UPDATED $Date: 2005-09-19 15:38:33 $
13 This manual is for GNU Gnulib (updated @value{UPDATED}),
14 which is a library of common routines intended to be shared at the
17 Copyright @copyright{} 2004, 2005 Free Software Foundation, Inc.
20 Permission is granted to copy, distribute and/or modify this document
21 under the terms of the GNU Free Documentation License, Version 1.1 or
22 any later version published by the Free Software Foundation; with no
23 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
24 and with the Back-Cover Texts as in (a) below. A copy of the
25 license is included in the section entitled ``GNU Free Documentation
28 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
29 this GNU Manual, like GNU software. Copies published by the Free
30 Software Foundation raise funds for GNU development.''
34 @dircategory Software development
36 * Gnulib: (gnulib). Source files to share among distributions.
41 @subtitle updated @value{UPDATED}
42 @author @email{bug-gnulib@@gnu.org}
44 @vskip 0pt plus 1filll
59 * Invoking gnulib-tool::
60 * Copying This Manual::
68 This manual contains some bare-bones documentation, but not much more.
69 It's mostly been a place to store notes until someone (you?)@ gets
70 around to writing a coherent manual.
75 @item Gnulib is hosted at Savannah:
76 @url{http://savannah.gnu.org/projects/gnulib}. Get the sources
77 through CVS from there.
78 @item The Gnulib home page:
79 @url{http://www.gnu.org/software/gnulib/}.
88 * Out of memory handling::
89 * Library version handling::
90 * Regular expressions::
97 @cindex comments describing functions
98 @cindex describing functions, locating
99 Where to put comments describing functions: Because of risk of
100 divergence, we prefer to keep most function describing comments in
101 only one place: just above the actual function definition. Some
102 people prefer to put that documentation in the .h file. In any case,
103 it should appear in just one place unless you can ensure that the
104 multiple copies will always remain identical.
108 @section Header files
110 @cindex double inclusion of header files
111 @cindex header file include protection
112 It is a tradition to use CPP tricks to avoid parsing the same header
113 file more than once, which might cause warnings. The trick is to wrap
114 the content of the header file (say, @file{foo.h}) in a block, as in:
120 body of header file goes here
125 Whether to use @code{FOO_H} or @code{_FOO_H} is a matter of taste and
126 style. The C89 and C99 standards reserve all identifiers that begin with an
127 underscore and either an uppercase letter or another underscore, for
128 any use. Thus, in theory, an application might not safely assume that
129 @code{_FOO_H} has not already been defined by a library. On the other
130 hand, using @code{FOO_H} will likely lead the higher risk of
131 collisions with other symbols (e.g., @code{KEY_H}, @code{XK_H}, @code{BPF_H},
132 which are CPP macro constants, or @code{COFF_LONG_H}, which is a CPP
133 macro function). Your preference may depend on whether you consider
134 the header file under discussion as part of the application (which has
135 its own namespace for CPP symbols) or a supporting library (that
136 shouldn't interfere with the application's CPP symbol namespace).
138 @cindex C++ header files
139 @cindex Header files and C++
140 Adapting C header files for use in C++ applications can use another
149 body of header file goes here
156 The idea here is that @code{__cplusplus} is defined only by C++
157 implementations, which will wrap the header file in an @samp{extern "C"}
158 block. Again, whether to use this trick is a matter of taste and
159 style. While the above can be seen as harmless, it could be argued
160 that the header file is written in C, and any C++ application using it
161 should explicitly use the @samp{extern "C"} block itself. Your
162 preference might depend on whether you consider the API exported by
163 your header file as something available for C programs only, or for C
164 and C++ programs alike.
166 @subheading Include ordering
168 When writing a gnulib module, or even in general, a good way to order
169 the @samp{#include} directives is the following.
172 @item First comes the #include "..." specifying the module being implemented.
173 @item Then come all the #include <...> of system or system-replacement headers,
175 @item Then come all the #include "..." of gnulib and private headers, in
187 Gnulib provides @samp{quote} and @samp{quotearg} modules to help with
188 quoting text, such as file names, in messages to the user. Here's an
189 example of using @samp{quote}:
194 error (0, errno, _("cannot change owner of %s"), quote (fname));
200 error (0, errno, _("cannot change owner of `%s'"), fname);
203 @noindent in that @code{quote} escapes unusual characters in
204 @code{fname}, e.g., @samp{'} and control characters like @samp{\n}.
207 However, a caveat: @code{quote} reuses the storage that it returns.
208 Hence if you need more than one thing quoted at the same time, you
209 need to use @code{quote_n}.
211 @findex quotearg_alloc
212 Also, the quote module is not suited for multithreaded applications.
213 In that case, you have to use @code{quotearg_alloc}, defined in the
214 @samp{quotearg} module, which is decidedly less convenient.
221 The @code{ctime} function need not be reentrant, and consequently is
222 not required to be thread safe. Implementations of @code{ctime}
223 typically write the time stamp into static buffer. If two threads
224 call @code{ctime} at roughly the same time, you might end up with the
225 wrong date in one of the threads, or some undefined string. There is
226 a re-entrant interface @code{ctime_r}, that take a pre-allocated
227 buffer and length of the buffer, and return @code{NULL} on errors.
228 The input buffer should be at least 26 bytes in size. The output
229 string is locale-independent. However, years can have more than 4
230 digits if @code{time_t} is sufficiently wide, so the length of the
231 required output buffer is not easy to determine. Increasing the
232 buffer size when @code{ctime_r} return @code{NULL} is not necessarily
233 sufficient. The @code{NULL} return value could mean some other error
234 condition, which will not go away by increasing the buffer size.
236 A more flexible function is @code{strftime}. However, note that it is
244 The @code{inet_ntoa} function need not be reentrant, and consequently
245 is not required to be thread safe. Implementations of
246 @code{inet_ntoa} typically write the time stamp into static buffer.
247 If two threads call @code{inet_ntoa} at roughly the same time, you
248 might end up with the wrong date in one of the threads, or some
249 undefined string. Further, @code{inet_ntoa} is specific for
250 @acronym{IPv4} addresses.
252 A protocol independent function is @code{inet_ntop}.
255 @node Out of memory handling
256 @section Out of memory handling
258 @cindex Out of Memory handling
259 @cindex Memory allocation failure
260 The GSS API does not have a standard error code for the out of memory
261 error condition. Instead of adding a non-standard error code, this
262 library has chosen to adopt a different strategy. Out of memory
263 handling happens in rare situations, but performing the out of memory
264 error handling after almost all API function invocations pollute your
265 source code and might make it harder to spot more serious problems.
266 The strategy chosen improve code readability and robustness.
268 @cindex Aborting execution
269 For most applications, aborting the application with an error message
270 when the out of memory situation occur is the best that can be wished
271 for. This is how the library behaves by default.
273 @vindex xalloc_fail_func
274 However, we realize that some applications may not want to have the
275 GSS library abort execution in any situation. The GSS library support
276 a hook to let the application regain control and perform its own
277 cleanups when an out of memory situation has occured. The application
278 can define a function (having a @code{void} prototype, i.e., no return
279 value and no parameters) and set the library variable
280 @code{xalloc_fail_func} to that function. The variable should be
284 extern void (*xalloc_fail_func) (void);
287 The GSS library will invoke this function if an out of memory error
288 occurs. Note that after this the GSS library is in an undefined
289 state, so you must unload or restart the application to continue call
290 GSS library functions. The hook is only intended to allow the
291 application to log the situation in a special way. Of course, care
292 must be taken to not allocate more memory, as that will likely also
296 @node Library version handling
297 @section Library version handling
299 The module @samp{check-version} can be useful when your gnulib
300 application is a system library. You will typically wrap the call to
301 the @code{check_version} function through a library API, your library
302 header file may contain:
305 #define STRINGPREP_VERSION "0.5.18"
307 extern const char *stringprep_check_version (const char *req_version);
310 To avoid ELF symbol collisions with other libraries that use the
311 @samp{check-version} module, add to @file{config.h} through a
312 AC_DEFINE something like:
315 AC_DEFINE(check_version, stringprep_check_version, [Rename check_version.])
318 The @code{stringprep_check_version} function will thus be implemented
319 by the @code{check_version} module.
321 There are two uses of the interface. The first is a way to provide
322 for applications to find out the version number of the library it
323 uses. The application may contain diagnostic code such as:
326 printf ("Stringprep version: header %s library %s",
328 stringprep_check_version (NULL));
331 Separating the library and header file version can be useful when
332 searching for version mismatch related problems.
334 The second uses is as a rudimentary test of proper library version, by
335 making sure the application get a library version that is the same, or
336 newer, than the header file used when building the application. This
337 doesn't catch all problems, libraries may change backwards incompatibly
338 in later versions, but enable applications to require a certain
339 minimum version before it may proceed.
341 Typical uses look like:
344 /* Check version of libgcrypt. */
345 if (!gcry_check_version (GCRYPT_VERSION))
346 die ("version mismatch\n");
350 @node Regular expressions
351 @section Regular expressions
353 Gnulib supports many different types of regular expressions; although
354 the underlying features are the same or identical, the syntax used
355 varies. The descriptions given here for the different types are
356 generated automatically.
358 @include regexprops-generic.texi
361 @node Invoking gnulib-tool
362 @chapter Invoking gnulib-tool
365 @cindex invoking @command{gnulib-tool}
367 @command{gnulib-tool} is the way to import Gnulib modules. It is also
368 possible to borrow Gnulib modules in a package without using
369 @command{gnulib-tool}, relying only on the metainformation stored in
370 the @file{modules/*} files, but with a growing number of modules this
371 becomes tedious. @command{gnulib-tool} simplifies the management of
372 source files, @file{Makefile.am}s and @file{configure.ac} in packages
373 incorporating Gnulib modules.
375 Run @samp{gnulib-tool --help}. To get familiar with @command{gnulib-tool},
376 you can also try some commands with the option @samp{--dry-run}; then
377 @code{gnulib-tool} will only report which actions it would perform in a
381 * Initial import:: First import of Gnulib modules.
382 * Modified imports:: Changing the import specification.
383 * Simple update:: Tracking Gnulib development.
384 * CVS Issues:: Integration with CVS.
389 @section Initial import
390 @cindex initial import
392 Gnulib assumes your project uses Autoconf and Automake. Invoking
393 @samp{gnulib-tool --import} will copy source files, create a
394 @file{Makefile.am} to build them, generate a file @file{gnulib-comp.m4} with
395 Autoconf M4 macro declarations used by @file{configure.ac}, and generate
396 a file @file{gnulib-cache.m4} containing the cached specification of how
399 Our example will be a library that uses Autoconf, Automake and
400 Libtool. It calls @code{strdup}, and you wish to use gnulib to make
401 the package portable to C89 (which doesn't have @code{strdup}).
404 ~/src/libfoo$ gnulib-tool --import strdup
405 Module list with included dependencies:
412 Copying file m4/gnulib-tool.m4
413 Copying file m4/onceonly_2_57.m4
414 Copying file lib/strdup.c
415 Copying file lib/strdup.h
416 Copying file m4/strdup.m4
417 Creating lib/Makefile.am
418 Creating m4/gnulib-cache.m4
419 Creating m4/gnulib-comp.m4
422 You may need to add #include directives for the following .h files.
426 - add "lib/Makefile" to AC_CONFIG_FILES in ./configure.ac,
427 - mention "lib" in SUBDIRS in Makefile.am,
428 - mention "-I m4" in ACLOCAL_AMFLAGS in Makefile.am,
429 - invoke gl_EARLY in ./configure.ac, right after AC_PROG_CC,
430 - invoke gl_INIT in ./configure.ac.
434 By default, the source code is copied into @file{lib/} and the M4
435 macros in @file{m4/}. You can override these paths by using
436 @code{--source-base=DIRECTORY} and @code{--m4-base=DIRECTORY}. Some
437 modules also provide other files necessary for building. These files
438 are copied into the directory specified by @samp{AC_CONFIG_AUX_DIR} in
439 @file{configure.ac} or by the @code{--aux-dir=DIRECTORY} option. If
440 neither is specified, the current directory is assumed.
442 @code{gnulib-tool} can make symbolic links instead of copying the
443 source files. Use the @samp{--symbolic} (or @samp{-s} for short) option
446 @code{gnulib-tool} will overwrite any pre-existing files, in
447 particular @file{Makefile.am}. Unfortunately, separating the
448 generated @file{Makefile.am} content (for building the gnulib library)
449 into a separate file, say @file{gnulib.mk}, that could be included
450 by your handwritten @file{Makefile.am} is not possible, due to how
451 variable assignments are handled by Automake.
453 Consequently, it is a good idea to choose directories that are not
454 already used by your projects, to separate gnulib imported files from
455 your own files. This approach is also useful if you want to avoid
456 conflicts between other tools (e.g., @code{gettextize} that also copy
457 M4 files into your package. Simon Josefsson successfully uses a source
458 base of @file{gl/}, and a M4 base of @file{gl/m4/}, in several
461 After the @samp{--import} option on the command line comes the list of
462 Gnulib modules that you want to incorporate in your package. The names
463 of the modules coincide with the filenames in Gnulib's @file{modules/}
466 Some Gnulib modules depend on other Gnulib modules. @code{gnulib-tool}
467 will automatically add the needed modules as well; you need not list
468 them explicitly. @code{gnulib-tool} will also memoize which dependent
469 modules it has added, so that when someday a dependency is dropped, the
470 implicitly added module is dropped as well (unless you have explicitly
471 requested that module).
473 If you want to cut a dependency, i.e. not add a module although one of
474 your requested modules depends on it, you may use the option
475 @samp{--avoid=@var{module}} to do so. Multiple uses of this option are
476 possible. Of course, you will then need to implement the same interface
477 as the removed module.
479 A few manual steps are required to finish the initial import.
480 @code{gnulib-tool} printed a summary of these steps.
482 First, you need to make sure Autoconf can find the macro definitions
483 in @file{gnulib-comp.m4}. Use the @code{ACLOCAL_AMFLAGS} specifier in your
484 top-level @file{Makefile.am} file, as in:
487 ACLOCAL_AMFLAGS = -I m4
490 You are now ready to call the M4 macros in @code{gnulib-comp.m4} from
491 @file{configure.ac}. The macro @code{gl_EARLY} must be called as soon
492 as possible after verifying that the C compiler is working.
493 Typically, this is immediately after @code{AC_PROG_CC}, as in:
502 The core part of the gnulib checks are done by the macro
503 @code{gl_INIT}. Place it further down in the file, typically where
504 you normally check for header files or functions. For example:
513 @code{gl_INIT} will in turn call the macros related with the
514 gnulib functions, be it specific gnulib macros, like @code{gl_FUNC_ALLOCA}
515 or autoconf or automake macros like @code{AC_FUNC_ALLOCA} or
516 @code{AM_FUNC_GETLINE}. So there is no need to call those macros yourself
517 when you use the corresponding gnulib modules.
519 You must also make sure that the gnulib library is built. Add the
520 @code{Makefile} in the gnulib source base directory to
521 @code{AC_CONFIG_FILES}, as in:
524 AC_CONFIG_FILES(... lib/Makefile ...)
527 You must also make sure that @code{make} will recurse into the gnulib
528 directory. To achieve this, add the gnulib source base directory to a
529 @code{SUBDIRS} Makefile.am statement, as in:
535 or if you, more likely, already have a few entries in @code{SUBDIRS},
536 you can add something like:
542 Finally, you have to add compiler and linker flags in the appropriate
543 source directories, so that you can make use of the gnulib library.
544 Since some modules (@samp{getopt}, for example) may copy files into
545 the build directory, @file{top_builddir/lib} is needed as well
546 as @file{top_srcdir/lib}. For example:
550 AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_builddir)/lib
552 LIBADD = lib/libgnu.a
556 Don't forget to @code{#include} the various header files. In this
557 example, you would need to make sure that @samp{#include "strdup.h"}
558 is evaluated when compiling all source code files, that want to make
559 use of @code{strdup}.
561 When an include file is provided by Gnulib
562 you shouldn't try to include the corresponding system header files
563 yourself, but let the gnulib header file do it. The ordering
564 of the definition for some symbols may be significant; the Gnulib
565 header files take care of that.
567 For example, to use the @code{time_r} gnulib module you should
568 use include header file provided by the gnulib, and so
569 @samp{#include "time_r.h"}, but you shouldn't explicitly
570 @samp{#include <time.h>} as it is already done in @file{time_r.h}
571 before the redefinition of some symbols.
573 @node Modified imports
574 @section Modified imports
576 You can at any moment decide to use Gnulib differently than the last time.
578 If you only want to use more Gnulib modules, simply invoke
579 @command{gnulib-tool --import @var{new-modules}}. @code{gnulib-tool}
580 remembers which modules were used last time. The list of modules that
581 you pass after @samp{--import} is @emph{added} to the previous list of
584 For most changes, such as added or removed modules, or even different
585 choices of @samp{--lib}, @samp{--source-base} or @samp{--aux-dir}, there
586 are two ways to perform the change.
588 The standard way is to modify manually the file @file{gnulib-cache.m4}
589 in the M4 macros directory, then launch @samp{gnulib-tool --import}.
591 The other way is to call @command{gnulib-tool} again, with the changed
592 command-line options. Note that this doesn't let you remove modules,
593 because as you just learned, the list of modules is always cumulated.
594 Also this way is often impractical, because you don't remember the way
595 you invoked @code{gnulib-tool} last time.
597 The only change for which this doesn't work is a change of the
598 @samp{--m4-base} directory. Because, when you pass a different value of
599 @samp{--m4-base}, @code{gnulib-tool} will not find the previous
600 @file{gnulib-cache.m4} file any more... A possible solution is to manually
601 copy the @file{gnulib-cache.m4} into the new M4 macro directory.
603 In the @file{gnulib-cache.m4}, the macros have the following meaning:
606 The argument is a space separated list of the requested modules, not including
610 The argument is a space separated list of modules that should not be used,
611 even if they occur as dependencies. Corresponds to the @samp{--avoid}
612 command line argument.
615 The argument is the relative pathname of the directory containing the gnulib
616 source files (mostly *.c and *.h files). Corresponds to the
617 @samp{--source-base} command line argument.
620 The argument is the relative pathname of the directory containing the gnulib
621 M4 macros (*.m4 files). Corresponds to the @samp{--m4-base} command line
625 The argument is the relative pathname of the directory containing the gnulib
626 unit test files. Corresponds to the @samp{--tests-base} command line argument.
629 The argument is the name of the library to be created. Corresponds to the
630 @samp{--lib} command line argument.
633 The presence of this macro corresponds to the @samp{--lgpl} command line
634 argument. It takes no arguments.
637 The presence of this macro corresponds to the @samp{--libtool} command line
638 argument. It takes no arguments.
640 @item gl_MACRO_PREFIX
641 The argument is the prefix to use for macros in the @file{gnulib-comp.m4}
642 file. Corresponds to the @samp{--macro-prefix} command line argument.
646 @section Simple update
648 When you want to update to a more recent version of Gnulib, without
649 changing the list of modules or other parameters, a simple call
653 $ gnulib-tool --import
656 This will create, update or remove files, as needed.
661 All files created by @code{gnulib-tool}, except @file{gnulib-cache.m4},
662 should be treated like generated source files, like for example a
663 @file{parser.c} file is generated from @file{parser.y}.
665 In projects which commit all source files, whether generated or not, into
666 CVS, the @code{gnulib-tool} generated files should all be committed.
668 In projects which customarily omit from the CVS all files that generated
669 from other source files, all these files and directories would not be
670 added into CVS. The only file that must be added to CVS is
671 @file{gnulib-cache.m4} in the M4 macros directory. Also, the script for
672 restoring files not in CVS, customarily called @file{autogen.sh} or
673 @file{bootstrap.sh}, will typically contain the statement for restoring
677 $ gnulib-tool --update
680 The @samp{--update} option operates much like the @samp{--import} option,
681 but it does not offer the possibility to change the way Gnulib is used.
682 Also it does not report in the ChangeLogs the files that it had to add
683 because they were missing.
685 @node Copying This Manual
686 @appendix Copying This Manual
689 * GNU Free Documentation License:: License for copying this manual.