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
REQUIRED
option 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_PATH
is specified allNO_*
options are enabled.
New in version 3.12: Search paths specified in the
_ROOT
CMake variable and the_ROOT
environment 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_PATH
is passed or by setting theCMAKE_FIND_USE_PACKAGE_ROOT_PATH
toFALSE
. 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_PATH
is passed or by setting theCMAKE_FIND_USE_CMAKE_PATH
toFALSE
: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_PATH
is passed or by setting theCMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH
toFALSE
:
<PackageName>_DIR
CMAKE_PREFIX_PATH
CMAKE_FRAMEWORK_PATH
CMAKE_APPBUNDLE_PATH
Search paths specified by the
HINTS
option. 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 thePATHS
option.==Search the standard system environment variables.== This can be skipped if
NO_SYSTEM_ENVIRONMENT_PATH
is passed or by setting theCMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH
toFALSE
. Path entries ending in/bin
or/sbin
are automatically converted to their parent directories:
PATH
Search paths stored in the CMake User Package Registry. This can be skipped if
NO_CMAKE_PACKAGE_REGISTRY
is passed or by setting the variableCMAKE_FIND_USE_PACKAGE_REGISTRY
toFALSE
or the deprecated variableCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY
toTRUE
.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_PREFIX
andCMAKE_STAGING_PREFIX
can be skipped ifNO_CMAKE_INSTALL_PREFIX
is passed or by setting theCMAKE_FIND_USE_INSTALL_PREFIX
toFALSE
. All these locations can be skipped ifNO_CMAKE_SYSTEM_PATH
is passed or by setting theCMAKE_FIND_USE_CMAKE_SYSTEM_PATH
toFALSE
:The platform paths that these variables contain are locations that typically include installed software. An example being
/usr/local
for UNIX based platforms.Search paths stored in the CMake System Package Registry. This can be skipped if
NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
is passed or by setting theCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY
variable toFALSE
or the deprecated variableCMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY
toTRUE
.See the
cmake-packages(7)
manual for details on the system package registry.Search paths specified by the
PATHS
option. These are typically hard-coded guesses.
以上是官方文档中提供的搜索路径构成方式。可以注意一下第五条,find_package
是会去搜索系统环境中所有的路径的。