# Define new variable in cmake configure script

**URL:** <https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480>\
**Category:** Code\
**Created:** [June 3, 2021, 1:46pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480 "2021-06-03T13:46:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 3, 2021, 1:46pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/1 "2021-06-03T13:46:36Z")

</div>

Hi,

Let suppose I have a file called `myconf.cmake.in` and inside it I have:

```cmake
set(MY_VAR ${CMAKE_SOURCE_DIR}) 

message(CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR})
message(MY_VAR: ${MY_VAR})

```

After doing `configure_file(${CMAKE_SOURCE_DIR}/myconf.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/myconf.cmake)` I can open configured `myconf.cmake` and I can see that `CMAKE_SOURCE_DIR` is set to a source dir (as it was expected) while `MY_VAR` is empty (unexpected for me).

Thus I think I have some problems when defining new variables in cmake configuring scripts.  
Is there a way to overcome this?

cmake 3.19.2

---

<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:** [June 3, 2021, 1:51pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/2 "2021-06-03T13:51:49Z")

</div>

Did you call `configure_file()` before or after setting `MY_VAR`? You should set the variable first, then call `configure_file()`.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [June 3, 2021, 1:54pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/3 "2021-06-03T13:54:15Z")

</div>

If I understand correctly, you want to do:

```auto
configure_file(${CMAKE_SOURCE_DIR}/myconf.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/myconf.cmake @ONLY)

```

And:

```auto
set(MY_VAR @CMAKE_SOURCE_DIR@) 

message(CMAKE_SOURCE_DIR: @CMAKE_SOURCE_DIR@)
message(MY_VAR: ${MY_VAR})

```

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 3, 2021, 1:54pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/4 "2021-06-03T13:54:55Z")

</div>

> [@kyle.edwards](#):
>
> Did you call `configure_file()` before or after setting `MY_VAR`? You should set the variable first, then call `configure_file()`.

Actually I want to set `MY_VAR` **inside configuring script**.

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 3, 2021, 1:58pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/5 "2021-06-03T13:58:32Z")

</div>

I think this is the same that I did. I just tried and I get:

```cmake
message(MY_VAR: )

```

`MY_VAR` is empty

---

<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:** [June 3, 2021, 1:59pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/6 "2021-06-03T13:59:52Z")

</div>

Ah, I get it now. Yes, @fenrir’s suggestion is correct.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [June 3, 2021, 2:00pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/7 "2021-06-03T14:00:22Z")

</div>

Did you add the `@ONLY` argument to the `configure_file` invocation?

Edit:  
Apparently now you did 😉

---

<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:** [June 3, 2021, 2:04pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/8 "2021-06-03T14:04:01Z")

</div>

> [@fenrir](#):
>
> Did you add the `@ONLY` argument to the `configure_file` invocation?

Yeah, the `@ONLY` argument is important here. Otherwise, `${MY_VAR}` gets replaced with the value of `${MY_VAR}` from the calling script, which in this case is empty because it hasn’t been set.

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 3, 2021, 2:19pm UTC](https://discourse.cmake.org/t/define-new-variable-in-cmake-configure-script/3480/9 "2021-06-03T14:19:26Z")

</div>

@fenrir, @kyle.edwards thank you!  
You were right.

Without `@ONLY` argument in `configure_file()` cmake tries to replace `MY_VAR` with the value of `MY_VAR` in assumption that it was previously set in my project. But it wasnt in my case and it set to an empty (I didn’t think about that)

So I needed to add `@ONLY` argument and make difference between `${MY_VAR}` and `@MY_VAR@` in configuration script

Thank you very much!
