# Is there a way to add a resource file to Visual Studio project and copy it to the executable directory?

**URL:** <https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886>\
**Category:** Code\
**Created:** [November 17, 2022, 5:27am UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886 "2022-11-17T05:27:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jack/32/2926_2.png) [@Jack](https://discourse.cmake.org/u/Jack)\
**Post date:** [November 17, 2022, 5:27am UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/1 "2022-11-17T05:27:59Z")

</div>

I have some text files that I want to add to the Visual Studio project. I can’t add it to the `add_executable()` call because I get this error:

```auto
  Cannot find source file:

    my_text_file.txt

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc

```

I also want to copy these to the build directory. I tried the command:

```auto
  add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${CMAKE_SOURCE_DIR}/my_text_file.txt"
                ${CMAKE_CURRENT_BINARY_DIR})

```

But it didn’t seem to work.

---

<div class="post-metadata">

**Author:** ![ChrisJaquet](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chrisjaquet/32/304_2.png) [@ChrisJaquet](https://discourse.cmake.org/u/ChrisJaquet)\
**Post date:** [November 17, 2022, 8:59am UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/2 "2022-11-17T08:59:13Z")

</div>

To add text files, I add them as sources, but you will need to mark them explicitly as header files:

```auto
set_source_files_properties(my_text_file.txt PROPERTIES HEADER_FILE_ONLY TRUE)

```

This will prevent it from being compiled but will still appear in your IDE, etc. They should still be added to the project using `add_executable`. Make sure that the path you give `add_executable` is correct since I think it only tries the various extensions if the file (as named) cannot be found in the current source dir.

For the copying, I do not see anything obviously wrong with your custom command. Just note that there is a difference between [CMAKE\_SOURCE\_DIR](https://cmake.org/cmake/help/latest/variable/CMAKE_SOURCE_DIR.html) and [CMAKE\_CURRENT\_SOURCE\_DIR](https://cmake.org/cmake/help/latest/variable/CMAKE_CURRENT_SOURCE_DIR.html). This could also be why your `add_executable` call is failing.

---

<div class="post-metadata">

**Author:** ![Jack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jack/32/2926_2.png) [@Jack](https://discourse.cmake.org/u/Jack)\
**Post date:** [November 17, 2022, 1:47pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/3 "2022-11-17T13:47:48Z")

</div>

Thanks Chris,

```auto
set_source_files_properties(my_text_file.txt PROPERTIES HEADER_FILE_ONLY TRUE)

```

That worked great.

As for the copy, I changed the command to:

```auto
  add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_CURRENT_SOURCE_DIR}/filenames_to_hash.txt
                ${CMAKE_CURRENT_BINARY_DIR})

```

and it worked, sorta. It put it in one level down from where the executable was put. So instead of `build/project/Debug/`, it put it in `build/project/`. What should I be making the target directory?

---

<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:** [November 17, 2022, 2:32pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/4 "2022-11-17T14:32:17Z")

</div>

You can use the `CMAKE_CFG_INTDIR` variable for the subdir that is made per-generator (if relevant).

---

<div class="post-metadata">

**Author:** ![Jack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jack/32/2926_2.png) [@Jack](https://discourse.cmake.org/u/Jack)\
**Post date:** [November 17, 2022, 2:34pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/5 "2022-11-17T14:34:41Z")

</div>

Attempted to show all the variables listed here: [cmake-variables(7) — CMake 3.25.0 Documentation](https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html), but none showed the actual directory where the build was placed.

---

<div class="post-metadata">

**Author:** ![Jack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jack/32/2926_2.png) [@Jack](https://discourse.cmake.org/u/Jack)\
**Post date:** [November 17, 2022, 3:00pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/6 "2022-11-17T15:00:50Z")

</div>

Hmmm, `CMAKE_CFG_INTDIR` seems to expand to `$(Configuration)`. Not very useful. ☹

---

<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:** [November 17, 2022, 3:25pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/7 "2022-11-17T15:25:59Z")

</div>

For Visual Studio, that is IDE-expanded into the name of the current configuration. This should work:

```cmake
add_custom_command(
  TARGET ${PROJECT_NAME} POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy
    ${CMAKE_CURRENT_SOURCE_DIR}/filenames_to_hash.txt
    ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})

```

Alternatively, this is probably simpler:

```cmake
add_custom_command(
  TARGET ${PROJECT_NAME} POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy
    ${CMAKE_CURRENT_SOURCE_DIR}/filenames_to_hash.txt
    $<TARGET_FILE_DIR:${PROJECT_NAME}>)

```

---

<div class="post-metadata">

**Author:** ![Jack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jack/32/2926_2.png) [@Jack](https://discourse.cmake.org/u/Jack)\
**Post date:** [November 17, 2022, 4:29pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/8 "2022-11-17T16:29:49Z")

</div>

> [@ben.boeckel](#):
>
> `$<TARGET_FILE_DIR:${PROJECT_NAME}>`

The generator expression was exactly what I was looking for. The other one didn’t work.

Thx for the help!

---

<div class="post-metadata">

**Author:** ![Jack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jack/32/2926_2.png) [@Jack](https://discourse.cmake.org/u/Jack)\
**Post date:** [November 17, 2022, 4:32pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-add-a-resource-file-to-visual-studio-project-and-copy-it-to-the-executable-directory/6886/9 "2022-11-17T16:32:56Z")

</div>

FYI: I couldn’t mark two messages as solutions, so I quoted them here.

> [@ChrisJaquet](#):
>
> To add text files, I add them as sources, but you will need to mark them explicitly as header files:
> 
> ```auto
> set_source_files_properties(my_text_file.txt PROPERTIES HEADER_FILE_ONLY TRUE)
> 
> ```
> 
> This will prevent it from being compiled but will still appear in your IDE, etc. They should still be added to the project using `add_executable`. Make sure that the path you give `add_executable` is correct since I think it only tries the various extensions if the file (as named) cannot be found in the current source dir.

> [@ben.boeckel](#):
>
> ```auto
> add_custom_command(
> TARGET ${PROJECT_NAME} POST_BUILD
> COMMAND ${CMAKE_COMMAND} -E copy
> ${CMAKE_CURRENT_SOURCE_DIR}/filenames_to_hash.txt
> $<TARGET_FILE_DIR:${PROJECT_NAME}>)
> 
> ```
