find_package如何在CONFIG模式下工作

时间:2015-07-30 20:52:02

标签: cmake

我在CONFIG模式下使用find_package来查找Qt5:

find_package(Qt5 CONFIG REQUIRED COMPONENTS Core)

我没有指定CMAKE_PREFIX_PATH,并且未在PATH中设置/home/user/Qt/5.5/gcc_64/bin,但是cmake找到了* Config.cmake文件:

/home/user/Qt/5.5/gcc_64/lib/cmake/Qt5/Qt5Config.cmake
/home/user/Qt/5.5/gcc_64/lib/cmake/Qt5Core/Qt5CoreConfig.cmake

它是如何工作的? cmake如何创建搜索路径?

EDIT1

我阅读了一份文档但我不清楚:

<prefix>/(lib/<arch>|lib|share)/cmake/<name>*/          (U)
<prefix>/(lib/<arch>|lib|share)/<name>*/                (U)
<prefix>/(lib/<arch>|lib|share)/<name>*/(cmake|CMake)/  (U)

前缀是什么?上述模式如何与Qt路径匹配? :

/home/user/Qt/5.5/gcc_64/lib/cmake/Qt5/Qt5Config.cmake

我为我的库创建了一个类似的路径:

/home/user/Mylib/1.0/gcc_64/lib/cmake/Mylib1/Mylib1Config.cmake

find_package(Mylib1 CONFIG)会返回错误:

CMake Warning at CMakeLists.txt:14 (find_package):
Could not find a package configuration file provided by "Mylib1" with any
of the following names:

Mylib1Config.cmake
mylib1-config.cmake

Add the installation prefix of "Mylib1" to CMAKE_PREFIX_PATH or set
"Mylib1_DIR" to a directory containing one of the above files.  If "Mylib1"
provides a separate development package or SDK, be sure it has been
installed.

2 个答案:

答案 0 :(得分:2)

<prefix>视为/home/user/Qt/5.5/gcc_64,可以通过该模板获取Qt5Config.cmake的完整目录名称:

<prefix>/(lib/<arch>|**lib**|share)/cmake/<name>*/

至于前缀本身的来源,我猜它来自User Package Registry

  
      
  1. 搜索存储在CMake用户包注册表中的路径。如果传递NO_CMAKE_PACKAGE_REGISTRY,则可以跳过此操作。有关用户包注册表的详细信息,请参阅cmake-packages(7)手册。
  2.   

您可以查看~/.cmake/packages/文件夹的内容,以检查注册表的内容。

答案 1 :(得分:0)

例如,如果/home/user/Qt/5.5/gcc_64/bin位于PATH,则cmake将自动搜索兄弟lib目录。