# Write multiline text to file with path given by generator expression

**URL:** https://discourse.cmake.org/t/write-multiline-text-to-file-with-path-given-by-generator-expression/7781
**Category:** Usage
**Created:** [March 27, 2023, 11:29pm UTC](https://discourse.cmake.org/t/write-multiline-text-to-file-with-path-given-by-generator-expression/7781 "2023-03-27T23:29:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jnc](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jnc](https://discourse.cmake.org/u/jnc)
#### Post date: [March 27, 2023, 11:29pm UTC](https://discourse.cmake.org/t/write-multiline-text-to-file-with-path-given-by-generator-expression/7781/1 "2023-03-27T23:29:30Z")

</div>

Hi,

I have a project that requires creating a text file in the same folder as the output binary. The location of the binary can be obtained using a generator expression, which is not available when using `file(WRITE)`. However, if I use something like

```auto
add_custom_command(TARGET tgt
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E echo ${text_to_write} > $<TARGET_FILE_DIR:tgt>/qt.conf)

```

the ninja generator complains at build time, even if I use `VERBATIM`. The text has multiple lines as is something like this:

```auto
[Paths]
Plugins = /home/user/path/to/qt5/plugins

```

The best solution that I’ve found is to use `file(WRITE)` to a temp file, then copying the temp file to its final destination using `add_custom_command()`, but it feels too convoluted. Is there a way of doing this with just one command?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 28, 2023, 9:22pm UTC](https://discourse.cmake.org/t/write-multiline-text-to-file-with-path-given-by-generator-expression/7781/2 "2023-03-28T21:22:11Z")

</div>

You probably want `file(GENERATE)`, which can use generator expressions but writes the file during the CMake generation phase:

```cmake
file(GENERATE
    OUTPUT "$<TARGET_FILE_DIR:tgt>/qt.conf"
    CONTENT "${text_to_write}" # Use one of these two
    #INPUT "/path/to/somewhere/qt.conf" #
)

```
