CMake - 安装第三方DLL依赖项

时间:2017-06-20 10:05:49

标签: c++ dll cmake install

我使用的是预编译的第三方库,它有多个DLL(一个用于实际的第三方,还有一些用作自己的依赖项) 我的目录结构如下

MyApp
    CMakeLists.txt // Root CMake file
    src
        MyCode.cpp
    thirdpartydep // Precompiled thirdparty dependency
        FindThirdPartyDep.cmake
        bin/
            thirdparty.dll
            thirdparty_dep1.dll
            thirdparty_dep2.dll
        include/
            thirdparty.h
        lib/
            thirdparty.lib // this is the importlibrary that loads thirdparty.dll

到目前为止,我们一直在使用thirdpartydep/bin复制copy_if_different目录中的所有DLL并手动列出DLL的路径。我正在尝试正确设置install目标,以便将thirdpartydep/bin中的dll复制到CMAKE_INSTALL_PREFIX/bin,但我无法弄清楚如何告诉cmake有关额外的二进制文件属于thirdpartydep的文件。

1 个答案:

答案 0 :(得分:0)

如果您将现代CMake与正确构建CONFIG第三方软件包(* -config.cmake)而不是MODULES(Find * .cmake)一起使用,那么它将起作用:

MACRO(INSTALL_ADD_IMPORTED_DLLS target_list target_component destination_folder)
  foreach(one_trg ${target_list})
    get_target_property(one_trg_type ${one_trg} TYPE)
    if (NOT one_trg_type STREQUAL "INTERFACE_LIBRARY")
       get_target_property(one_trg_dll_location ${one_trg} IMPORTED_LOCATION_RELEASE)
       if( one_trg_dll_location MATCHES ".dll$")
          install(FILES ${one_trg_dll_location} DESTINATION ${destination_folder} CONFIGURATIONS Release COMPONENT ${target_component})
       endif()
       get_target_property(one_trg_dll_location ${one_trg} IMPORTED_LOCATION_DEBUG)
       if( one_trg_dll_location MATCHES ".dll$")
          install(FILES ${one_trg_dll_location} DESTINATION ${destination_folder} CONFIGURATIONS Debug COMPONENT ${target_component})
       endif()
    endif()
  endforeach()
ENDMACRO()

它的用法如下:

set(THIRDPARTY_TARGETS "example_target1 example_target2 opencv_core")
if(MSVC)
    INSTALL_ADD_IMPORTED_DLLS("${THIRDPARTY_TARGETS}" bin bin)
endif()