CMake only copies files on reload, not build

2019-09-03 10:15发布

问题:

I'm using CMake in the CLion IDE, and in my CMakeLists.txt I use the following command in order to copy some resource files into the binary directory:

file(COPY ${CMAKE_SOURCE_DIR}/res DESTINATION ${CMAKE_BINARY_DIR})

This works whenever my CMake project is reloaded in CLion. However, whenever I just try to build, the files aren't copied again. How do I fix this? Am I using the wrong command?

回答1:

Use add_custom_target:

add_custom_target(copy_res_directory ALL
    COMMAND ${CMAKE_COMMAND} -E copy_directory 
        "${CMAKE_SOURCE_DIR}/res" "${CMAKE_BINARY_DIR}/res"
    COMMENT "Copy res directory to build tree"
    VERBATIM)


回答2:

Use configure_file(... COPYONLY)



标签: c++ cmake clion