我可以安装共享的导入库吗? [英] Can I install shared imported library?

查看:117
本文介绍了我可以安装共享的导入库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部项目和一个导入的共享库。 include目录和implib都可以正常运行,但是尝试安装共享库(dll)失败,并出现以下错误:

I have an external project and an imported shared library. The include directories and implib all work correctly, but trying to install the shared library (dll) fails with the following error:

install TARGETS given target "my_shared_lib" which does not exist in this directory.

此处的代码可重现:

add_library(my_shared_lib SHARED IMPORTED GLOBAL)
set_property(TARGET my_shared_lib PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/ExternalProjects/my_shared_lib")
set_property(TARGET my_shared_lib PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/ExternalProjects/my_shared_lib/my_shared_lib.dll")
set_property(TARGET my_shared_lib PROPERTY IMPORTED_IMPLIB "${CMAKE_CURRENT_BINARY_DIR}/ExternalProjects/my_shared_lib/my_shared_lib.lib")

add_executable(main main.cpp)
add_dependencies(main my_shared_lib)
target_link_libraries(main PUBLIC my_shared_lib)

install(TARGETS main DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/DIST")
install(TARGETS my_shared_lib DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/DIST")

有什么想法吗?

编辑:现在,我已经通过使用get_property提取IMPORTED_LOCATION,然后使用INSTALL FILES并提供该属性的值。看来可行,但是是否有更好,更惯用的cmake解决方案?

For now I've gotten around this problem by using get_property to pull out the IMPORTED_LOCATION, then using INSTALL FILES and giving the value of that property. It seems to work, but is there a better, more idiomatic-cmake solution?

推荐答案

CMake不允许安装 IMPORTED 库作为 TARGETS 。改为使用 install(FILES)

CMake doesn't allow to install IMPORTED libraries as TARGETS. Use install(FILES) instead.

这种行为至少有两个原因:

There are at least 2 reasons for such behavior:


  1. 选出一位CMake开发人员错误报告


导入的目标最初是为从现有外部安装包中导入而设计的,因此当时的安装没有意义。 / p>

Imported targets were originally designed for importing from an existing installation of some external package so installing did not make sense at the time.



  • 在安装 normal 库时,CMake可以对其进行修改以进行一些调整RPATH之类的属性。之所以可以进行这种修改,是因为CMake 知道该库的构建方式。这是将库安装为 TARGET 的主要优点。

  • When install normal library, CMake is able to modify it for adjust some properties like RPATH. Such modification is possible because CMake knows how the library has been built. This is a main advantage of installing library as a TARGET.

    但是对于 IMPORTED 库,CMake没有有关库编译过程的信息。 ,并且无法对其进行任何合理的修改。因此,CMake只能按原样安装库文件 :与简单的 install(FILES)相比没有优势。

    But for IMPORTED library CMake has no information about the library's compilation process, and cannot perform any reasonable modification of it. So, CMake may only install the library file as is: no advantages against simple install(FILES).

    这篇关于我可以安装共享的导入库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆