[CMake] Copying target output file to another directory

Robert Dailey rcdailey.lists at gmail.com
Tue Jul 18 10:44:21 EDT 2017


So I need to copy the output *.so from a target to an arbitrary
directory outside the CMAKE_BINARY_DIR. I tried doing this as a post
build event using generator expressions:

add_custom_command(
    TARGET ${target} POST_BUILD VERBATIM
    COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:${target}>" ${dir}
)

This fails because I'm running it outside of the directory where
${target} is defined:

CMake Error at Core/CMake/third-party.cmake:86 (add_custom_command):
  TARGET 'MagickCore' was not created in this directory.


How can I do this without moving my add_custom_command() to the same
directory that created the target? I need to add all the custom
commands at the end of generation because it depends on certain global
properties which are not completely done being set until the very end.


More information about the CMake mailing list