Difference between -D <var>=<value> and -D <var>:<type>=<value>
(I'm using CMake 3.17.3 and I haven't checked this on other versions.)
It seems there is a difference in how -D <var>=<value>
and -D <var>:<type>=<value>
interact with cache, while I don't see this in documentation.
For example, let's take a simple CMakeLists.txt
:
cmake_minimum_required(VERSION 3.17.3)
project(Example
LANGUAGES CXX
)
set(VARIABLE "default" CACHE STRING "Test cache variable")
Now, we configure it using the following commands:
cmake -S "." -B "./build"
cmake -D "VARIABLE=non-default" -S "." -B "./build"
cmake -D "VARIABLE:STRING=non-default" -S "." -B "./build"
Command 1, in CMakeCache.txt
we will find:
//Test cache variable
VARIABLE:STRING=default
which is fully expected.
Command 2, in CMakeCache.txt
we will find:
//Test cache variable
VARIABLE:STRING=non-default
which is fully expected.
Command 3, in CMakeCache.txt
we will find:
//No help, variable specified on the command line.
VARIABLE:STRING=non-default
which is no expected since the docstring is lost.
Furthermore, the configuration ends with:
CMake Warning:
Manually-specified variables were not used by the project:
VARIABLE
It seems that if the -D <var>:<type>=<value>
form is used (as opposed to -D <var>=<value>
) a new cache variable is created and overrides what CMakeLists.txt
specifies. I expect this will work normally since the variable name is the same and other properties of a cache variable typically don't matter for the build itself.
Furthermore, -D
provided entires trigger a warning if they end up unused while overriding the value of a cache variable counts as "used" even if the cache variable isn't really used.
Is this difference between -D <var>=<value>
and -D <var>:<type>=<value>
a feature or bug? Is it documented anywhere?