Copy file from source directory to binary directory using CMake

后端 未结 8 1818
佛祖请我去吃肉
佛祖请我去吃肉 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:17

    both option are valid and targeting two different steps of your build:

    1. file(COPY ... copies the file in configuration step and only in this step. When you rebuild your project without having changed your cmake configuration, this command won't be executed.
    2. add_custom_command is the preferred choice when you want to copy the file around on each build step.

    The right version for your task would be:

    add_custom_command(
            TARGET foo POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
                    ${CMAKE_SOURCE_DIR}/test/input.txt
                    ${CMAKE_CURRENT_BINARY_DIR}/input.txt)
    

    you can choose between PRE_BUILD, PRE_LINK, POST_BUILD best is you read the documentation of add_custom_command

    an example on how to use the first version can be found here: Use CMake add_custom_command to generate source for another target

提交回复
热议问题