CheckIncludeFiles — CMake 4.1.4 Documentation

# CheckIncludeFiles

This module provides a command to check one or more C/C++ header files.

Load this module in a CMake project with:

    include(CheckIncludeFiles)

## Commands

This module provides the following command:

check_include_files

  Checks once whether one or more header files can be included together in source code:

      check_include_files(<includes> <variable> [LANGUAGE <language>])

  This command checks once whether the given `<includes>` list of header files exist and can be included together in a C or C++ source file. The result of the check is stored in an internal cache variable named `<variable>`. Specify the `<includes>` argument as a semicolon-separated list of header file names.

  If `LANGUAGE` is set, the specified compiler will be used to perform the check. Acceptable values are `C` and `CXX`. If not set, the C compiler will be used if enabled. If the C compiler is not enabled, the C++ compiler will be used if enabled.

  Variables Affecting the Check

  The following variables may be set before calling this command to modify the way the check is run:

  `CMAKE_REQUIRED_FLAGS`

    A space-separated string of additional flags to pass to the compiler. A semicolon-separated list will not work. The contents of `CMAKE_<LANG>_FLAGS` and its associated configuration-specific `CMAKE_<LANG>_FLAGS_<CONFIG>` variables are automatically prepended to the compiler command before the contents of this variable.

  `CMAKE_REQUIRED_DEFINITIONS`

    A semicolon-separated list of compiler definitions, each of the form `-DFOO` or `-DFOO=bar`. A definition for the name specified by the result variable argument of the check command is also added automatically.

  `CMAKE_REQUIRED_INCLUDES`

    A semicolon-separated list of header search paths to pass to the compiler. These will be the only header search paths used; the contents of the `INCLUDE_DIRECTORIES` directory property will be ignored.

  `CMAKE_REQUIRED_LINK_OPTIONS`

    Added in version 3.14.

    A semicolon-separated list of options to add to the link command (see `try_compile()` for further details).

  `CMAKE_REQUIRED_LIBRARIES`

    A semicolon-separated list of libraries to add to the link command. These can be the names of system libraries, or they can be Imported Targets (see `try_compile()` for further details).

  `CMAKE_REQUIRED_LINK_DIRECTORIES`

    Added in version 3.31.

    A semicolon-separated list of library search paths to pass to the linker (see `try_compile()` for further details).

  `CMAKE_REQUIRED_QUIET`

    Added in version 3.1.

    If this variable evaluates to a boolean true value, all status messages associated with the check will be suppressed.

  Added in version 3.12: The `CMAKE_REQUIRED_LIBRARIES` variable, if policy `CMP0075` is set to `NEW`.

## Examples

Checking one or more C headers and storing the check result in cache variables:

    include(CheckIncludeFiles)

    check_include_files(sys/socket.h HAVE_SYS_SOCKET_H)

    if(HAVE_SYS_SOCKET_H)
      # The <net/if.h> header on Darwin and BSD-like systems is not self-contained
      # and also requires <sys/socket.h>
      check_include_files("sys/socket.h;net/if.h" HAVE_NET_IF_H)
    else()
      check_include_files(net/if.h HAVE_NET_IF_H)
    endif()

The `LANGUAGE` option can be used to specify which compiler to use. For example, checking multiple `C++` headers, when both `C` and `CXX` languages are enabled in the project:

    include(CheckIncludeFiles)

    check_include_files("header_1.hpp;header_2.hpp" HAVE_HEADERS LANGUAGE CXX)

## See Also

- The `CheckIncludeFile` module to check for a single C header.

- The `CheckIncludeFileCXX` module to check for a single C++ header.