2

Looking at CMake documentation for the command line options, there's some ambiguity in the -D option used to define a variable.

In the command line synopsis there's

-D<var>=<value>

While in the option description we read:

-D <var>:<type>=<value>

The two entries differ by a space between -D and the variable definition, and by the presence/absence of :<type>.

Does the space make any difference? What is the difference between specifying or not the type? Is it cached in any case?

From a quick test, it seems the space is optional. While if the variable type is not specified, the variable goes in CMakeCache.txt as:

//No help, variable specified on the command line.
MYVARIABLENAME:UNINITIALIZED=MYVARIABLEVALUE

And it will not appear in the cache of cmake-gui.

Are these behaviours documented anywhere?


Edit:
I have reported an issue in CMake bug tracker. As visible in the link, it seems that some solution was already on its way, and documentation should be fixed for CMake 3.3!.
Antonio
  • 19,451
  • 13
  • 99
  • 197

1 Answers1

1

I don't believe the behaviour you're seeing is documented, but your conclusions are generally correct.

If the type is not specified, then the cached entry has type UNINITIALIZED. Since CMake isn't a strongly-typed language, you can use this variable as any type you require within the CMakeLists.txt - a string, path, list, etc. However, unless you explicitly change the type in your CMakeLists.txt (e.g. using a set call) its type will remain UNINITIALIZED as far as the cached value goes.

As far as I know, the type is really only useful if you're using the CMake GUI so it can appropriately choose which sort of input box to use for the variable, or whether to show it to the user at all or not.

As for the space - this is a more awkward situation. I have another answer which goes into this in a bit of detail, but basically, the space should have no effect, but it does under certain circumstances. I recommend not putting a space after -D arguments.

Community
  • 1
  • 1
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • 1
    Thanks! So, I see in general if the type is not chosen, the variable does not show up in the cmake-gui, not even as advanced (yet, it is cached in CMakeCache.txt). However I am puzzled by some variable for which I do not specify a type, I use in several part in my code, it is still stored in CMakeCache.txt as UNITIALIZED, but magically appears in the cmake-gui. I will investigate more when I have a bit of time. – Antonio May 19 '15 at 08:17
  • Oh - OK. I didn't know that, but I don't use the GUI generally. – Fraser May 19 '15 at 08:55
  • See question update, it seems that a fix is on its way! Curiously, the documentation from now on will show consistently to put a space in between arguments, although stating that also the "only one argument" (=no space) option is possible. – Antonio May 20 '15 at 12:47