CheckTypeSize

Check sizeof a type

check_type_size
check_type_size(<type> <variable> [BUILTIN_TYPES_ONLY]
                                  [LANGUAGE <language>])

Check if the type exists and determine its size. Results are reported in the following variables:

HAVE_<variable>

Holds a true or false value indicating whether the type exists.

<variable>

Holds one of the following values:

<size>

Type has non-zero size <size>.

0

Type has architecture-dependent size. This may occur when CMAKE_OSX_ARCHITECTURES has multiple architectures. In this case <variable>_CODE contains C preprocessor tests mapping from each architecture macro to the corresponding type size. The list of architecture macros is stored in <variable>_KEYS, and the value for each key is stored in <variable>-<key>.

"" (empty string)

Type does not exist.

<variable>_CODE

Holds C preprocessor code to define the macro <variable> to the size of the type, or to leave the macro undefined if the type does not exist.

The options are:

BUILTIN_TYPES_ONLY

Support only compiler-builtin types. If not given, the macro checks for headers <sys/types.h>, <stdint.h>, and <stddef.h>, and saves results in HAVE_SYS_TYPES_H, HAVE_STDINT_H, and HAVE_STDDEF_H. The type size check automatically includes the available headers, thus supporting checks of types defined in the headers.

LANGUAGE <language>

Use the <language> compiler to perform the check. Acceptable values are C and CXX.

Despite the name of the macro you may use it to check the size of more complex expressions, too. To check e.g. for the size of a struct member you can do something like this:

check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)

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

CMAKE_REQUIRED_FLAGS

String of additional flags to pass to the compiler. The string must be space-delimited--a ;-list will not work. The contents of CMAKE_<LANG>_FLAGS and its associated configuration-specific variable are automatically added to the compiler command before the contents of CMAKE_REQUIRED_FLAGS.

CMAKE_REQUIRED_DEFINITIONS

A ;-list of compiler definitions of the form -DFOO or -DFOO=bar. A definition for the name specified by <resultVar> will also be added automatically.

CMAKE_REQUIRED_INCLUDES

A ;-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

New in version 3.14.

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

CMAKE_REQUIRED_LIBRARIES

A ;-list of libraries to add to the link command. These can be the name of system libraries or they can be Imported Targets (see try_compile() for further details).

CMAKE_REQUIRED_QUIET

New in version 3.1.

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

CMAKE_EXTRA_INCLUDE_FILES

list of extra headers to include.