From: http://www.openismus.com/documents/linux/using_libraries/using_libraries
Using C/C++ libraries with Automake and Autoconf
Contents
Introduction
If you have read Using Automake and Autoconf with C++ then you already know how to use automake and autoconf to build your C or C++ applications. This document will show you what you need to add to those configure.ac and Makefile.am files to link your code with a shared library.
An example is included, which links to the examplelib library used in Building C/C++ libraries with Automake and Autoconf.
CFLAGS and LIBS
The Makefile needs two pieces of information — how to find the library's header files and how to link to the library itself. These are traditionally stored in variables ending in CFLAGS (for the headers' include argument) and LIBS (for the linker argument). For instance, PROJECT_CFLAGS and PROJECT_LIBS. These variables will be set in the configure.ac file.
configure.ac
Your configure.ac script should find the library and set the CFLAGS and LIBS variables:
Libraries which have installed a pkg-config .pc file
Most modern libraries use pkg-config to provide includes and linker information, instead of the methods described below. In this case, you should use the PKG_CHECK_MODULES() macro in your configure.ac file. For instance:
PKG_CHECK_MODULES([DEPS], [gtkmm-2.4 >= 2.12.2 somethingelse-1.0 >= 1.0.2])Of course you must ensure that you have installed pkg-config.
DEPS_CFLAGS and DEPS_LIBS will then include the include and linker options for the library and all of its dependencies, for you to use in your Makefile.am file. The DEPS prefix means 'dependencies', but you can use any prefix that you like. Notice that you can get information about several libraries at once, putting all of the information into one set of _CFLAGS and _LIBS variables.
You could also use more than one PKG_CHECK_MODULES() line to put information about different sets of libraries in separate _CFLAGS and _LIBS variables. However, doing so should be avoided because then pkg-config will not be able to strip redundant flags, nor can it ensure that dependent libraries are specified in the correct order. If you need to build multiple binaries with different sets of dependencies, call PKG_CHECK_MODULES() once for each target with the full list of dependencies of that target.
Libraries without pkg-config files
There are still libraries which do not use pkg-config files to make your life easier. The best thing to do in this situation is to allow the user to tell the configure script where to find the library. You can do this with the AC_ARG_WITH macro. This adds a command line argument to the configure script. It is important to be able to individually set the library and include paths, as the includes are architecture-independent whereas the libraries are compiled files and therefore dependent on the architecture.
For instance:
# Get MySQL library and include locations AC_ARG_WITH([mysql-include-path], [AS_HELP_STRING([--with-mysql-include-path], [location of the MySQL headers, defaults to /usr/include/mysql])], [MYSQL_CFLAGS="-I$withval"], [MYSQL_CFLAGS='-I/usr/include/mysql']) AC_SUBST([MYSQL_CFLAGS]) AC_ARG_WITH([mysql-lib-path], [AS_HELP_STRING([--with-mysql-lib-path], [location of the MySQL libraries])], [MYSQL_LIBS="-L$withval -lmysqlclient"], [MYSQL_LIBS='-lmysqlclient']) AC_SUBST([MYSQL_LIBS])
Makefile.am
The CFLAGS and LIBS variables are used in your Makefile.am files.
For programs
If you are using the library in a program, then you should do something like the following:
AM_CPPFLAGS = $(DEPS_CFLAGS) program_LDADD = $(DEPS_LIBS)Note that "CPP" is short for "C preprocessor", not C++. The preprocessor flags are used for both C and C++ compilation. The variable AM_CPPFLAGS is used to change the default preprocessor options for all objects built by this Makefile.am. It is generally advisable to use the same compiler options for all object files. Library dependencies should be target-specific, however, which is why program_LDADD was used here. The corresponding variable for the default value, if you do want to change it, would be LDADD (not AM_LDADD).
For libraries
If you are using the library from another library, then you should do something like the following. This will not actually link with a shared library — it just tells your library that it needs to link with the other library at runtime.
libsomething_la_CPPFLAGS = $(DEPS_CFLAGS) libsomething_la_LIBADD = $(DEPS_LIBS)Note that this example assumes you are using libtool for cross-platform support for building shared libraries.
Example Files
You may download this example which demonstrates how to link an executable to the example library used in Building C/C++ libraries with Automake and Autoconf, or browse the source at theGitorious project page.