0

My executable needs a text file to run (with configuration and similar stuff), it is a simple text file, not generated and it is together in the source directory. I tried using configure_file and it works fine if I use any generator that is not Visual Studio or XCode. As soon as I try to do it using any of those two generators (in Windows and macOS respectively) the file is not copied in the Debug/Release directory but in the ${CMAKE_CURRENT_BINARY_DIR}, I understand this happens because configure_file runs at configure time and not at build time, so I guess I cannot use things like ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG} to manually copy the file to that directory.

This is my CMakeLists.txt file, the hello.cpp is a simple hello world application and the file sample.txt is a simple file with a line of text:

cmake_minimum_required(VERSION 3.10)
project(hello)

configure_file(sample.txt sample.txt COPYONLY)
add_executable(hello hello.cc)

Is there any way to add that sample.txt file as a direct dependency or something so it is copied at build time to the same directory where the output binary target?

Thanks a lot, I had been looking around the whole CMake documentation and questions without finding something relevant to my question.

cprieto
  • 400
  • 2
  • 14
  • Does this answer your question? [How to copy DLL files into the same folder as the executable using CMake?](https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake) You can use `add_custom_command` to copy the text file as a `POST_BUILD` command to the directory containing the built executable (as shown in the first answer). Be sure to use CMake generator expression `$` so the correct Debug or Release directory is referenced. – Kevin Jun 16 '20 at 16:07
  • Thanks @squareskittles that helped a lot! may you post this as an answer so I can mark it as answered? – cprieto Jun 16 '20 at 17:03
  • Happy to help! Your question was similar enough to the linked question, that *already* had several accepted solutions. So, your question has been marked as a duplicate, and I cannot provide an answer. – Kevin Jun 16 '20 at 17:14

0 Answers0