# Secondary Expansion in the cmake language

**URL:** https://discourse.cmake.org/t/secondary-expansion-in-the-cmake-language/1779
**Category:** Code
**Created:** [September 3, 2020, 12:54pm UTC](https://discourse.cmake.org/t/secondary-expansion-in-the-cmake-language/1779 "2020-09-03T12:54:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jmon12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jmon12/32/823_2.png) [@jmon12](https://discourse.cmake.org/u/jmon12)
#### Post date: [September 3, 2020, 12:54pm UTC](https://discourse.cmake.org/t/secondary-expansion-in-the-cmake-language/1779/1 "2020-09-03T12:54:17Z")

</div>

I have a simple question: is there **any concept of secondary expansion** (such as `!var` in bash or [GNU make secondary expansion](https://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html)) in the cmake language?

One usage example could be in the [second step of the cmake tutorial](https://github.com/Kitware/CMake/tree/master/Help/guide/tutorial/Step2). One could imagine such a line in the \*.h.in file for handling the versioning:

```
#define ${PROJECT_NAME}_VERSION_MAJOR @${PROJECT_NAME}_VERSION_MAJOR@

```

That of course doesn’t work because the second `${PROJECT_NAME}` is never expanded, but would be if I could specify a secondary expansion.

Thank you in advance! I’m relatively new to cmake, I maybe missed it in the doc.

---

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [September 3, 2020, 8:38pm UTC](https://discourse.cmake.org/t/secondary-expansion-in-the-cmake-language/1779/2 "2020-09-03T20:38:25Z")

</div>

Secondary expansions are supported by CMake. What you can do is perform a second expansion like this:

```cmake
message( "${${PROJECT_NAME}_VERSION_MINOR}" )

```

As you pointed out this does not work with the `@VAR@` form being the outer replacement. I suspect this is an oversight.

Note that for `configure_file` this behaviour is not documented and might be changed in the future.

I suggest you to do the second expansion yourself:

```cmake
configure_file( f.in f.tmp )
configure_file( ${CMAKE_CURRENT_BINARY_DIR}/f.tmp f.out )

```

Take care, though, as the order of the commands is important.

---

<div class="post-metadata">

### Author: ![jmon12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jmon12/32/823_2.png) [@jmon12](https://discourse.cmake.org/u/jmon12)
#### Post date: [September 18, 2020, 8:28pm UTC](https://discourse.cmake.org/t/secondary-expansion-in-the-cmake-language/1779/3 "2020-09-18T20:28:20Z")

</div>

I’m sorry for my late answer!

Indeed I tested my (useless) test case, and the trivial secondary expansion is indeed supported. I was not expecting so much simplicity!

Thank you for your quick answer, and sorry for my (very!) delayed one.
