Generate executable which generates a file

Consider the following cmake code in a CMakeLists.txt file:


add_executable(mygen generate.c)

add_custom_command(
  OUTPUT out.txt
  COMMAND mygen ${CMAKE_CURRENT_SOURCE_DIR}/in.txt ${CMAKE_CURRENT_BINARY_DIR}/out.txt
  DEPENDS mygen ${CMAKE_CURRENT_SOURCE_DIR}/in.txt)

add_library(mylib out.txt)

This does the following:

The buildsystem rules will ensure that the executable is built before attempting to run the command that uses it.