Copy file from source directory to binary directory using CMake

后端 未结 8 1817
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 03:37

I\'m trying to create a simple project on CLion. It uses CMake (I\'m new here) to generate Makefiles to build project (or some sort of it)

All I need to is transfer

相关标签:
8条回答
  • 2020-11-28 04:36

    I would suggest TARGET_FILE_DIR if you want the file to be copied to the same folder as your .exe file.

    $ Directory of main file (.exe, .so.1.2, .a).

    add_custom_command(
      TARGET ${PROJECT_NAME} POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy 
        ${CMAKE_CURRENT_SOURCE_DIR}/input.txt 
        $<TARGET_FILE_DIR:${PROJECT_NAME}>)
    

    In VS, this cmake script will copy input.txt to the same file as your final exe, no matter it's debug or release.

    0 讨论(0)
  • 2020-11-28 04:36

    The suggested configure_file is probably the easiest solution. However, it will not rerun the copy command to if you manually deleted the file from the build directory. To also handle this case, the following works for me:

    add_custom_target(copy-test-makefile ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/input.txt)
    add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/input.txt
                       COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
                                                        ${CMAKE_CURRENT_BINARY_DIR}/input.txt
                       DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
    
    0 讨论(0)
提交回复
热议问题