使用自定义目录提示 Find<name>.cmake 文件 [英] Hinting Find<name>.cmake Files with a custom directory
问题描述
在 CMake 中,有没有办法制作内置的 Find 脚本来确定我们指定的自定义目录的优先级?因为特别是在 Windows 中,模块查找脚本通常无法检测到模块,例如 Visual Studio 目录.因此,我通常必须手动设置外部库的路径,这很累人.相反,我希望这些脚本在自定义目录中查找,让我们先说主项目中的依赖项"文件夹,以便我可以直接将这些外部文件放在该文件夹中,这比将它们放入 VC 文件夹或手动设置要容易得多路径.
In CMake, is there a way to make built-in Find scripts to prioritize a custom directory that we specify? Because especially in windows, module finding scripts usually can't detect the module in, for example visual studio directories. Therefore I usually have to manually set the paths for the external libraries which is pretty tiring. Instead, I want those scripts to look in a custom directory, let's say 'dependencies' folder in the main project first so that I can directly put those externals in that folder which is much easier than putting them into the VC folder or manually setting the paths.
推荐答案
设置 CMAKE_PREFIX_PATH 变量正好用于这些目的:提示 find_*
函数关于搜索项的位置.
Setting CMAKE_PREFIX_PATH variable serves exactly these purposes: hinting find_*
function about location of searched item.
虽然这个变量的描述没有注意到find_package
函数,但是变量间接影响了它:Find
find_*
函数都有用于搜索项的精确算法,并且在系统路径之前检查使用CMAKE_PREFIX_PATH
构造的路径.
While description of this variable doesn't note about find_package
function, the variable affects it indirectly: the most of Find<name>.cmake
scripts use find_library and find_path functions. Note, that all find_*
functions have precise algorithm for search items, and paths constructed with CMAKE_PREFIX_PATH
are checked before system ones.
此外,CMAKE_PREFIX_PATH
会影响其他一些搜索过程.例如,如果第 3 方包提供
脚本而不是 Find
脚本,则该脚本也会被搜索使用此变量.pkg_check_modules 也使用 CMAKE_PREFIX_PATH
进行搜索用于描述包的 .pc
文件.
Moreover, CMAKE_PREFIX_PATH
affects some other search procedures. E.g., if 3rd party package provides <name>Config.cmake
script instead of Find<name>.cmake
one, this script is also searched for using this variable. pkg_check_modules also uses CMAKE_PREFIX_PATH
to search for .pc
files describing the package.
CMAKE_PREFIX_PATH
变量可以设置为环境一(以平台相关和使用特定的方式),作为 cmake
调用的参数:
CMAKE_PREFIX_PATH
variable can be set as environment one (in platform-depending and usage-specific way), as parameter to cmake
call:
cmake -DCMAKE_PREFIX_PATH=<additional-path> <other-parameters>
或在 CMakeLists.txt
文件中.在最后一种情况下,最好附加搜索目录,这样你的包的用户也可以为你的项目没有提供的搜索包设置变量:
or within CMakeLists.txt
file. In the last case it is better to append search directories, so user of you package is able to set the variable too for search packages not shipped with your project:
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/dependencies")
<小时>
请注意,变量 CMAKE_PREFIX_PATH
不会影响搜索 FindXXX.cmake
脚本本身.要指定脚本所在的目录,请使用 CMAKE_MODULE_PATH
变量.
Note, that variable CMAKE_PREFIX_PATH
doesn't affect searching for FindXXX.cmake
script itself. For specifying the directory where the script is located, use CMAKE_MODULE_PATH
variable.
这篇关于使用自定义目录提示 Find<name>.cmake 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!