安装EXPORT需要子项目的目标 [英] Install EXPORT requires target from subproject

查看:51
本文介绍了安装EXPORT需要子项目的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写cmake脚本来安装正在处理的项目.其中一部分是必需的 install(EXPORT LIB_EXPORTS ...),其中 LIB_EXPORTS 是我在各种 install(TARGETS...).

I'm trying to write a cmake script for installing a project I'm working on. Part of this is the necessary install(EXPORT LIB_EXPORTS ...) where LIB_EXPORTS is what I've been using for the EXPORT property in my various install(TARGETS ...).

我有一个超级构建结构,该结构使用 add_subdirectory 来构建我的项目所依赖的某些项目(SDL2,CivetWeb).

I have a superbuild structure that uses add_subdirectory to build some projects (SDL2, CivetWeb) that my project depends on.

我的问题是,当我使用 target_link_libraries 从子项目(SDL2中的SDL2-static,CivetWeb中的c-library)添加链接时,cmake抱怨这些依赖项不在导出集中

My problem is that when I use target_link_libraries to add a link from a subproject (SDL2-static from SDL2, c-library from CivetWeb) cmake complains that these dependencies aren't in the export set.

CMake Error: install(EXPORT "LIB_EXPORTS" ...) includes target "sc2api" which requires target "c-library" that is not in the export set.
CMake Error: install(EXPORT "LIB_EXPORTS" ...) includes target "sc2renderer" which requires target "SDL2-static" that is not in the export set.

我知道将目标添加到导出集中的唯一方法是使用 install(TARGETS ... EXPORT LIB_EXPORTS),但是我们无法安装未创建此子目录的目标.如果可以肯定地找到该库文件的生成位置,我可以 install(FILES ... EXPORT LIB_EXPORTS),但是我感觉这将安装两次(一次在CMakeLists.txt中).项目子目录,请点击此处).坦白说,我不确定为什么要包含这些内容,因为这些库应该静态链接到我项目中的目标中.

The only way I know to add targets to an export set is by using install(TARGETS ... EXPORT LIB_EXPORTS) but we can't install a target that this subdirectory hasn't created. I could install(FILES ... EXPORT LIB_EXPORTS) if I could find for sure where that library file's been generated but I have a feeling that this would install it twice (once by the CMakeLists.txt in the project subdirectory, once here). Frankly, I'm not sure why including these are necessary, since the libraries should be statically linked into the targets in my project.

我的问题:

  1. 如何在导出集中包含这些外部目标?
  2. 如果不应该,安装导出集的正确方法是什么?
  3. 奖励问题:这些子项目会自动将其安装目标添加到我项目的安装目标中.这有必要吗?如果不是,该如何禁用呢?

推荐答案

我们无法安装尚未创建此子目录的目标

we can't install a target that this subdirectory hasn't created

首先,事实并非如此.由于您要使用 add_subdirectory 添加依赖项,因此它们不是 IMPORTED ;这意味着它们是全局可见的,并且在项目中的何处创建都无关紧要.重要的是,它们是为调用 install(TARGETS)的调用而及时创建的.

First off, this isn't quite true. Since you're adding your dependencies with add_subdirectory, they are not IMPORTED; this means they are globally visible and it doesn't matter where in the project they were created. It just matters that they have been created in time for the call to install(TARGETS).

一种方便的方法是将所有打包命令放入 packaging/CMakeLists.txt 中,然后在 end 处调用 add_subdirectory(packaging)顶级 CMakeLists.txt 的名称,以便在第一次 install()调用之前创建每个目标.

One convenient approach is to put all your packaging commands in packaging/CMakeLists.txt and then call add_subdirectory(packaging) at the end of your top-level CMakeLists.txt so that every target has been created before the first install() call.

然后将目标添加到常规导出集中:

You would then add the targets to your normal export set:

install(TARGETS sc2api sc2renderer c-library SDL2-static
        EXPORT LIB_EXPORTS
        ...)

install(EXPORT LIB_EXPORTS ...)

这是首选的处理方式.

坦率地说,我不确定为什么要包含这些内容,因为这些库应该静态链接到我项目中的目标中.

Frankly, I'm not sure why including these are necessary, since the libraries should be statically linked into the targets in my project.

它们可能具有自己的使用要求,就像它们所依赖的其他库一样.因此,在导出时,CMake需要重新创建与使用您的库相关的所有 ,包括所有传递依赖项.如果您绝对确定这永远不会成为问题,则可以使用 $< BUILD_INTERFACE:...> 阻止导出依赖项.

They might carry their own usage requirements, like other libraries they depend on. So when exporting, CMake needs to recreate everything that's relevant to using your library, including all transitive dependencies. If you're absolutely certain that this will never be a concern, you can use $<BUILD_INTERFACE:...> to block the dependency from exporting.

add_library(sc2api SHARED)
target_link_libraries(sc2api PRIVATE $<BUILD_INTERFACE:c-library>)

这对于 INTERFACE 库可能会很有用,该库会收集警告标志或类似内容,并会导出为空目标.

This is occasionally useful for INTERFACE libraries that collect warning flags or the like, and would export as empty targets.

这篇关于安装EXPORT需要子项目的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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