CMake Install 安装第三方依赖库

cmake 提供 install 命令来安装文件,方便打包。但是有时也会对第三方依赖库有需求一起打包,在不确定第三方库dll文件路径和名称的情况下,需要另一种方式来打包了。

比如想要打包opencv_world.dll,一般cmake依赖opencv的方式是通过第三方软件包(* -config.cmake)的方式:

# CMakeLists.txt
find_package(OpenCV REQUIRED)

target_link_libraries(my_project ${OpenCV_LIBS})

那么在install opencv时可以采用该宏自动找到opencv_world.dll并安装到目标路径下

MACRO(INSTALL_IMPORTED_DLLS target_list target_component destination_dir)
  foreach(target_dll ${target_list})
    get_target_property(target_type ${target_dll} TYPE)
    if (NOT target_type STREQUAL "INTERFACE_LIBRARY")
       get_target_property(target_dll_location ${target_dll} IMPORTED_LOCATION_RELEASE)
       if( one_trg_dll_location MATCHES ".dll$")
          install(FILES ${target_dll_location} DESTINATION ${destination_dir} CONFIGURATIONS Release COMPONENT ${target_component})
       endif()
       get_target_property(target_dll_location ${target_dll} IMPORTED_LOCATION_DEBUG)
       if( target_dll_location MATCHES ".dll$")
          install(FILES ${target_dll_location} DESTINATION ${destination_dir} CONFIGURATIONS Debug COMPONENT ${target_component})
       endif()
    endif()
  endforeach()
ENDMACRO()

INSTALL_IMPORTED_DLLS(${OpenCV_LIBS} bin bin)

 参考:c++ - CMake - 安装第三方DLL依赖项 - Thinbug

你可能感兴趣的:(code,test,opencv)