Next: , Previous: , Up: Invoking gnulib-tool   [Contents][Index]

3.2 Initial import

Gnulib assumes that your project uses Autoconf. When using Gnulib, you will need to have Autoconf among your build tools.

Unless you use gnulib-tool’s --gnu-make option, Gnulib also assumes that your project uses Automake at least in a subdirectory of your project. While the use of Automake in your project’s top level directory is an easy way to fulfil the Makefile conventions of the GNU coding standards, Gnulib does not require it.

Invoking ‘gnulib-tool --import’ will copy source files, create a to build them, generate a file gnulib-comp.m4 with Autoconf M4 macro declarations used by, and generate a file gnulib-cache.m4 containing the cached specification of how Gnulib is used.

Our example will be a library that uses Autoconf, Automake and Libtool. It calls strdup, and you wish to use gnulib to make the package portable to C99 and C11 (which don’t have strdup).

~/src/libfoo$ gnulib-tool --import strdup
Module list with included dependencies:
File list:
Creating directory ./lib
Creating directory ./m4
Copying file lib/dummy.c
Copying file lib/strdup.c
Copying file lib/
Copying file m4/absolute-header.m4
Copying file m4/extensions.m4
Copying file m4/gnulib-common.m4
Copying file m4/gnulib-tool.m4
Copying file m4/strdup.m4
Copying file m4/string_h.m4
Creating lib/
Creating m4/gnulib-cache.m4
Creating m4/gnulib-comp.m4

You may need to add #include directives for the following .h files.
  #include <string.h>

Don't forget to
  - add "lib/Makefile" to AC_CONFIG_FILES in ./,
  - mention "lib" in SUBDIRS in,
  - mention "-I m4" in ACLOCAL_AMFLAGS in,
  - invoke gl_EARLY in ./, right after AC_PROG_CC,
  - invoke gl_INIT in ./

By default, the source code is copied into lib/ and the M4 macros in m4/. You can override these paths by using --source-base=DIRECTORY and --m4-base=DIRECTORY. Some modules also provide other files necessary for building. These files are copied into the directory specified by ‘AC_CONFIG_AUX_DIR’ in or by the --aux-dir=DIRECTORY option. If neither is specified, the current directory is assumed.

gnulib-tool can make symbolic links instead of copying the source files. The option to specify for this is ‘--symlink’, or ‘-s’ for short. This can be useful to save a few kilobytes of disk space. But it is likely to introduce bugs when gnulib is updated; it is more reliable to use ‘gnulib-tool --update’ (see below) to update to newer versions of gnulib. Furthermore it requires extra effort to create self-contained tarballs, and it may disturb some mechanism the maintainer applies to the sources. For these reasons, this option is generally discouraged.

gnulib-tool will overwrite any pre-existing files, in particular It is also possible to separate the generated content (for building the gnulib library) into a separate file, say, that can be included by your handwritten, but this is a more advanced use of gnulib-tool.

Consequently, it is a good idea to choose directories that are not already used by your projects, to separate gnulib imported files from your own files. This approach is also useful if you want to avoid conflicts between other tools (e.g., gettextize that also copy M4 files into your package. Simon Josefsson successfully uses a source base of gl/, and a M4 base of gl/m4/, in several packages.

After the ‘--import’ option on the command line comes the list of Gnulib modules that you want to incorporate in your package. The names of the modules coincide with the filenames in Gnulib’s modules/ directory.

Some Gnulib modules depend on other Gnulib modules. gnulib-tool will automatically add the needed modules as well; you need not list them explicitly. gnulib-tool will also memorize which dependent modules it has added, so that when someday a dependency is dropped, the implicitly added module is dropped as well (unless you have explicitly requested that module).

If you want to cut a dependency, i.e., not add a module although one of your requested modules depends on it, you may use the option ‘--avoid=module’ to do so. Multiple uses of this option are possible. Of course, you will then need to implement the same interface as the removed module.

A few manual steps are required to finish the initial import. gnulib-tool printed a summary of these steps.

First, you must ensure Autoconf can find the macro definitions in gnulib-comp.m4. Use the ACLOCAL_AMFLAGS specifier in your top-level file, as in:


You are now ready to call the M4 macros in gnulib-comp.m4 from The macro gl_EARLY must be called as soon as possible after verifying that the C compiler is working. Typically, this is immediately after AC_PROG_CC, as in:


If you are using AC_PROG_CC_STDC, the macro gl_EARLY must be called after it, like this:


The core part of the gnulib checks are done by the macro gl_INIT. Place it further down in the file, typically where you normally check for header files or functions. It must come after other checks which may affect the compiler invocation, such as AC_MINIX. For example:

# For gnulib.

gl_INIT will in turn call the macros related with the gnulib functions, be it specific gnulib macros, like gl_FUNC_ALLOCA or Autoconf or Automake macros like AC_FUNC_ALLOCA or AM_FUNC_GETLINE. So there is no need to call those macros yourself when you use the corresponding gnulib modules.

You must also make sure that the gnulib library is built. Add the Makefile in the gnulib source base directory to AC_CONFIG_FILES, as in:

AC_CONFIG_FILES(... lib/Makefile ...)

You must also make sure that make will recurse into the gnulib directory. To achieve this, add the gnulib source base directory to a SUBDIRS statement, as in:


or if you, more likely, already have a few entries in SUBDIRS, you can add something like:

SUBDIRS += lib

Finally, you have to add compiler and linker flags in the appropriate source directories, so that you can make use of the gnulib library. Since some modules (‘getopt’, for example) may copy files into the build directory, top_builddir/lib is needed as well as top_srcdir/lib. For example:

AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
LDADD = lib/libgnu.a

Don’t forget to #include the various header files. In this example, you would need to make sure that ‘#include <string.h>’ is evaluated when compiling all source code files, that want to make use of strdup.

In the usual case where Autoconf is creating a config.h file, you should include config.h first, before any other include file. That way, for example, if config.h defines ‘restrict’ to be the empty string on a non-C99 host, or a macro like ‘_FILE_OFFSET_BITS’ that affects the layout of data structures, the definition is consistent for all include files. Also, on some platforms macros like ‘_FILE_OFFSET_BITS’ and ‘_GNU_SOURCE’ may be ineffective, or may have only a limited effect, if defined after the first system header file is included.

Finally, note that you cannot use AC_LIBOBJ or AC_REPLACE_FUNCS in your and expect the resulting object files to be automatically added to lib/libgnu.a. This is because your AC_LIBOBJ and AC_REPLACE_FUNCS invocations from augment a variable @LIBOBJS@ (and/or @LTLIBOBJS@ if using Libtool), whereas lib/libgnu.a is built from the contents of a different variable, usually @gl_LIBOBJS@ (or @gl_LTLIBOBJS@ if using Libtool).

Next: , Previous: , Up: Invoking gnulib-tool   [Contents][Index]