0

I'm making a C++ cmake project using Visual Studio 2022. And I'm using some shaders that needs to be on the same directory as the executable. I've added a line inside my cmake file that copies the files I'm editing in my development directory next to the exe so it can use it. However every time I make changes to those shader files I have to manually run the cmake file by pressing ctrl+s.

I want it so that every time I build my project (when I press the green play button of the current selected item) it copies those files next to the exe automatically. My first instinct is to write a powershell or bash script but I didn't knew how visual studio would run those scripts on build.

So I looked around the internet for solutions and found out that I can set it up on my solutions properties tab. However the problem is since this is a cmake project there are no solutions and there are no properties tab.

How can I achieve this?

Turgut
  • 711
  • 3
  • 25
  • 3
    `add_custom_command( TARGET ${PROJECT_NAME} PRE_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${file_i} "${CMAKE_BINARY_DIR}/$" )` ? Every time you build your project, it will copy the files. – ChrisMM Aug 30 '23 at 16:54
  • @ChrisMM I rememer adding something similar to cmake and it not working – Turgut Aug 30 '23 at 16:56
  • 3
    Then you should post that into your question... Pre/Post build commands are used for this type of situation; I use many of them. – ChrisMM Aug 30 '23 at 16:58
  • @ChrisMM How do I add a Pre/Post build command? I just added that line to my cmake and I ended up manually saving again... – Turgut Aug 30 '23 at 17:10
  • The above cmake command is to make a pre-build command for Visual Studio; you'd have to adapt it to what you need (like changing `${file_i}` for example. – ChrisMM Aug 30 '23 at 17:12
  • @ChrisMM I was using that command for something else than saving shaders, I will give it a try, thanks. – Turgut Aug 30 '23 at 17:13
  • @ChrisMM One more question, can I use this to copy an entire directory? – Turgut Aug 30 '23 at 17:16
  • 1
    What might be the issue, is that it's a pre _build_ command. If the project doesn't build, then it won't be copying the file. You can probably copy entire directories. There's a `copy_directory_if_different `, see here: https://cmake.org/cmake/help/latest/manual/cmake.1.html – ChrisMM Aug 30 '23 at 17:30
  • There are many questions on Stack Overflow about copying directory with CMake. See e.g. [this one](https://stackoverflow.com/questions/13429656/how-to-copy-contents-of-a-directory-into-build-directory-after-make-with-cmake) or [that one](https://stackoverflow.com/questions/44054266/cmake-custom-command-copy-changed-file-even-if-dont-build-target-and-only-if). If you are trying some approach but it doesn't work for you, then you should include to the question post the **code of your attempt**. Otherwise.. we could just re-iterate solutions from those questions, which is not very useful. – Tsyvarev Aug 30 '23 at 18:43
  • check out https://stackoverflow.com/a/10672739/11107541 – starball Aug 30 '23 at 21:50

0 Answers0