之后如何使导入的目标成为GLOBAL? [英] How to make imported target GLOBAL afterwards?

查看:67
本文介绍了之后如何使导入的目标成为GLOBAL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从CMake 3.8的 FindBoost.cmake 模块中:

From the FindBoost.cmake module of CMake 3.8:

foreach(COMPONENT ${Boost_FIND_COMPONENTS})
  if(_Boost_IMPORTED_TARGETS AND NOT TARGET Boost::${COMPONENT})
    string(TOUPPER ${COMPONENT} UPPERCOMPONENT)
    if(Boost_${UPPERCOMPONENT}_FOUND)
      if(Boost_USE_STATIC_LIBS)
        add_library(Boost::${COMPONENT} STATIC IMPORTED)
      else()
        # Even if Boost_USE_STATIC_LIBS is OFF, we might have static
        # libraries as a result.
        add_library(Boost::${COMPONENT} UNKNOWN IMPORTED)
      endif()

以及该模块的 docu 中的相应注释:

and the corresponding comment from the docu of that module:


重要的是要注意,导入的目标与此模块创建的变量的行为有所不同:同一调用多次find_package(Boost)具有不同选项(例如静态或共享)的目录或子目录将不会覆盖第一次调用创建的目标的值。

It is important to note that the imported targets behave differently than variables created by this module: multiple calls to find_package(Boost) in the same directory or sub-directories with different options (e.g. static or shared) will not override the values of the targets created by the first call.

我认为将目标设为 GLOBAL 是合理的。

I see the rational for having the targets not being GLOBAL.

不过,首选的制作方式是它们是全局的吗?

However, what is the preferred way of making them global?

我习惯于在子目录中定义项目的依赖项,其中包括任何 find_package(...)调用。因此,Boost导入的目标在另一个目录中不可用,例如 /tests/CMakeLists.txt

I'm used to defining the dependencies of my project in a sub-directory including any find_package(...) calls. Consequently, the Boost imported targets are not available in another directory, e.g. /tests/CMakeLists.txt:

<project_root>
  /3rdparty
    /git-submodule-of-a-small-lib
    /CMakeLists.txt
  /include
    /...
  /tests
    /CMakeLists.txt
  /CMakeLists.txt


推荐答案

我设法通过包含 3rdparty / CMakeLists.txt 而不是<$ c $来解决全局项目范围中无法使用导入的Boost目标的问题c> add_subdirectory(3rdparty),但是通过 include(3rdparty / CMakeLists.txt)来评估 3rdparty / CMakeLists.txt 在调用者的范围内。

I managed to workaround the problem of having the imported Boost targets not available in the global project scope by including 3rdparty/CMakeLists.txt not by add_subdirectory(3rdparty) but via include(3rdparty/CMakeLists.txt) as this evaluates 3rdparty/CMakeLists.txt in the caller's scope.

这篇关于之后如何使导入的目标成为GLOBAL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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