0

I'm trying to copy assets (images, docs, resources, etc.) to the output directory, e.g. build/. Currently I'm using the following method:

add_custom_command(
    TARGET my-project POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_SOURCE_DIR}/assets
            ${CMAKE_BINARY_DIR}
)

It works just as I need, until the multi-configuration generators (such as Visual Studio) come into play. Since they generate separate Debug and Release directories for my target, I get the following result:

  • The executable is put into the build/Debug directory
  • The assets are put into the build/ directory

Obviously, I want both assets and the executable go either to build/ or build/Debug (the second one is preferred, otherwise we lose the whole point of the multi-configuration generators).

How should I handle this (pretty widespread, in my opinion) case?

John Doe
  • 555
  • 6
  • 17
  • Have you checked [that question](https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake)? It tells about copying dlls near the executable. – Tsyvarev Aug 13 '21 at 18:31
  • @JohnDoe The important thin in the linked question is the use of generator expressions: `$` evaluates to the directory output file is stored in and this is evaluated seperately for every configuration. To make first option work reasonably well you'd need to specify different names for the output files of each configuration; otherwise you'll force your build system to rebuild the target partially on changing configurations... – fabian Aug 13 '21 at 18:51
  • @Tsyvarev Seems that this expression doesn't retain my `assets` directory structure and flattens a multi-level file/folder tree. Thanks for your suggestion, though. – John Doe Aug 14 '21 at 07:31
  • @fabian Thanks for your tip. However, I don't think I can use this expression (see my previous comment). – John Doe Aug 14 '21 at 07:32
  • @JohnDoe: Expression `$` represents only directory with the executable. By itself, that directory neither affects on structure of `assets` directory nor flattens it. Probably, **something else** wrong with your command, but since you don't show your attempt, we cannot say what is wrong. – Tsyvarev Aug 14 '21 at 09:35
  • @Tsyvarev You're right: my fault, I misconfigured one of my custom commands. If you post an answer, I'll upvote and accept it. Thank you! – John Doe Aug 14 '21 at 20:08

0 Answers0