1

I am trying to copy one file ${PROJECT_SOURCE_DIR}/abc.h to another location {PROJECT_SOURCE_DIR}/src with the following command:

  add_custom_command(
        TARGET MyTarget
        POST_BUILD
        COMMAND -E copy ${PROJECT_SOURCE_DIR}/abc.h
        $<"${PROJECT_SOURCE_DIR}/src":MyTarget>)

However, no matter how I try, it seems that the created VC Studio project will not perform the file copy. Any ideas? Thanks.

1 Answer 1

3

There are a couple of issues.

First, you want to execute cmake -E copy ... inside the custom command. To do this, you can provide the path to the CMake executable via the variable CMAKE_COMMAND.

Next, you don't need generator expressions in this case. You can just copy from "${PROJECT_SOURCE_DIR}/abc.h" to "${PROJECT_SOURCE_DIR}/src"

So, your final command should be more like:

add_custom_command(
      TARGET MyTarget
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/abc.h"
                                       "${PROJECT_SOURCE_DIR}/src")

As an aside, if the copied file is forming part of your build, you might be better to use cmake -E copy_if_different ... rather than just copy since this won't update the copied file's timestamp needlessly. (If the file is seen as "updated" all sources that #include it will be recompiled when a rebuild happens).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.