# Passing build variables using command line

**URL:** https://discourse.cmake.org/t/passing-build-variables-using-command-line/2530
**Category:** Usage
**Created:** [January 13, 2021, 6:58am UTC](https://discourse.cmake.org/t/passing-build-variables-using-command-line/2530 "2021-01-13T06:58:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![toothache](https://discourse.cmake.org/user_avatar/discourse.cmake.org/toothache/32/1098_2.png) [@toothache](https://discourse.cmake.org/u/toothache)
#### Post date: [January 13, 2021, 6:58am UTC](https://discourse.cmake.org/t/passing-build-variables-using-command-line/2530/1 "2021-01-13T06:58:20Z")

</div>

I’d like to override the cmake cached variable using the command line. But if a variable is repeatedly provided, does the last value take effect?

```auto
# what is the value of FOO?
> cmake -DFOO=123 -DFOO=321

```

Update:  
Test result shows that the last value takes effect. However, I couldn’t find any documentation describing such behavior. Is this behavior consistent across different OS? Should I write script based on this assumption?

---

<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: [January 13, 2021, 4:15pm UTC](https://discourse.cmake.org/t/passing-build-variables-using-command-line/2530/2 "2021-01-13T16:15:00Z")

</div>

This is easy to test:

```cmake
message("${foo}")

```

```auto
$ cmake -Dfoo=first -Dfoo=second -P foo.cmake
second

```

---

<div class="post-metadata">

### Author: ![toothache](https://discourse.cmake.org/user_avatar/discourse.cmake.org/toothache/32/1098_2.png) [@toothache](https://discourse.cmake.org/u/toothache)
#### Post date: [January 14, 2021, 1:00am UTC](https://discourse.cmake.org/t/passing-build-variables-using-command-line/2530/3 "2021-01-14T01:00:13Z")

</div>

Yes, the last takes place from my test as well. However, this behavior is not documented anywhere. I’m wondering if I can write script based on this assumption.

---

<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: [January 14, 2021, 1:32pm UTC](https://discourse.cmake.org/t/passing-build-variables-using-command-line/2530/4 "2021-01-14T13:32:24Z")

</div>

Given CMake’s backwards compatibility guarantees, it is likely something we’d need to guarantee in the future. Please open an issue for us to make sure it is in the documentation.

---

<div class="post-metadata">

### Author: ![toothache](https://discourse.cmake.org/user_avatar/discourse.cmake.org/toothache/32/1098_2.png) [@toothache](https://discourse.cmake.org/u/toothache)
#### Post date: [January 15, 2021, 1:30am UTC](https://discourse.cmake.org/t/passing-build-variables-using-command-line/2530/5 "2021-01-15T01:30:07Z")

</div>

Thanks! The issue has been created.

[Documentation: override the same build variable multiple times from command line (#21707) · Issues · CMake / CMake · GitLab (kitware.com)](https://gitlab.kitware.com/cmake/cmake/-/issues/21707)
