Next: , Previous: , Up: Tutorial   [Contents][Index]

4.3 Adding a check for a library

Suppose our program relies on an external library. We want to be able to check for this when configure is run, and print an error message if the library is not installed. This can be done by amending configure.ac to the following:

AC_INIT([hello], [1.0])
AM_INIT_AUTOMAKE
AC_PROG_CC

AC_CHECK_LIB([gmp], [__gmpz_add])
if test "x$ac_cv_lib_gmp___gmpz_add" = xno; then
  AC_MSG_ERROR([libgmp is required])
fi

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

The first argument to AC_CHECK_LIB is the name of the library to look for, and the second a symbol of a function appearing in that library. We have used the GMP library as an example.

In this case the result of the check is available in the ac_cv_lib_gmp___gmpz_add shell variable. Shell variables holding the results of tests are known as cache variables. (The ‘ac_cv_’ prefix stands for “Autoconf cache variable”.)

This example shows that shell script code can appear in configure.ac. The conditional appearing after AC_CHECK_LIB checks the result of the library check and, if the library was not found, the AC_MSG_ERROR macro terminates the configure script with an error message. We need to use the ‘test’ shell command instead of the ‘[’ command because ‘[’ has a special meaning as a quote character. We prefix the letter ‘x’ to the strings being compared to account for the case that the variable expands to an empty string.