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.

The following shows how you can do this by amending We have used the GMP library as an example.

AC_INIT([hello], [1.0])

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


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.

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 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 stops the configure script and prints an error message.

We needed to use the ‘test’ shell command in this example instead of the ‘[’ command because ‘[’ has a special meaning as a quote character. We prefixed the letter ‘x’ to the strings being compared to account for the case that the variable expands to an empty string.