# Difficulty with add\_executable() with generated files

**URL:** https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921
**Category:** Usage
**Tags:** tool:cmake
**Created:** [January 28, 2022, 6:26am UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921 "2022-01-28T06:26:40Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nicholas_yue/32/464_2.png) [@Nicholas\_Yue](https://discourse.cmake.org/u/Nicholas_Yue)
#### Post date: [January 28, 2022, 6:26am UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/1 "2022-01-28T06:26:40Z")

</div>

The files does not get created in time to be included in add\_executable()

What do I need to modify to get the files generation target run before add\_executable so that ${generated\_files} via file GLOB\_RECURSE have files ?

```auto
include_directories(.)

set(TARGET_NAME vtkui)

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generatednodes)

add_custom_target(ALL # codegen
  COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/node_generation_main.py ${CMAKE_CURRENT_SOURCE_DIR}/nodes_definition.json ${CMAKE_CURRENT_BINARY_DIR}/generatednodes/
  )

file(GLOB_RECURSE generated_files ${CMAKE_CURRENT_BINARY_DIR}/generatednodes/*)

message("generated_files = ${generated_files}")

add_executable(${TARGET_NAME} WIN32
  VTKUIMainWindow.cpp
  abstractbase.cpp
  gcomponents.cpp
  #vtknodes/PNGReader.cpp
  #vtknodes/PNGWriter.cpp
  #vtknodes/GaussianSmooth.cpp
  main.cpp
  ${generated_files}
  # ${CMAKE_CURRENT_BINARY_DIR}/generatednodes/generatednodes.h
  )

#add_custom_command(TARGET ${TARGET_NAME} PRE_BUILD
# # OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generatednodes/generatednodes.h
# COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/node_generation_main.py ${CMAKE_CURRENT_SOURCE_DIR}/nodes_definition.json ${CMAKE_CURRENT_BINARY_DIR}/generatednodes/
# )

# add_dependencies(${TARGET_NAME} codegen)

```

---

<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: [January 28, 2022, 6:19pm UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/2 "2022-01-28T18:19:51Z")

</div>

The generator is called during build time, so the files do not even exist when CMake runs.  
CMake needs to know which files it shall compile and link, so they need to explicitly listed. Otherwise it can’t generate the build rules for them.

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [January 28, 2022, 6:40pm UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/3 "2022-01-28T18:40:15Z")

</div>

May be you could have a look at this article:

> **[Generated Sources In CMake Builds](https://crascit.com/2017/04/18/generated-sources-in-cmake-builds/)**
>
> Using the right methods, generated sources can be seamlessly incorporated into CMake builds with robust dependencies.

which explains the issue of generated source files.

You’ll discover that using `add_custom_command` is better than `add_custom_target` provided that you can give the list of generated files ahead of time.

NB: if `${CMAKE_CURRENT_SOURCE_DIR}/node_generation_main.py` is yours may be you can make it spit out the list of generated files in order to avoid the `file(GLOB_RECURSE generated_files ...` which is not executed soon enough (at configure time and not build time).

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nicholas_yue/32/464_2.png) [@Nicholas\_Yue](https://discourse.cmake.org/u/Nicholas_Yue)
#### Post date: [January 28, 2022, 6:48pm UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/4 "2022-01-28T18:48:17Z")

</div>

Yes, node\_generation\_main.py is my own code

Question  
(1) Should I spit out the file names of the generated files as full path or relative path ? One line for each file or concatenated in a single line (as a CMake list string?) ?  
(2) How should I then pass that information to add\_executable ? Any pseudo code example ?

Cheers

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 29, 2022, 2:56pm UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/5 "2022-01-29T14:56:53Z")

</div>

> [@Nicholas\_Yue](#):
>
> (1) Should I spit out the file names of the generated files as full path or relative path ? One line for each file or concatenated in a single line (as a CMake list string?) ?  
> (2) How should I then pass that information to add\_executable ? Any pseudo code example ?

I would generate a file that is of a known filename that `#include`’s the other files generated. This path can then be added to the source listing for the executable. If you _do_ statically know the list of files that will be created, that would be better, but it seems that (based on the glob), the set of files depends on the content of that json file.

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [January 29, 2022, 6:47pm UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/6 "2022-01-29T18:47:54Z")

</div>

> [@Nicholas\_Yue](#):
>
> Question  
> (1) Should I spit out the file names of the generated files as full path or relative path ? One line for each file or concatenated in a single line (as a CMake list string?) ?  
> (2) How should I then pass that information to add\_executable ? Any pseudo code example ?
> 
> Cheers

(1) since the generator is yours you can make it generate a list file that can be included using  
[https://cmake.org/cmake/help/latest/command/include.html](https://cmake.org/cmake/help/latest/command/include.html), so the content of the generated file may defined a CMake variable, e.g. GENERATED\_FILES which contains the list of generated file.

You can  
include(generated\_file.cmake OPTIONAL)  
so that inclusion would not break if file is not there, but you’ll have to run cmake twice to get it right.

or you statically know the list of file (as @ben.boeckel said)

or you generate the list of file during _configuration_ using [https://cmake.org/cmake/help/latest/command/execute\_process.html](https://cmake.org/cmake/help/latest/command/execute_process.html) so that the to-be-include file is ensured to exists (provided `execute_process` happens before `include`).  
In that last case you may have to rerun cmake manually when nodes\_definition.json changes, unless  
you add [https://cmake.org/cmake/help/latest/prop\_dir/CMAKE\_CONFIGURE\_DEPENDS.html](https://cmake.org/cmake/help/latest/prop_dir/CMAKE_CONFIGURE_DEPENDS.html) property to nodes\_definition.json file so that cmake will be rerun as soon as nodes\_definition.json changes.

Now @ben.boeckel advised against this solution in the past: [CMAKE\_CONFIGURE\_DEPENDS with newly created file](https://discourse.cmake.org/t/cmake-configure-depends-with-newly-created-file/663) so may be he can explain why.

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nicholas_yue/32/464_2.png) [@Nicholas\_Yue](https://discourse.cmake.org/u/Nicholas_Yue)
#### Post date: [January 29, 2022, 6:51pm UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/7 "2022-01-29T18:51:50Z")

</div>

Thank you @erk and @ben.boeckel

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [January 31, 2022, 8:54am UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/8 "2022-01-31T08:54:30Z")

</div>

I would be interested in knowing your final solution @Nicholas_Yue

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nicholas_yue/32/464_2.png) [@Nicholas\_Yue](https://discourse.cmake.org/u/Nicholas_Yue)
#### Post date: [January 31, 2022, 4:55pm UTC](https://discourse.cmake.org/t/difficulty-with-add-executable-with-generated-files/4921/9 "2022-01-31T16:55:29Z")

</div>

After reading Craig Scott’s post, I chose the “Generating Files At Configure Time” option.

As I am still in the prototyping/R&D phase of a project, I make the decision to priorities the time for something that is good enough for now.

```auto
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generatednodes)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/generatednodes)

execute_process(COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/node_generation_main.py ${CMAKE_CURRENT_SOURCE_DIR}/nodes_definition.json ${CMAKE_CURRENT_BINARY_DIR}/generatednodes/ )

file(GLOB_RECURSE generated_files ${CMAKE_CURRENT_BINARY_DIR}/generatednodes/*)

message("generated_files = ${generated_files}")

add_executable(${TARGET_NAME} WIN32
  VTKUIMainWindow.cpp
  abstractbase.cpp
  gcomponents.cpp
  main.cpp
  ${generated_files}
  )

```

When the prototype is approved with more resources, I will revisit this as I also need to look into packaging and installation via CMake.

Thank you for your support and interest.

Kind regards
