cmake的find_package()简单总结
遇到的问题
find_package(lzb)出现错误如下:
CMake Warning at CMakeLists.txt:37 (find_package):
By not providing "Findlzb.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "lzb", but
CMake did not find one.
Could not find a package configuration file provided by "lzb" with any of
the following names:
lzbConfig.cmake
lzb-config.cmake
Add the installation prefix of "lzb" to CMAKE_PREFIX_PATH or set "lzb_DIR"
to a directory containing one of the above files. If "lzb" provides a
separate development package or SDK, be sure it has been installed.
两种模式
Moudule模式:搜索CMAKE_MODULE_PATH指定路径下的FindXXX.cmake文件。
Config模式:搜索指定路径下的XXXConfig.cmake或者XXX-config.cmake文件。
优先级
cmake默认采用Moudle模式,如果失败,会采用Config模式。
但是如果事前指定XXX_DIR,会优先搜索XXX_DIR下的Config文件;如果XXX_DIR下未找到相应文件,CMake会从Moudule模式重新开始寻找:
If <package>_DIR has been set to a directory not containing a configuration file CMake will ignore it and search from scratch
如果在Moudule模式下也未找到,那么进入Config模式,会在所谓的“其它目录”里面寻找,这里我也没仔细追寻“其它目录”指的啥,具体可以看CMake文档。
关于QUIET和REQUIRED选项
如果指定了QUIET选项,那么表示:就算未找到该包的配置文件,也不会报错。
相反地,如果指定了REQUIRED选项:如果未找到文件,会报错。
If the package configuration file cannot be found CMake will generate an error describing the problem unless the QUIET argument is specified. If REQUIRED is
specified and the package is not found a fatal error is generated and the configure step stops executing.
参考
https://zhuanlan.zhihu.com/p/50829542
https://www.jianshu.com/p/46e9b8a6cb6a
https://cmake.org/cmake/help/v3.10/command/find_package.html