需求
代码编写过程中写了GTest相关的东西,涉及到了调用三方库的dll及其他相关的东西。这就需要将三方内包含的目录及文件在编译前复制到exe的执行文件目录内。这就需要cmake的add_custom_target命令了。
官方文档:https://cmake.org/cmake/help/v3.22/command/add_custom_target.html?highlight=add_custom
代码
# 归拢需要复制的文件
FILE(GLOB RUNTIME_DEBUG_DLL_FILES CONFIGURE_DEPENDS
${ZsOverdrive_CONFIG_PATH}/../../bin/Releasex64/*
)
# 归拢需要复制的文件夹
FILE(GLOB RUNTIME_DEBUG_DLL_DIRS CONFIGURE_DEPENDS
${ZsOverdrive_CONFIG_PATH}/../../bin/Releasex64
${ZsOverdrive_CONFIG_PATH}/../../driver
)
# 复制相关文件
ADD_CUSTOM_TARGET(CopyRuntimeFiles ALL
VERBATIM
COMMAND_EXPAND_LISTS
# 创建目录
COMMAND ${CMAKE_COMMAND} -E
make_directory "${PROJECT_BINARY_DIR}/$<$<CONFIG:Release>:Release>$<$<CONFIG:Debug>:Debug>/"
# 复制文件
COMMAND ${CMAKE_COMMAND} -E
copy_if_different
"$<$<CONFIG:Release>:${RUNTIME_RELEASE_DLL_FILES}>"
"$<$<CONFIG:Debug>:${RUNTIME_DEBUG_DLL_FILES}>"
"${PROJECT_BINARY_DIR}/$<$<CONFIG:Release>:Release>$<$<CONFIG:Debug>:Debug>/"
# 复制文件夹
COMMAND ${CMAKE_COMMAND} -E
copy_directory
"$<$<CONFIG:Release>:${RUNTIME_DEBUG_DLL_DIRS}>"
"$<$<CONFIG:Debug>:${RUNTIME_DEBUG_DLL_DIRS}>"
"${PROJECT_BINARY_DIR}/$<$<CONFIG:Release>:Release>$<$<CONFIG:Debug>:Debug>/"
)
还不快抢沙发