CheckVariableExists¶
This module provides a command to check whether a C variable exists.
Load this module in a CMake project with:
include(CheckVariableExists)
Commands¶
This module provides the following command:
- check_variable_exists¶
Checks once if a C variable exists:
check_variable_exists(<var> <variable>)
This command attempts to compile and link a test C program that references the specified C variable
<var>
. A boolean result of whether the check was successful is stored in an internal cache variable<variable>
.备注
Prefer using
CheckSymbolExists
orCheckSourceCompiles
instead of this command for more robust detection. This command performs a link-only check and doesn't detect whether a variable is also declared in system or library headers. Neither can it detect variables that might be defined as preprocessor macros.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_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: Basic Usage¶
In the following example, a check is performed whether the linker sees the
C variable tzname
and stores the check result in the
PROJECT_HAVE_TZNAME
internal cache variable:
include(CheckVariableExists)
check_variable_exists(tzname PROJECT_HAVE_TZNAME)
Example: Isolated Check With Linked Libraries¶
In the following example, this module is used in combination with the
CMakePushCheckState
module to link additional required library
using the CMAKE_REQUIRED_LIBRARIES
variable. For example, in a find
module, to check whether the Net-SNMP library has the
usmHMAC192SHA256AuthProtocol
array:
include(CheckVariableExists)
include(CMakePushCheckState)
find_library(SNMP_LIBRARY NAMES netsnmp)
if(SNMP_LIBRARY)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES ${SNMP_LIBRARY})
check_variable_exists(usmHMAC192SHA256AuthProtocol SNMP_HAVE_SHA256)
cmake_pop_check_state()
endif()
See Also¶
The
CheckSymbolExists
module to check whether a C symbol exists.