无法在 CMAKE:Error 复制文件中复制文件

Could not copy file in CMAKE:Error copying file

我正在尝试将我的 CMake 项目的输出复制到另一个目录,但它不起作用。它说:复制文件时出错。

我的项目是这样的:

A----
    |----B 
    |    |
    |    |
    |    |-----CmakeListB 
    |
    | 
    |
    | 
    |
    |-----C
    |     |
    |     |
    |     |------CmakeListC       
    |
    |
    |-----CmakeListA   

A 是我最重要的项目。 B是A的子项目,构建dll C是一个子项目,构建exe

我上面描述的并不重要

我尝试将 B 构建的 dll 复制到另一个目录,我这样写命令,但它不起作用。(On Windows7, VS2017)

   add_custom_command(
TARGET HOOK_SRC POST_BUILD 
COMMAND ${CMAKE_COMMAND} -E copy    
${CMAKE_CURRENT_BINARY_DIR}/MSP_S_HOOK.dll 
${CMAKE_CURRENT_BINARY_DIR}/1/MSP_S_HOOK.dll)  

CmakeList 如下所示


project(eSDK_MSP C CXX)

cmake_minimum_required(VERSION 3.8)

foreach (policy
        CMP0074)
    if (POLICY ${policy})
        cmake_policy(SET ${policy} NEW)
    endif ()
endforeach ()


if (POLICY CMP0074)
    cmake_policy(SET CMP0074 NEW)
endif ()

set(CMAKE_CXX_STANDARD 11)

if (MSVC)

endif ()
if (MINGW OR CYGWIN)
    add_definitions(-O3)
    link_libraries(ws2_32 mswsock)
    if (USE_32BITS)
        message("build x86 target")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
    else ()
        message("build x64 target")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
    endif (USE_32BITS)
endif ()

ADD_DEFINITIONS(-DBOOST_COROUTINES_NO_DEPRECATION_WARNING)

macro(get_WIN32_WINNT version)
    if (WIN32 AND CMAKE_SYSTEM_VERSION)
        set(ver ${CMAKE_SYSTEM_VERSION})
        string(REPLACE "." "" ver ${ver})
        string(REGEX REPLACE "([0-9])" "0\1" ver ${ver})
        set(${version} "0x${ver}")
    endif ()
endmacro()

get_WIN32_WINNT(ver)
add_definitions(-D_WIN32_WINNT=${ver})


set(Boost_USE_STATIC_LIBS ON) # enable dynamic linking
set(Boost_USE_MULTITHREAD ON)  # enable multithreading
set(Boost_USE_STATIC_RUNTIME OFF)
message(${CMAKE_BINARY_DIR})

find_package(BOOST REQUIRED)

if (NOT Boost_FOUND)
    message("not found boost package")
else ()
    message("found boost package")
    include_directories(${Boost_INCLUDE_DIR})
    LINK_DIRECTORIES(${CMAKE_LIBRARY_PATH} $ENV{BOOST_LIBRARYDIR})
    message("includes: ${Boost_INCLUDE_DIRS}")
    message("libs: $ENV{BOOST_LIBRARYDIR}")
endif ()


include_directories("."
        "../MSP_S/src/window"
        )

set(HOOK_SRC
        dllmain.cpp
        ../MSP_S/src/window/ShmUtils.h
        HOOK.def)

ADD_LIBRARY(HOOK_SRC SHARED ${HOOK_SRC})
target_link_libraries(HOOK_SRC wsock32 ws2_32 )
#MESSAGE(..)

if (MSVC)
#    if (CMAKE_CL_64)
#        add_custom_command(TARGET HOOK_SRC POST_BUILD
#                COMMAND ${CMAKE_COMMAND} -E copy
#                "MSP_S_HOOK.dll"
#                ${OUTPUT_DIR}"MSP_S_HOOKx64.dll")
#    else ()
#        add_custom_command(TARGET HOOK_SRC POST_BUILD
#                COMMAND ${CMAKE_COMMAND} -E copy
#                "MSP_S_HOOK.dll"
#                ${OUTPUT_DIR}"MSP_S_HOOKx86.dll")
#    endif ()
endif ()

message("hook "  ${TARGET_FILE_DIr})


add_custom_command(
TARGET HOOK_SRC POST_BUILD 
COMMAND ${CMAKE_COMMAND} -E copy    
${CMAKE_CURRENT_BINARY_DIR}/MSP_S_HOOK.dll 
${CMAKE_CURRENT_BINARY_DIR}/1/MSP_S_HOOK.dll)

set_target_properties(HOOK_SRC PROPERTIES LINK_FLAGS "/INCREMENTAL:NO" LINKER_LANGUAGE C)

我该怎么做才能解决这个问题?

您是否尝试过使用 ${PROJECT_BINARY_DIR} 而不是 ${CMAKE_CURRENT_BINARY_DIR}? 我相信 ${CMAKE_CURRENT_BINARY_DIR} 只是获取您正在处理的 CMake 项目的主要二进制目录。 下面的代码可以正确复制 dll。

add_custom_command(
       TARGET HOOK_SRC POST_BUILD 
       COMMAND ${CMAKE_COMMAND} -E copy_if_different    
       ${PROJECT_BINARY_DIR}/MSP_S_HOOK.dll 
       ${PROJECT_BINARY_DIR}/1/MSP_S_HOOK.dll
       $<TARGET_FILE_DIR:HOOK_SRC>)