CheckTypeSize¶
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)
Commands¶
This module provides the following command:
- check_type_size¶
Checks once whether the C/C++ type or expression exists and determines its size:
check_type_size( <type> <size-var> [RESULT_VARIABLE <result-var>] [BUILTIN_TYPES_ONLY] [LANGUAGE <language>] )
The arguments are:
<type>
The type or expression being checked.
<size-var>
The name of the internal cache variable for storing the size of the type or expression
<type>
. This name is also used as a prefix as explained below.RESULT_VARIABLE <result-var>
Добавлено в версии 4.2.
The name of the internal cache variable that holds a boolean value indicating whether the type or expression
<type>
exists. If not given, the command will by default define an internal cache variable namedHAVE_<size-var>
instead.BUILTIN_TYPES_ONLY
If 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 inHAVE_SYS_TYPES_H
,HAVE_STDINT_H
, andHAVE_STDDEF_H
internal cache variables. For C++std::
types,<cstdint>
and<cstddef>
are also checked withHAVE_CSTDINT
andHAVE_CSTDDEF
defined respectively. The command automatically includes the available headers in the type size check, thus supporting checks of types defined in the headers.LANGUAGE <language>
Uses the
<language>
compiler to perform the check. Acceptable values areC
andCXX
. If not specified, it defaults toC
.
Result Variables
Results are reported in the following variables:
<size-var>
Internal cache variable that holds one of the following values:
<size>
If the type or expression
<type>
exists, it will have a non-zero size<size>
in bytes.0
When the type has an architecture-dependent size; This may occur when
CMAKE_OSX_ARCHITECTURES
has multiple architectures. In this case also the<size-var>_KEYS
variable is defined and the<size-var>_CODE
variable contains preprocessor tests mapping as explained below.- "" (empty string)
When the type or expression
<type>
does not exist.
HAVE_<size-var>
Internal cache variable that holds a boolean value indicating whether the type or expression
<type>
exists. This variable is defined when theRESULT_VARIABLE
argument is not used.<result-var>
Добавлено в версии 4.2.
Internal cache variable defined when the
RESULT_VARIABLE
argument is used. It holds a boolean value indicating whether the type or expression<type>
exists (same value asHAVE_<size-var>
). In this case, theHAVE_<size-var>
variable is not defined.<size-var>_CODE
CMake variable that holds preprocessor code to define the macro
<size-var>
to the size of the type, or to leave the macro undefined if the type does not exist.When the type has an architecture-dependent size (
<size-var>
value is0
) this variable contains preprocessor tests mapping from each architecture macro to the corresponding type size.<size-var>_KEYS
CMake variable that is defined only when the type has an architecture-dependent size (
<size-var>
value is0
) and contains a list of architecture macros. The value for each key is stored in<size-var>-<key>
variables.
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-specificCMAKE_<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
Добавлено в версии 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
Добавлено в версии 3.31.
A semicolon-separated list of library search paths to pass to the linker (see
try_compile()
for further details).
CMAKE_REQUIRED_QUIET
Добавлено в версии 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
A semicolon-separated list of extra header files to include when performing the check.
Examples¶
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 SIZEOF_LONG_KEYS)
message("key: ${key}")
message("value: ${SIZEOF_LONG-${key}}")
endforeach()
message("SIZEOF_LONG_CODE:\n${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 SIZEOF_LONG 4
#elif defined(__x86_64)
# define SIZEOF_LONG 8
#else
# error SIZEOF_LONG unknown
#endif
Example: Configuration Header¶
The next example demonstrates how the result variables can be used in a configuration header:
include(CheckTypeSize)
check_type_size(long SIZEOF_LONG)
configure_file(config.h.in config.h @ONLY)
config.h.in
¶/* Define whether the type 'long' exists. */
#cmakedefine HAVE_SIZEOF_LONG
/* The size of 'long', as computed by sizeof. */
@SIZEOF_LONG_CODE@
Example: Checking Complex Expressions¶
Despite the name of this module, it may also be used to determine the size of more complex expressions. For example, to check the size of a struct member:
include(CheckTypeSize)
check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
Example: Isolated Check¶
In the following example, the check is performed with temporarily modified
additional headers using the CMAKE_EXTRA_INCLUDE_FILES
variable and
CMakePushCheckState
module. The result of the check is stored in
HAVE_SIZEOF_UNION_SEMUN
, and size is stored in SIZEOF_UNION_SEMUN
internal cache variables.
include(CheckTypeSize)
include(CMakePushCheckState)
cmake_push_check_state(RESET)
set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/ipc.h sys/sem.h)
check_type_size("union semun" SIZEOF_UNION_SEMUN)
cmake_pop_check_state()
Example: Customizing Result Variable¶
Since CMake 4.2, the HAVE_<size-var>
variable name can be customized
using the RESULT_VARIABLE
argument. In the following example, this
module is used to check whether the struct flock
exists, and the result
is stored in the MyProj_HAVE_STRUCT_FLOCK
internal cache variable:
cmake_minimum_required(VERSION 4.2)
# ...
include(CheckTypeSize)
include(CMakePushCheckState)
cmake_push_check_state(RESET)
set(CMAKE_EXTRA_INCLUDE_FILES "fcntl.h")
check_type_size(
"struct flock"
MyProj_SIZEOF_STRUCT_FLOCK
RESULT_VARIABLE MyProj_HAVE_STRUCT_FLOCK
)
cmake_pop_check_state()