find_package 和 pkg_search_module 有什么区别 [英] what is the difference between find_package and pkg_search_module

查看:31
本文介绍了find_package 和 pkg_search_module 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这些命令,我​​现在只能说 pkg_search_module 有一些奇怪的用法,我必须在其中编写我想要两次的依赖项.而不是像在 find_package 中那样一次

All I can say about those commands right now is pkg_search_module has some odd usage, where I have to write the dependency that I would like to have two times. instead of just once like in find_package

pkg_search_module(ZLIB REQUIRED zlib)
#seriously two times zlib and once in only capital case‽
find_package(ZLIB)
#much cleaner but for some odd reason find_package does not work all the time

推荐答案

pkg_search_module 使用 pkg-config 工具来确定请求库的位置.这对于已经使用 pkg-config 的系统非常有用,因此您无需复制 CMake 的所有信息.请注意,这种方法存在潜在的可移植性问题,因为它需要在构建机器上正确设置 pkg-config.

pkg_search_module uses the pkg-config tool to determine the location of the requested library. This is mostly useful on systems where pkg-config is already in use, so you do not need to replicate all the information for CMake. Note that this approach has potential portability issues, since it requires pkg-config to be setup correctly on the build machine.

find_package 另一方面是CMake 自己的解决同一问题的机制.为此,您需要为请求的库提供 CMake 查找脚本(CMake 已经附带 其中一些,但您可以轻松编写自己的)或包配置脚本 由请求的库本身提供.无论哪种情况,您都可能需要调整您的 CMAKE_MODULE_PATH 让 CMake 能够找到相应的脚本.

find_package on the other hand is CMake's very own mechanism for solving the same problem. For this to work you either need a CMake find script for the requested library (CMake already ships with a couple of those, but you can easily write your own) or alternatively a package config script provided by the requested library itself. In either case you might have to adjust your CMAKE_MODULE_PATH for CMake to be able to find the respective script.

选择使用哪个很简单:

  • 始终首选包配置脚本(如果库提供).
  • 使用查找脚本作为不知道 CMake 的库的后备.
  • 如果您想利用 pkg-config 的协同作用,请使用 pkg_search_module.请特别注意,可以使用 pkg_search_module 作为获取所需信息的一种方法来实现查找脚本.
  • Always prefer package config scripts, if provided by the library.
  • Use find scripts as a fallback for libraries that are not aware of CMake.
  • Use pkg_search_module if you want to exploit synergies with pkg-config. In particular, note that it is possible to implement a find script using pkg_search_module as one method of obtaining the required information.

这篇关于find_package 和 pkg_search_module 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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