This module provides a command to check the size of a C/C++ type or expression.
Load this module in a CMake project with:
include(CheckTypeSize)
This module provides the following command:
Checks once whether the C/C++ type or expression exists and determines its size:
check_type_size(<type> <variable> [BUILTIN_TYPES_ONLY] [LANGUAGE <language>])
The arguments are:
<type>The type or expression being checked.
<variable>The name of the variable and a prefix used for storing the check results.
BUILTIN_TYPES_ONLYIf given, only compiler-builtin types will be supported in the check.
If not given, the command checks for common headers <sys/types.h>,
<stdint.h>, and <stddef.h>, and saves results in
HAVE_SYS_TYPES_H, HAVE_STDINT_H, and HAVE_STDDEF_H internal
cache variables. The type size check automatically includes the available
headers, thus supporting checks of types defined in the headers.
LANGUAGE <language>Uses the <language> compiler to perform the check.
Acceptable values are C and CXX.
If not specified, it defaults to C.
Result Variables
Results are reported in the following variables:
HAVE_<variable>Internal cache variable that holds a boolean true or false value
indicating whether the type or expression <type> exists.
<variable>Internal cache variable that holds one of the following values:
<size>If the type or expression exists, it will have a non-zero size
<size> in bytes.
0When type has architecture-dependent size; This may occur when
CMAKE_OSX_ARCHITECTURES has multiple architectures.
In this case <variable>_CODE contains 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>.
When type or expression does not exist.
<variable>_CODECMake variable that holds 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.
Despite the name of this command, it may also be used to determine the size of more complex expressions. For example, to check the size of a struct member:
check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
Variables Affecting the Check
The following variables may be set before calling this command to modify the way the check is run:
CMAKE_REQUIRED_FLAGSA 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_DEFINITIONSA 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_INCLUDESA 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_OPTIONSAdded in version 3.14.
A semicolon-separated list of options to
add to the link command (see try_compile() for further details).
CMAKE_REQUIRED_LIBRARIESA 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_DIRECTORIESAdded 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_QUIETAdded 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_FILESA semicolon-separated list of extra header files to include when performing the check.
Consider the code:
include(CheckTypeSize)
# Check for size of long.
check_type_size(long SIZEOF_LONG)
message("HAVE_SIZEOF_LONG: ${HAVE_SIZEOF_LONG}")
message("SIZEOF_LONG: ${SIZEOF_LONG}")
message("SIZEOF_LONG_CODE: ${SIZEOF_LONG_CODE}")
On a 64-bit architecture, the output may look something like this:
HAVE_SIZEOF_LONG: TRUE
SIZEOF_LONG: 8
SIZEOF_LONG_CODE: #define SIZEOF_LONG 8
On Apple platforms, when CMAKE_OSX_ARCHITECTURES has multiple
architectures, types may have architecture-dependent sizes.
For example, with the code
include(CheckTypeSize)
check_type_size(long SIZEOF_LONG)
message("HAVE_SIZEOF_LONG: ${HAVE_SIZEOF_LONG}")
message("SIZEOF_LONG: ${SIZEOF_LONG}")
foreach(key IN LISTS SIZE_OF_LONG_KEYS)
message("key: ${key}")
message("value: ${SIZE_OF_LONG-${key}}")
endforeach()
message("SIZEOF_LONG_CODE:
${SIZEOF_LONG_CODE}")
the result may be:
HAVE_SIZEOF_LONG: TRUE
SIZEOF_LONG: 0
key: __i386
value: 4
key: __x86_64
value: 8
SIZEOF_LONG_CODE:
#if defined(__i386)
# define SIZE_OF_LONG 4
#elif defined(__x86_64)
# define SIZE_OF_LONG 8
#else
# error SIZE_OF_LONG unknown
#endif