CMake常用函数的说明
概述
这份文档主要用于收集和整理使用到的cmake指令,确保对齐内容有准确的理解。
find_package使用说明
对于有第三方库依赖的项目来说,find_package指令是一个非常便利以及推荐的依赖包处理工具。下面的例子就非常好的说明了,如果有对OpenCV的依赖,find_package是多么的强大和便利。1
1 | add_executable(my_bin src/my_bin.cpp) |
基本的工作流程如下:
find_package在一些目录中查找OpenCV的配置文件。- 找到后,
find_package会将头文件目录设置到${OpenCV_INCLUDE_DIRS}中,将链接库设置到${OpenCV_LIBS}中。 - 设置可执行文件的链接库和头文件目录,编译文件。
Tips:
- The
REQUIREDoption stops processing with an error message if the package cannot be found. - 对于
find_package更加详细的指令说明可以参考官方文档:Basic Signature和Full Signature。
find_package搜索路径:
The set of installation prefixes is constructed using the following steps. If
NO_DEFAULT_PATHis specified allNO_*options are enabled.
New in version 3.12: Search paths specified in the
_ROOTCMake variable and the_ROOTenvironment variable, where<PackageName>is the package to be found. The package root variables are maintained as a stack so if called from within a find module, root paths from the parent's find module will also be searched after paths for the current package. This can be skipped ifNO_PACKAGE_ROOT_PATHis passed or by setting theCMAKE_FIND_USE_PACKAGE_ROOT_PATHtoFALSE. See policyCMP0074.Search paths specified in cmake-specific cache variables. These are intended to be used on the command line with a
-DVAR=VALUE. The values are interpreted as semicolon-separated lists. This can be skipped ifNO_CMAKE_PATHis passed or by setting theCMAKE_FIND_USE_CMAKE_PATHtoFALSE:Search paths specified in cmake-specific environment variables. These are intended to be set in the user's shell configuration, and therefore use the host's native path separator (
;on Windows and:on UNIX). This can be skipped ifNO_CMAKE_ENVIRONMENT_PATHis passed or by setting theCMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATHtoFALSE:
<PackageName>_DIRCMAKE_PREFIX_PATHCMAKE_FRAMEWORK_PATHCMAKE_APPBUNDLE_PATHSearch paths specified by the
HINTSoption. These should be paths computed by system introspection, such as a hint provided by the location of another item already found. Hard-coded guesses should be specified with thePATHSoption.==Search the standard system environment variables.== This can be skipped if
NO_SYSTEM_ENVIRONMENT_PATHis passed or by setting theCMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATHtoFALSE. Path entries ending in/binor/sbinare automatically converted to their parent directories:
PATHSearch paths stored in the CMake User Package Registry. This can be skipped if
NO_CMAKE_PACKAGE_REGISTRYis passed or by setting the variableCMAKE_FIND_USE_PACKAGE_REGISTRYtoFALSEor the deprecated variableCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRYtoTRUE.See the
cmake-packages(7)manual for details on the user package registry.Search cmake variables defined in the Platform files for the current system. The searching of
CMAKE_INSTALL_PREFIXandCMAKE_STAGING_PREFIXcan be skipped ifNO_CMAKE_INSTALL_PREFIXis passed or by setting theCMAKE_FIND_USE_INSTALL_PREFIXtoFALSE. All these locations can be skipped ifNO_CMAKE_SYSTEM_PATHis passed or by setting theCMAKE_FIND_USE_CMAKE_SYSTEM_PATHtoFALSE:The platform paths that these variables contain are locations that typically include installed software. An example being
/usr/localfor UNIX based platforms.Search paths stored in the CMake System Package Registry. This can be skipped if
NO_CMAKE_SYSTEM_PACKAGE_REGISTRYis passed or by setting theCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRYvariable toFALSEor the deprecated variableCMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRYtoTRUE.See the
cmake-packages(7)manual for details on the system package registry.Search paths specified by the
PATHSoption. These are typically hard-coded guesses.
以上是官方文档中提供的搜索路径构成方式。可以注意一下第五条,find_package是会去搜索系统环境中所有的路径的。