无法在CMAKE中复制文件:复制文件时出错

时间:2019-05-16 13:04:46

标签: c++ cmake nmake

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

我的项目看起来是这样的:

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

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

我上面描述的没关系。

我尝试将B生成的dll复制到另一个目录,这样编写命令,但不起作用。(在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)

该如何解决?

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用 ${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>)