One thing that surprises me is that the --warn-uninitialized
flag does not issue a warning when ${X}
is used and the variable X
has been explicitly unset using unset(X)
or set(X)
. For example:
if(NOT DEFINED X)
message("'X' is undefined")
message("X=${X}") # warning: uninitialized variable 'X'
endif()
unset(X) # or set(X)
if(NOT DEFINED X)
message("'X' is undefined")
message("X=${X}") # no warning
endif()
The relevant output is:
'X' is undefined
CMake Warning (dev) at example.cmake:5 (message):
uninitialized variable 'X'
This warning is for project developers. Use -Wno-dev to suppress it.
X=
'X' is undefined
X=
It appears that the current behavior treats unset(X)
(or set(X)
) as if it initializes the variable X
, even though it is actually undefined. I find this behavior unintuitive and unhelpful. It also makes
--warn-uninitialized
inconsistent with if(DEFINED <variable>)
, as demonstrated above.
If the goal is to initialize a variable with an empty string, the appropriate and intuitive command should be set(<variable> "")
. Moreover, unset(X)
is not equivalent to set(<variable> "")
, because unsetting a variable may expose a cache variable with the same name. So it remains unclear to me how the command unset
can be logically associated with the concept of initialization.