Previous: Particular Headers, Up: Header Files


5.6.3 Generic Header Checks

These macros are used to find system header files not covered by the “particular” test macros. If you need to check the contents of a header as well as find out whether it is present, you have to write your own test for it (see Writing Tests).

— Macro: AC_CHECK_HEADER (header-file, [action-if-found], [action-if-not-found], [includes])

If the system header file header-file is compilable, execute shell commands action-if-found, otherwise execute action-if-not-found. If you just want to define a symbol if the header file is available, consider using AC_CHECK_HEADERS instead.

includes is decoded to determine the appropriate include directives. If omitted or empty, configure will check for both header existence (with the preprocessor) and usability (with the compiler), using AC_INCLUDES_DEFAULT for the compile test. If there is a discrepancy between the results, a warning is issued to the user, and the compiler results are favored (see Present But Cannot Be Compiled). In general, favoring the compiler results means that a header will be treated as not found even though the file exists, because you did not provide enough prerequisites.

Providing a non-empty includes argument allows the code to provide any prerequisites prior to including the header under test; it is common to use the argument AC_INCLUDES_DEFAULT (see Default Includes). With an explicit fourth argument, no preprocessor test is needed. As a special case, an includes of exactly ‘-’ triggers the older preprocessor check, which merely determines existence of the file in the preprocessor search path; this should only be used as a last resort (it is safer to determine the actual prerequisites and perform a compiler check, or else use AC_PREPROC_IFELSE to make it obvious that only a preprocessor check is desired).

— Macro: AC_CHECK_HEADERS (header-file..., [action-if-found], [action-if-not-found], [includes])

For each given system header file header-file in the blank-separated argument list that exists, define HAVE_header-file (in all capitals). If action-if-found is given, it is additional shell code to execute when one of the header files is found. You can give it a value of ‘break’ to break out of the loop on the first match. If action-if-not-found is given, it is executed when one of the header files is not found.

includes is interpreted as in AC_CHECK_HEADER, in order to choose the set of preprocessor directives supplied before the header under test.

Previous versions of Autoconf merely checked whether the header was accepted by the preprocessor. This was changed because the old test was inappropriate for typical uses. Headers are typically used to compile, not merely to preprocess, and the old behavior sometimes accepted headers that clashed at compile-time (see Present But Cannot Be Compiled). If you need to check whether a header is preprocessable, you can use AC_PREPROC_IFELSE (see Running the Preprocessor).

Actually requiring a header to compile improves the robustness of the test, but it also requires that you make sure that headers that must be included before the header-file be part of the includes, (see Default Includes). If looking for bar.h, which requires that foo.h be included before if it exists, we suggest the following scheme:

AC_CHECK_HEADERS([foo.h])
AC_CHECK_HEADERS([bar.h], [], [],
[#ifdef HAVE_FOO_H
# include <foo.h>
#endif
])

The following variant generates smaller, faster configure files if you do not need the full power of AC_CHECK_HEADERS.

— Macro: AC_CHECK_HEADERS_ONCE (header-file...)

For each given system header file header-file in the blank-separated argument list that exists, define HAVE_header-file (in all capitals). This is a once-only variant of AC_CHECK_HEADERS. It generates the checking code at most once, so that configure is smaller and faster; but the checks cannot be conditionalized and are always done once, early during the configure run. Thus, this macro is only safe for checking headers that do not have prerequisites beyond what AC_INCLUDES_DEFAULT provides.