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


3.10 Using Gnulib for both a library and a program

Your project might build both a library and some accompanying programs in the same source tree. In that case you might want to use different modules for the library than for the programs. Typically the programs might want to make use of getopt-posix or version-etc, while the library wants to stay clear of these modules for technical or licensing reasons.

Let’s assume that your project contains a lib directory where the source of the library resides and a src directory for the sources of the programs as follows.

.
|-- configure.ac
|-- lib
|   |-- foo.c
|   `-- Makefile.am
|-- Makefile.am
`-- src
    |-- bar.c
    `-- Makefile.am

You can now add two instances of Gnulib to your project in separate source trees:

~/src/libfoo$ gnulib-tool --import --lib=libgnu --source-base=gnulib \
              --m4-base=gnulib/m4 --macro-prefix=gl strndup
~/src/libfoo$ gnulib-tool --import --lib=libgnutools \
              --source-base=src/gnulib --m4-base=src/gnulib/m4 \
              --macro-prefix=gl_tools getopt-gnu

The first one will import the module strndup in gnulib and the second one will import getopt-gnu in src/gnulib and you will end up with the following source tree (many files omitted in the interest of brevity):

.
|-- configure.ac
|-- gnulib
|   |-- m4
|   |-- strndup.c
|-- lib
|   |-- foo.c
|   `-- Makefile.am
|-- Makefile.am
`-- src
    |-- bar.c
    |-- gnulib
    |   |-- getopt.c
    |   |-- getopt.in.h
    |   |-- m4
    `-- Makefile.am

As discussed in Bundling the unit tests of the Gnulib modules, you may not use ‘--with-tests’ for this project since the configure.ac is shared.

Integration with your code is basically the same as outlined in Initial import with the one exception that you have to add both the macro gl_EARLY and the macro gl_tools_EARLY to your configure.ac (and of course also both macros gl_INIT and gl_tools_INIT). Obviously the name of the second macro is dependent on the value of the --macro-prefix option in your gnulib-tool invocation.

...
AC_PROG_CC
gl_EARLY
gl_tools_EARLY
...
# For gnulib.
gl_INIT
gl_tools_INIT
...

Also as outlined in Initial import you will have to add compiler and linker flags. For the library you might have to add something along the line of the following to your Makefile.am:

...
AM_CPPFLAGS = -I$(top_srcdir)/gnulib -I$(top_builddir)/gnulib
...
libfoo_la_LIBADD = $(top_builddir)/gnulib/libgnu.la
...

Correspondingly for the programs you will have to add something like this:

...
AM_CPPFLAGS = -I$(top_srcdir)/src/gnulib -I$(top_builddir)/src/gnulib
...
LDADD = $(top_builddir)/src/gnulib/libgnutools.la
...

The name of the library that you have pass in the linker option depends on the --lib option in gnulib-tool invocation.


Next: Caveat: gettextize and autopoint users, Previous: Building directly from the top-level directory, Up: Invoking gnulib-tool   [Contents][Index]