gpt4 book ai didi

Cmake:基于变量内容的 add_custom_command 参数

转载 作者:行者123 更新时间:2023-12-01 05:00:17 24 4
gpt4 key购买 nike

我想要一个 Cmake 函数来将一些二进制文件复制到特定位置。为此,我有以下函数定义:

function ( collect_binaries TARGET_NAME DEST_DIR )
set ( targetsToCopy ${ARGN} )

set ( copy_cmd "COMMAND ${CMAKE_COMMAND} -E make_directory ${DEST_DIR}\n" )

foreach ( target ${targetsToCopy} )
LIST( APPEND copy_cmd "COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> ${DEST_DIR}$<TARGET_FILE_NAME:${target}>\n")
endforeach( target ${targetsToCopy} )

#message( FATAL_ERROR ${copy_cmd} )
add_custom_target( ${TARGET_NAME} )
add_custom_command( TARGET ${TARGET_NAME} PRE_BUILD ${copy_cmd} )

endfunction( collect_binaries )

以及以下用法:
collect_binaries( bin_copy ${PROJECT_BINARY_DIR}/out/ target_1 target_2 target3 )

我在项目树中定义了 target_1、target_2 和 target_3。考虑到这一点,我得到了以下 Cmake 配置输出:

CMake Warning (dev) at binary_copy.cmake:15 (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.



在这种情况下,目标似乎是未知的……但它确实存在并且没有错字。这里有什么问题?

最佳答案

您正在设置 copy_cmd collect_binaries 中的变量用作 CMake 字符串。 add_custom_command但是需要一个 CMake 列表来正确解析参数,即:

function ( collect_binaries TARGET_NAME DEST_DIR )
set ( targetsToCopy ${ARGN} )

set ( copy_cmd COMMAND ${CMAKE_COMMAND} -E make_directory ${DEST_DIR} )

foreach ( target ${targetsToCopy} )
LIST( APPEND copy_cmd COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> "${DEST_DIR}$<TARGET_FILE_NAME:${target}>")
endforeach( target ${targetsToCopy} )

#message( FATAL_ERROR ${copy_cmd} )
add_custom_target( ${TARGET_NAME} )
add_custom_command( TARGET ${TARGET_NAME} PRE_BUILD ${copy_cmd} )

endfunction( collect_binaries )

关于Cmake:基于变量内容的 add_custom_command 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33696412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com