find_package和pkg_search_module有什么区别 [英] what is the difference between find_package and 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.
使用哪个选择非常简单:
The choice which one to use is quite simple:
- 如果库提供了,总是喜欢软件包配置脚本。
- 使用查找脚本作为库的后备
- 如果要使用
pkg-config <的协同作用,请使用
pkg_search_module
/ code>。特别要注意的是,可以使用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 withpkg-config
. In particular, note that it is possible to implement a find script usingpkg_search_module
as one method of obtaining the required information.
这篇关于find_package和pkg_search_module有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!