Refering to generated files specified in top level CMakeList.txt from sub_directory CMakeList.txt

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

In top level CMakeList.txt I do

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

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?

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

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

Please see CMP0118 for details.

1 Like

Thank you. make_minimum_required did the trick.