gpt4 book ai didi

cmake - 通过 `find_package` 获取导入的目标?

转载 作者:行者123 更新时间:2023-12-04 03:07:01 29 4
gpt4 key购买 nike

CMake manual of Qt 5使用 find_package并说:

Imported targets are created for each Qt module. Imported target names should be preferred instead of using a variable like Qt5<Module>_LIBRARIES in CMake commands such as target_link_libraries.



是Qt专用还是 find_package为所有库生成导入目标? documentation of find_package in CMake 3.0说:

When the package is found package-specific information is provided through variables and Imported Targets documented by the package itself.



manual for cmake-packages说:

The result of using find_package is either a set of IMPORTED targets, or a set of variables corresponding to build-relevant information.



但是我没有看到另一个 FindXXX.cmake - 文档说明创建了导入目标的脚本。

最佳答案

find_package这些天是一个双头野兽:

CMake provides direct support for two forms of packages, Config-file Packages and Find-module Packages



Source

现在,这实际上意味着什么?

查找模块包是您可能最熟悉的那些。他们执行一个 CMake 代码脚本(例如 this one ),该脚本对 find_library 等函数进行大量调用和 find_path 找出在哪里可以找到图书馆。

这种方法的最大优点是它非常通用。只要文件系统上有东西,我们就可以找到它。最大的缺点是它提供的信息通常比该事物的物理位置多。也就是说,查找模块操作的结果通常只是一堆文件系统路径。这意味着像传递依赖或多个构建配置这样的建模是相当困难的。

如果你试图找到的东西本身是用 CMake 构建的,这会变得特别痛苦。在这种情况下,您已经在构建脚本中建模了一堆东西,您现在需要为查找脚本煞费苦心地重构它们,以便下游项目可以使用它。

这是 配置文件包闪耀。与 find-modules 不同,运行脚本的结果不仅仅是一堆路径,而是创建了功能齐全的 CMake 目标。对于依赖项目,看起来依赖项已作为同一项目的一部分构建。

这允许以非常方便的方式传输更多信息。明显的缺点是配置文件脚本比查找脚本复杂得多。因此,您不想自己编写它们,而是让 CMake 为您生成它们。或者更确切地说,让依赖项提供一个配置文件作为其部署的一部分,然后您可以简单地使用 find_package 加载它。称呼。这正是 Qt5 所做的。

这也意味着,如果您自己的项目是一个库,请考虑 generating a config file as part of the build process .这不是 CMake 最直接的功能,但结果非常强大。

以下是这两种方法在 CMake 代码中的典型外观的快速比较:

查找模块样式
find_package(foo)
target_link_libraries(bar ${FOO_LIBRARIES})
target_include_directories(bar ${FOO_INCLUDE_DIR})
# [...] potentially lots of other stuff that has to be set manually

配置文件样式
find_package(foo)
target_link_libraries(bar foo)
# magic!

tl;博士 :如果依赖项提供了配置文件包,则总是更喜欢它们。如果没有,请改用查找脚本。

关于cmake - 通过 `find_package` 获取导入的目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35815850/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com