通常需要确保CMake构建项目在编译后最终位于某个位置,并且该add_custom_command(..POST_BUILD...)命令是实现该目标的通用设计模式:
add_custom_command(
TARGET mytarget
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mytarget> ${CMAKE_BINARY_DIR}/final_destination
)
Run Code Online (Sandbox Code Playgroud)
令人遗憾的是,当所讨论的目标位于相对于包含add_custom_command调用的文件的子目录中时,该文件是通过add_subdirectory()命令递归编译的。尝试这样做会导致以下错误消息:
CMake Warning (dev) at CMakeLists.txt:4 (add_custom_command):
Policy CMP0040 is not set: The target in the TARGET signature of
add_custom_command() must exist. Run "cmake --help-policy CMP0040" for
policy details. Use the cmake_policy command to set the policy and
suppress this warning.
TARGET 'mytarget' was not created in this directory.
This warning is for project developers. Use -Wno-dev to suppress it.
Run Code Online (Sandbox Code Playgroud)
在许多情况下,有一个简单的解决方法:只需确保该add_custom_command() …
cmake ×1