# cmake command line -D and using DEFINED

**URL:** https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264
**Category:** Usage
**Created:** [November 30, 2020, 12:58pm UTC](https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264 "2020-11-30T12:58:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bradbell](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bradbell](https://discourse.cmake.org/u/bradbell)
#### Post date: [November 30, 2020, 12:58pm UTC](https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264/1 "2020-11-30T12:58:27Z")

</div>

I would like to be able to just define a cmake variable without giving it a value, similar to how the C preprocessor can define variables and check if the variable is defined.

1. It seems that I have to use `-D variable_name=`  
instead of just `-D variable_name` ?

2. It also seems that If I only check if the variable is defined, cmake-3.17.2  
posts a warning that the variable was not used while cmake-3.18.4 does  
not post the warning ?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 30, 2020, 1:49pm UTC](https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264/2 "2020-11-30T13:49:00Z")

</div>

CMake variables do not have such a state. They either have a value or are undefined. The value can be empty (which might be what you’re looking for). `-Dvariable_name=` will set it to the empty string.

> CMake differences

3.18 might consider the variable “used” in an `if (DEFINED)` check. I’m not sure what would have changed that.

---

<div class="post-metadata">

### Author: ![bradbell](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bradbell](https://discourse.cmake.org/u/bradbell)
#### Post date: [November 30, 2020, 2:14pm UTC](https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264/3 "2020-11-30T14:14:35Z")

</div>

Here is my test script that gives a warning with 3.17.2 and not 3.18.4. Note that it builds in place so use a temporary directory for the test.

```
#! /bin/bash -e
cat << EOF > CMakeLists.txt
PROJECT(cmake_defined)
IF( DEFINED variable )
    MESSAGE(STATUS "variable is defined")
ELSE( DEFINED variable )
    MESSAGE(STATUS "variable is not defined")
ENDIF( DEFINED variable )
EOF
cmake -D variable= .
```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 30, 2020, 2:15pm UTC](https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264/4 "2020-11-30T14:15:38Z")

</div>

I’m not sure this is a regression (we don’t guarantee when warnings fire), but thanks for the test case.

Cc: @kyle.edwards @brad.king

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [November 30, 2020, 3:49pm UTC](https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264/5 "2020-11-30T15:49:37Z")

</div>

The command-line syntax for adding a cache entry is `-DVAR=$value` for some `$value`, which may or may not be empty. I don’t think we define behavior for plain `-DVAR`.

> my test script that gives a warning with 3.17.2 and not 3.18.4

That script gives me a “Manually-specified variables were not used by the project” warning with 3.17.5, 3.18.5, and 3.19.1. Be sure to use a fresh tree each time because once the variable is in `CMakeCache.txt` the warning won’t appear again.

---

<div class="post-metadata">

### Author: ![bradbell](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bradbell](https://discourse.cmake.org/u/bradbell)
#### Post date: [November 30, 2020, 3:55pm UTC](https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264/6 "2020-11-30T15:55:02Z")

</div>

My mistake. I corrected my test case and got the warning in 3.18.4.

```
#! /bin/bash -e
if [-e CMakeCache.txt]
then
    rm CMakeCache.txt
fi
cat << EOF > CMakeLists.txt
PROJECT(cmake_defined)
IF( DEFINED variable )
    MESSAGE(STATUS "variable is defined")
ELSE( DEFINED variable )
    MESSAGE(STATUS "variable is not defined")
ENDIF( DEFINED variable )
EOF
cmake -D variable= .

```

Thanks

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [December 1, 2020, 1:50pm UTC](https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264/7 "2020-12-01T13:50:13Z")

</div>

> [@brad.king](#):
>
> I don’t think we define behavior for plain `-DVAR` .

The command line interface explicitly considers `-DVAR` an error.
