CheckStructHasMember — CMake 4.1.4 Documentation

# CheckStructHasMember

This module provides a command to check whether a struct or class has a specified member variable.

Load this module in a CMake project with:

    include(CheckStructHasMember)

## Commands

This module provides the following command:

check_struct_has_member

  Checks once if the given struct or class has the specified member variable:

      check_struct_has_member(
        <struct>
        <member>
        <headers>
        <variable>
        [LANGUAGE <language>]
      )

  This command checks once whether the struct or class `<struct>` contains the specified `<member>` after including the given header(s) `<headers>` where the prototype should be declared. Multiple header files can be specified in one argument as a string using a semicolon-separated list. The result is stored in an internal cache variable `<variable>`.

  The options are:

  `LANGUAGE <language>`

    Use the `<language>` compiler to perform the check. Acceptable values are `C` and `CXX`. If not specified, it defaults to `C`.

  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.

## Examples

### Example: Checking C Struct Member

In the following example, this module checks if the C struct `timeval` has a member variable `tv_sec` after including the `<sys/select.h>` header. The result of the check is stored in the internal cache variable `HAVE_TIMEVAL_TV_SEC`.

    include(CheckStructHasMember)

    check_struct_has_member(
      "struct timeval"
      tv_sec
      sys/select.h
      HAVE_TIMEVAL_TV_SEC
    )

### Example: Checking C++ Struct Member

In the following example, this module checks if the C++ struct `std::tm` has a member variable `tm_gmtoff` after including the `<ctime>` header. The result of the check is stored in the internal cache variable `HAVE_TM_GMTOFF`.

    include(CheckStructHasMember)

    check_struct_has_member(
      std::tm
      tm_gmtoff
      ctime
      HAVE_TM_GMTOFF
      LANGUAGE CXX
    )

### Example: Isolated Check With Compile Definitions

In the following example, the check is performed with temporarily modified compile definitions using the `CMakePushCheckState` module:

    include(CheckStructHasMember)
    include(CMakePushCheckState)

    cmake_push_check_state(RESET)
      set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)

      check_struct_has_member(
        "struct utsname"
        domainname
        sys/utsname.h
        HAVE_UTSNAME_DOMAINNAME
      )
    cmake_pop_check_state()