通过`find_package`获取导入的目标? [英] Getting imported targets through `find_package`?

查看:150
本文介绍了通过`find_package`获取导入的目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Qt 5的 CMake手册使用 find_package 并说:

The CMake manual of Qt 5 uses find_package and says:


为每个Qt模块创建导入的目标。应该首选导入的目标名称,而不是在CMake命令(例如target_link_libraries)中使用诸如 Qt5< Module> _LIBRARIES 之类的变量。

Qt专用吗? find_package 是否为所有库生成导入目标 find_package 的文档在CMake 3.0中说:

Is it special for Qt or does find_package generate imported targets for all libraries? The documentation of find_package in CMake 3.0 says:


找到包时,包特定的信息是通过变量和Imported Targets记录的来提供的。

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

cmake软件包手册说:


结果 find_package 的使用要么是一组导入目标,要么是一组与构建相关信息相对应的变量。

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 -script,其中文档说创建了导入的目标

But I did not see another FindXXX.cmake-script where the documentation says that a imported target is created.

推荐答案

find_package 如今是两头野兽:


CMake为两种形式的软件包提供直接支持:配置文件包
查找模块包

来源

现在,怎么办

查找模块软件包是您可能最熟悉的软件包。他们执行CMake代码的脚本(例如这一个)对 find_library find_path 找出要在哪里找到库。

Find-module packages are the ones you are probably most familiar with. They execute a script of CMake code (such as this one) that does a bunch of calls to functions like find_library and find_path to figure out where to locate a library.

这种方法的最大优点是它非常通用。只要文件系统上有东西,我们就可以找到它。不利的一面是,它所提供的信息通常仅比某物的物理位置多。也就是说,查找模块操作的结果通常只是一堆文件系统路径。这意味着对诸如传递依赖项或多个构建配置之类的东西进行建模非常困难。

The big advantage of this approach is that it is extremely generic. As long as there is something on the filesystem, we can find it. The big downside is that it often provides little more information than the physical location of that something. That is, the result of a find-module operation is typically just a bunch of filesystem paths. This means that modelling stuff like transitive dependencies or multiple build configurations is rather difficult.

如果您要查找的内容具有自身,这将变得特别痛苦 em>是使用CMake构建的。在那种情况下,您已经在构建脚本中建模了一堆东西,现在您需要为查找脚本进行艰苦的重构,以使其可用于下游项目。

This becomes especially painful if the thing you are trying to find has itself been built with CMake. In that case, you already have a bunch of stuff modeled in your build scripts, which you now need to painstakingly reconstruct for the find script, so that it becomes available to downstream projects.

这是配置文件包的发亮之处。与查找模块不同,运行脚本的结果不仅仅是一堆路径,而是创建了功能齐全的CMake目标。对于依赖项目,看起来依赖关系已作为同一项目的一部分构建。

This is where config-file packages shine. Unlike find-modules, the result of running the script is not just a bunch of paths, but it instead creates fully functional CMake targets. To the dependent project it looks like the dependencies have been built as part of that same project.

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

This allows to transport much more information in a very convenient way. The obvious downside is that config-file scripts are much more complex than find-scripts. Hence you do not want to write them yourself, but have CMake generate them for you. Or rather have the dependency provide a config-file as part of its deployment which you can then simply load with a find_package call. And that is exactly what Qt5 does.

这也意味着,如果您自己的项目是一个库,请考虑在配置过程中生成配置文件。这不是CMake最直接的功能,但是结果却非常强大。

This also means, if your own project is a library, consider generating a config file as part of the build process. It's not the most straightforward feature of CMake, but the results are pretty powerful.

这里快速比较了这两种方法在CMake代码中的样子:

Here is a quick comparison of how the two approaches typically look like in CMake code:

查找模块样式

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; dr :如果依赖项提供,请始终偏爱配置文件包他们。如果没有,请改用find-script。

tl;dr: Always prefer config-file packages if the dependency provides them. If not, use a find-script instead.

这篇关于通过`find_package`获取导入的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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