如何使用cmake中的find_package检查一个可选组件?

huangapple go评论84阅读模式
英文:

How to check an OPTIONAL_COMPONENT using find_package in cmake?

问题

I want to build a specific feature of a GUI only if a certain component of a package is found. If not, the GUI should be built either way, but without this feature. The idea would be something like this, but I don't know the correct notation (or even if it is possible at all) to check the component itself:

find_package(Qt5 QUIET COMPONENTS Core Widgets OPTIONAL_COMPONENTS Charts)

if (Qt5_FOUND)
    message(STATUS "Gui enabled")
    ...
    if (Charts_FOUND) // This is the check I want to make
        message(STATUS "Optional feature enabled")
        ...
    else()
        message(STATUS "QtCharts not found")
    endif()
...

In cmake documentation: Finding Packages, in the Built-In Find Modules it says..

__FOUND
If false, then the optional part of package is unavailable.

So I tried if(Qt5_Charts_FOUND), but still, QtCharts is not found.

I am sure QtCharts is installed and working.

英文:

I want to build a specific feature of a GUI only if a certain component of a package is found. If not, the GUI should be built either way, but without this feature. The idea would be something like this, but I don't know the correct notation (or even if it is possible at all) to check the component itself:

find_package(Qt5 QUIET COMPONENTS Core Widgets OPTIONAL_COMPONENTS Charts)

if (Qt5_FOUND)
    message(STATUS "Gui enabled")
    ...
    if (Charts_FOUND) // This is the check I want to make
        message(STATUS "Optional feature enabled")
        ...
    else()
        message(STATUS "QtCharts not found")
    endif()
...

In cmake documentation: Finding Packages, in the Built-In Find Modules it says..

<XX>_<YY>_FOUND
If false, then the optional <YY> part of <XX> package is unavailable.

So I tried if(Qt5_Charts_FOUND) , but still, QtCharts is not found.

I am sure QtCharts is installed and working.

答案1

得分: 2

以下是翻译好的内容:

  • "The way of determine whether a component is found depends on the package."
    确定组件是否被找到的方式取决于包

  • "Built-In Find Modules" referred by the CMake tutorial are those Find modules which are shipped with CMake itself. They are documented here: https://cmake.org/cmake/help/latest/manual/cmake-modules.7.html#find-modules.
    As you could find, this list doesn't contain FindQt5 module.
    CMake教程提到的“内置查找模块”是CMake自带的查找模块。它们在这里进行了文档化:https://cmake.org/cmake/help/latest/manual/cmake-modules.7.html#find-modules。
    正如您所发现的,这个列表中不包括FindQt5模块。

  • Since Qt5 defines IMPORTED target Qt5::<component> for every component which is found, one could assume that such target won't be defined for an optional component which is missed:

if (Qt5_FOUND)
    # Qt5 has been found, will all required components
    if(TARGET Qt5::Charts)
       # Charts component has been found
    else()
       # Charts component hasn't been found
    endif()
endif()

由于Qt5为每个找到的组件定义了IMPORTED目标Qt5::<component>,因此可以假设对于未找到的可选组件不会定义此类目标:

find_package(Qt5 QUIET COMPONENTS Core Widgets OPTIONAL_COMPONENTS Charts)
if (Qt5_FOUND)
    # 找到了Qt5,将所有必需组件
    if(TARGET Qt5::Charts)
       # 找到了Charts组件
    else()
       # 未找到Charts组件
    endif()
endif()
  • If a package is searched by a Find script, and that script uses find_package_handle_standard_args helper, then given helper processes "optionality" of components by treating the variable <PackageName>_<Component>_FOUND as indicator of whether component is found.
    如果一个包是通过Find脚本搜索的,并且该脚本使用find_package_handle_standard_args助手,那么该助手会通过将变量<PackageName>_<Component>_FOUND视为组件是否被找到的指示器来处理组件的"可选性"。

  • If a package is searched by a Config script, then given script should process "optionality" of components by checking variables <PackageName>_FIND_REQUIRED_<Component>. E.g. the script QtConfig.cmake performs such checks. Not all Config scripts check that variables. A scripts which doesn't check that variables treats (incorrectly) optional components as required ones.
    如果一个包是通过Config脚本搜索的,那么给定的脚本应该通过检查变量<PackageName>_FIND_REQUIRED_<Component>来处理组件的"可选性"。例如,脚本QtConfig.cmake执行这些检查。并非所有的Config脚本都会检查这些变量。如果一个脚本不检查这些变量,它会错误地将可选组件视为必需组件。

英文:

The way of determine whether a component is found depends on the package.

"Built-In Find Modules" referred by the CMake tutorial are those Find modules which are shipped with CMake itself. They are documented here: https://cmake.org/cmake/help/latest/manual/cmake-modules.7.html#find-modules.
As you could find, this list doesn't contain FindQt5 module.

Since Qt5 defines IMPORTED target Qt5::&lt;component&gt; for every component which is found, one could assume that such target won't be defined for an optional component which is missed:

find_package(Qt5 QUIET COMPONENTS Core Widgets OPTIONAL_COMPONENTS Charts)
if (Qt5_FOUND)
    # Qt5 has been found, will all required components
    if(TARGET Qt5::Charts)
       # Charts component has been found
    else()
       # Charts component hasn&#39;t been found
    endif()
endif()

If a package is searched by a Find script, and that script uses find_package_handle_standard_args helper, then given helper processes "optionality" of components by treating the variable &lt;PackageName&gt;_&lt;Component&gt;_FOUND as indicator of whether component is found.

If a package is searched by a Config script, then given script should process "optionality" of components by checking variables &lt;PackageName&gt;_FIND_REQUIRED_&lt;Component&gt;. E.g. the script QtConfig.cmake performs such checks. Not all Config scripts check that variables. A scripts which doesn't check that variables treats (incorrectly) optional components as required ones.

huangapple
  • 本文由 发表于 2023年5月25日 02:59:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定