cmake Windows .dll复制find_package命令

发布于 2025-02-08 05:49:00 字数 2912 浏览 0 评论 0原文

问题 为什么不将.dll复制到构建/发行/目录中,因为.EXE的构建?

Description

我使用此简单设置创建一个新的CMAKE文件:

cmake_minimum_required(VERSION 3.21)
project(use_sort)

find_package(sortdemo REQUIRED)
add_executable(example main.cpp)
target_link_libraries(example PRIVATE sortdemo::my_sort_lib sortdemo::my_print_lib)

该项目被配置为X64 Release链接到共享LIBS。 但是在Release文件夹中,我只能看到.exe,而无需.dll。

库my_sort_lib和my_print_lib是我在c:\ program文件中安装的库:

我不知道是否有必要,但是我也添加了这些库的cmakelists的链接: https://github.com/petrasvestartas/cmake/cmake/main/main/main/install_export

完整的项目在这里: httpps://github.com/petrasvestartas/petrasvestartas/cmake/cmake/cmake/cmake/tree/main/main/main/main/main/find_package

我尝试过的内容:

这不会复制任何DLL,

include(GNUInstallDirs )
install(
    TARGETS "example" 
    RUNTIME_DEPENDENCIES 
    )

这给了我错误

include(GNUInstallDirs )
install(
    TARGETS "example" 
    RUNTIME_DEPENDENCIES "sortdemo::my_sort_lib"
    )

错误:

CMake Error at CMakeLists.txt:28 (install):
  install TARGETS given unknown argument "sortdemo::my_sort_lib".

这也会给我错误,并且不可理解(使用另一个stackoverflow帖子中的答案)

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 "sortdemo::my_sort_lib" "sortdemo::my_print_lib")

if(MSVC)
    INSTALL_ADD_IMPORTED_DLLS("${THIRDPARTY_TARGETS}" bin bin)
endif()

错误

Make Error at CMakeLists.txt:28 (get_target_property):
  get_target_property() called with non-existent target "my_sort_lib".
Call Stack (most recent call first):
  CMakeLists.txt:44 (INSTALL_ADD_IMPORTED_DLLS)

Question
Why .dlls are not copied to the build/Release/ directory as .exe is built?

Description

I create a new CMake file with this simple setup:

cmake_minimum_required(VERSION 3.21)
project(use_sort)

find_package(sortdemo REQUIRED)
add_executable(example main.cpp)
target_link_libraries(example PRIVATE sortdemo::my_sort_lib sortdemo::my_print_lib)

The project gets configured and installed as x64 Release linked to SHARED libs.
But in Release folder I can only see the .exe, without .dlls.

The libraries my_sort_lib and my_print_lib are my libraries that were installed before in C:\Program Files:
enter image description here

I do not know if it is necessary, but I also add a link to the CMakeLists of these libraries: https://github.com/petrasvestartas/CMAKE/tree/main/install_export

The full project is here:
https://github.com/petrasvestartas/CMAKE/tree/main/find_package

What I tried:

This does not copy any dlls

include(GNUInstallDirs )
install(
    TARGETS "example" 
    RUNTIME_DEPENDENCIES 
    )

This gives me error

include(GNUInstallDirs )
install(
    TARGETS "example" 
    RUNTIME_DEPENDENCIES "sortdemo::my_sort_lib"
    )

Error:

CMake Error at CMakeLists.txt:28 (install):
  install TARGETS given unknown argument "sortdemo::my_sort_lib".

This gives me error too and not understandable (use this answer from another stackoverflow post)

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
quot;)
          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
quot;)
          install(FILES ${one_trg_dll_location} DESTINATION ${destination_folder} CONFIGURATIONS Debug COMPONENT ${target_component})
       endif()
    endif()
  endforeach()
ENDMACRO()

set(THIRDPARTY_TARGETS "sortdemo::my_sort_lib" "sortdemo::my_print_lib")

if(MSVC)
    INSTALL_ADD_IMPORTED_DLLS("${THIRDPARTY_TARGETS}" bin bin)
endif()

Error

Make Error at CMakeLists.txt:28 (get_target_property):
  get_target_property() called with non-existent target "my_sort_lib".
Call Stack (most recent call first):
  CMakeLists.txt:44 (INSTALL_ADD_IMPORTED_DLLS)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文