GNU build system: add a library through pkg-config
This article shows how to add a library to a GNU build system project by using the pkg-config tool.
The pkg-config tool is a tool which was developed in order to provide a simple and unified way to check if a given library is available in a system, verify it's version and add the necessary compiler flags for your software package to be able to build successfully.
How to add a library through pkg-config
To start off, let's pick up the GNU build system project which was set in the shallow topology tutorial and let's add a library to it. In this example we will add support for the libsigc++ library but any other library, provided that a valid .pc file was provided during it's installation, will work just as well.
The task of adding a library to a GNU build system project through the use of the pkg-config tool consists of the following tasks:
- Add a valid PKG_CHECK_MODULESmacro to the configure.acfile
- Add a set of library flags to the relevant Makefile.am files, which, in this example, is the ./src/Makefile.am file
- rebuild the configure script
The PKG_CHECK_MODULES macro is only present in a system if the pkg-config tool has been previously installed. The macro assumes the following form:
- PKG_CHECK_MODULES( [VARIABLE-PREFIX], [MODULES])
In the example above, the VARIABLE-PREFIX refers to the prefix used to define a set of M4 variables which are used by the GNU build system routines to refer to this particular library, while the MODULES consists of a set of tests performed on a set of modules in order to check if valid versions are present in the system. In this example, the entry on the configure.ac file will assume the following form:
AC_INIT([project_name], [0.1], [email_address@server])
PKG_CHECK_MODULES([SIGCPP], [sigc++-2.2 >= 2.2.8])
AC_CONFIG_FILES([Makefile src/Makefile]) AC_OUTPUT
In the above example the string 'SIGCPP was defined as the prefix used by the set of M4 variables used to refer to this particular library, while the test performed by pkg-config on this library consists of checking if the version of the libsigc++ library present in the system is 2.2.8 or greater. It is also important to point out that this particular library, libsigc++, releases this library with a library name which assumes the following form:
- libsigc++-<MAJOR VERSION>.<MINOR VERSION>
This is a convenient way to guarantee that multiple versions of this particular library can exist in a given system without causing any of the problems associated with the so called dependency hell. Nonetheless, this isn't a standard name scheme nor is it enforced by the pkg-config tool.
When a valid PKG_CHECK_MODULES macro is present in a given configure.ac file, autoconf generates a set of useful variables which may be used by other routines to be able to perform a set of tasks. Among those tasks there are the fundamental tasks of adding libraries to the build process and specifying the set of compiler flags that goes with them. In order to add these tasks to our project we tweak the Makefile.am file:
bin_PROGRAMS = project_name
project_name_SOURCES = main.c++
project_name_CFLAGS = $(SIGCPP_CFLAGS) project_name_LDADD = $(SIGCPP_LIBS)
Rebuild the configure script
Finally, in order to apply all changes, we need to rebuild the configure script. We do that by running the following commands:
Now, the configure script performs the necessary tests on libsigc++ and, if all tests are successful, a Makefile is generated which is able to include the libsigc++ library when building the project.