CheckTypeSize

This module provides a command to check the size of a C/C++ type or expression.

在 CMake 專案中使用以下命令載入該模組:

include(CheckTypeSize)

命令

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 named HAVE_<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 in HAVE_SYS_TYPES_H, HAVE_STDINT_H, and HAVE_STDDEF_H internal cache variables. For C++ std:: types, <cstdint> and <cstddef> are also checked with HAVE_CSTDINT and HAVE_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 are C and CXX. If not specified, it defaults to C.

結果變數

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 the RESULT_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 as HAVE_<size-var>). In this case, the HAVE_<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 is 0) 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 is 0) and contains a list of architecture macros. The value for each key is stored in <size-var>-<key> variables.

影響檢查的變數

可以在呼叫此命令之前設定以下變數,以修改檢查的執行方式:

CMAKE_REQUIRED_FLAGS

一個以空格分隔的字串,包含要傳遞給編譯器的額外旗標。分號分隔列表將無法運作。CMAKE_<LANG>_FLAGS 及其關聯的特定組態 CMAKE_<LANG>_FLAGS_<CONFIG> 變數的內容會在該變數的內容之前自動預置到編譯器命令中。

CMAKE_REQUIRED_DEFINITIONS

一個編譯器定義的分號分隔列表,每個定義的形式為 -DFOO-DFOO=bar。由檢查命令的結果變數引數所指定的名稱的定義也會自動新增。

CMAKE_REQUIRED_INCLUDES

一個要傳遞給編譯器的標頭搜尋路徑的分號分隔列表。這些將是唯一使用的標頭搜尋路徑;INCLUDE_DIRECTORIES 目錄屬性的內容將被忽略。

CMAKE_REQUIRED_LINK_OPTIONS

在 3.14 版被加入.

一個要新增到連結命令的選項的分號分隔列表(詳情請參見 try_compile())。

CMAKE_REQUIRED_LIBRARIES

一個要新增到連結命令的程式庫的分號分隔列表。這些可以是系統程式庫的名稱,也可以是 Imported Targets`(詳情請參見 :command:`try_compile)。

CMAKE_REQUIRED_LINK_DIRECTORIES

在 3.31 版被加入.

一個要傳遞給連結器的程式庫搜尋路徑的分號分隔列表(詳情請參見 try_compile())。

CMAKE_REQUIRED_QUIET

在 3.1 版被加入.

如果此變數的值為布林真值,則與檢查相關的所有狀態訊息都將被抑制。

CMAKE_EXTRA_INCLUDE_FILES

A 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 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()