Comparison Operators and Simple Checks
Let's learn about comparing values, simple checks, and filesystems in CMake.
We'll cover the following...
CMake assumes that the user is asking if the variable is defined (and is not explicitly false
). Luckily, we can explicitly check that fact (and not worry about the value inside):
if(DEFINED <name>)if(DEFINED CACHE{<name>})if(DEFINED ENV{<name>})
Comparing values
Comparison operations are supported with the following operators:
EQUAL
, LESS
, LESS_EQUAL
, GREATER
, and GREATER_EQUAL
They can be used to compare numeric values, like so:
if (1 LESS 2)
Note: The CMake documentation states that if one of the operands is not a number, the value will be
false
. But practical experiments show that the comparison of strings starting with a number works correctly. For example:if (20 EQUALS "20 GB")
.
We can compare software versions following ...