0

Using CMake, how do I put resources in a folder along with the build?

cmake_minimum_required(VERSION 3.5)

set(TARGET_NAME test-api)

add_executable(
    ${TARGET_NAME} 
    ${CMAKE_CURRENT_SOURCE_DIR}/Test.cpp
)

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_resources
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/test_resources ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_resources
)

I noticed that if I have the following folder structure:

/root
    /bin
        /Debug
        /Release

CMAKE_RUNTIME_OUTPUT_DIRECTORY points to /bin.

CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG points to /Debug.

CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE points to /Release.

I'd like to copy the test_resources to /Debug when I'm building debug and copy to /Release when I'm building Release. How do I do that?

3
  • 1
    file(GENERATE) command can help here
    – vsh
    May 27, 2020 at 8:20
  • Definitely sounds like the direction where I want to go :). Can I use it also to create new folders?
    – hasdrubal
    May 27, 2020 at 8:26
  • 1
    Does this answer your question? How to copy contents of a directory into build directory after make with CMake? You should use generator expressions as suggested in the first answer, which will be configuration-aware, and copy into the Debug or Release directory appropriately.
    – Kevin
    May 27, 2020 at 10:48

0

Browse other questions tagged or ask your own question.