# cmake -D \<var\>:\<type\>=\<value\>

**URL:** https://discourse.cmake.org/t/cmake-d-var-type-value/1135
**Category:** Usage
**Created:** [May 1, 2020, 2:16pm UTC](https://discourse.cmake.org/t/cmake-d-var-type-value/1135 "2020-05-01T14:16:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Dixon](https://discourse.cmake.org/user_avatar/discourse.cmake.org/david_dixon/32/127_2.png) [@David\_Dixon](https://discourse.cmake.org/u/David_Dixon)
#### Post date: [May 1, 2020, 2:16pm UTC](https://discourse.cmake.org/t/cmake-d-var-type-value/1135/1 "2020-05-01T14:16:26Z")

</div>

As far as I can recall, I have only passed command line arguments to cmake with `cmake -D <var>=<value>`. However, I just discovered that people on my team do the following `cmake -D <var>:<type>=<value>`. I knew that this form was possible but I didn’t realize that it was still convention for some cmake users. Are there reasons to prefer one over thew other? Is this legacy cmake usage?

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [May 1, 2020, 2:27pm UTC](https://discourse.cmake.org/t/cmake-d-var-type-value/1135/2 "2020-05-01T14:27:31Z")

</div>

If the project never defines `<var>` through `set(... CACHE)`, you are effectively defining a variable that didn’t exist before. Setting the `<type>` determines the type that’s stored in the cache, which in turn changes how it’s presented in the Curses and Qt GUIs. It also affects the `TYPE` property of the cache entry, and if the project happens to be reading the `TYPE` property (though from my experience, most projects don’t), that could affect the outcome. (Of course, none of this matters for `-P` scripts.)

Basically, you can do one or the other. Including the type has some slight advantages over not including it, and is generally preferred, but not including it generally won’t hurt anything except under some very specific corner cases. (FWIW, I often don’t include it.)

---

<div class="post-metadata">

### Author: ![anon45792294](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@anon45792294](https://discourse.cmake.org/u/anon45792294)
#### Post date: [May 7, 2020, 5:00pm UTC](https://discourse.cmake.org/t/cmake-d-var-type-value/1135/3 "2020-05-07T17:00:07Z")

</div>

From Craig Scott’s book “Professional Cmake: A Practical Guide” chapter 5.

Basically there are 2 reasons:

**Number 1:**  
"There is a special case for handling values initially declared without a type on the cmake command line. If the project’s CMakeLists.txt file then tries to set the same cache variable and specifies a type of FILEPATH or PATH, then if the value of that cache variable is a relative path, CMake will treat it as being relative to the directory from which cmake was invoked and automatically convert it to an absolute path. This is not particularly robust, since cmake could be invoked from any directory, not just the build directory. Therefore, developers are advised to always include a type if specifying a variable on the cmake command line for a variable that represents some kind of path. "

**Number 2**  
“It is a good habit to always specify the type of the variable on the command line in general anyway so that it is likely to be shown in GUI applications in the most appropriate form.”
