# Refering to generated files specified in top level CMakeList.txt from sub\_directory CMakeList.txt

**URL:** https://discourse.cmake.org/t/refering-to-generated-files-specified-in-top-level-cmakelist-txt-from-sub-directory-cmakelist-txt/9623
**Category:** Usage
**Created:** [December 13, 2023, 1:57pm UTC](https://discourse.cmake.org/t/refering-to-generated-files-specified-in-top-level-cmakelist-txt-from-sub-directory-cmakelist-txt/9623 "2023-12-13T13:57:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Thorbjorn\_Willoch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thorbjorn_willoch/32/1560_2.png) [@Thorbjorn\_Willoch](https://discourse.cmake.org/u/Thorbjorn_Willoch)
#### Post date: [December 13, 2023, 1:57pm UTC](https://discourse.cmake.org/t/refering-to-generated-files-specified-in-top-level-cmakelist-txt-from-sub-directory-cmakelist-txt/9623/1 "2023-12-13T13:57:39Z")

</div>

HI, I am using cmake 3.20. But it also happens with 3.28

In top level CMakeList.txt I do

```auto
add_custom_command(
    OUTPUT ${CMAKE_BINARY_DIR}/version.cpp
           ${CMAKE_BINARY_DIR}/_version.cpp
    COMMAND ${CMAKE_COMMAND} -P
    ${CMAKE_SOURCE_DIR}/version.cmake
    )

```

And later in same file I do

```auto
add_executable(chVersion
  ${CMAKE_BINARY_DIR}/version.cpp
  chVersion.cc
 
  )

```

And that works fine.  
But if I refer to ${CMAKE\_BINARY\_DIR}/version.cpp  
in another CMakeList.txt added via add\_subdirectory, cmake complains that it cannot find it, and fails during configuration.

Is it supposed to not work.  
Is there some work-around?

---

<div class="post-metadata">

### Author: ![Thorbjorn\_Willoch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thorbjorn_willoch/32/1560_2.png) [@Thorbjorn\_Willoch](https://discourse.cmake.org/u/Thorbjorn_Willoch)
#### Post date: [December 13, 2023, 3:17pm UTC](https://discourse.cmake.org/t/refering-to-generated-files-specified-in-top-level-cmakelist-txt-from-sub-directory-cmakelist-txt/9623/2 "2023-12-13T15:17:38Z")

</div>

OK. I read the documentation, and it is not supposed to work. My workaround is to duplicate the custom command in each directory I use it, and output to CMAKE\_CURRENT\_BINARY\_DIR

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [December 13, 2023, 10:05pm UTC](https://discourse.cmake.org/t/refering-to-generated-files-specified-in-top-level-cmakelist-txt-from-sub-directory-cmakelist-txt/9623/4 "2023-12-13T22:05:47Z")

</div>

Starting with CMake 3.20 it should work. Have you updated your `cmake_minimum_required` to use the newer policies?

Please see [CMP0118](https://cmake.org/cmake/help/latest/policy/CMP0118.html) for details.

---

<div class="post-metadata">

### Author: ![Thorbjorn\_Willoch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thorbjorn_willoch/32/1560_2.png) [@Thorbjorn\_Willoch](https://discourse.cmake.org/u/Thorbjorn_Willoch)
#### Post date: [December 14, 2023, 8:30am UTC](https://discourse.cmake.org/t/refering-to-generated-files-specified-in-top-level-cmakelist-txt-from-sub-directory-cmakelist-txt/9623/5 "2023-12-14T08:30:30Z")

</div>

Thank you. make\_minimum\_required did the trick.
