Copy target file to another location in a post build step in CMake

送分小仙女□ 提交于 2019-11-27 18:34:13

Rather than using the obsolete LOCATION property, prefer using generator expressions:

add_custom_command(TARGET mylibrary POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mylibrary> ${targetfile}
)

You could also just generate the exe in the target directory directly by setting the target property RUNTIME_OUTPUT_DIRECTORY instead of copying it. This has per-configuration options (e.g. RUNTIME_OUTPUT_DIRECTORY_DEBUG).

set_target_properties(mylibrary PROPERTIES
                      RUNTIME_OUTPUT_DIRECTORY_DEBUG <debug path>
                      RUNTIME_OUTPUT_DIRECTORY_RELEASE <release path>
)

For further details run:

cmake --help-property "RUNTIME_OUTPUT_DIRECTORY"
cmake --help-property "RUNTIME_OUTPUT_DIRECTORY_<CONFIG>"

Also, you should be able to use forward slashes throughout for path separators, even on Windows.

Use generator expressions in the POST_BUILD command instead of manually computing the output path. These are configuration aware. Example:

add_custom_command(TARGET mylibrary POST_BUILD 
  COMMAND "${CMAKE_COMMAND}" -E copy 
     "$<TARGET_FILE:mylibrary>"
     "my_target_path/$<CONFIGURATION>/$<TARGET_FILE_NAME:mylibrary>" 
  COMMENT "Copying to output directory")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!