CMake常用函数的说明

概述

这份文档主要用于收集和整理使用到的cmake指令,确保对齐内容有准确的理解。

find_package使用说明

对于有第三方库依赖的项目来说,find_package指令是一个非常便利以及推荐的依赖包处理工具。下面的例子就非常好的说明了,如果有对OpenCV的依赖,find_package是多么的强大和便利。1

1
2
3
4
add_executable(my_bin src/my_bin.cpp)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(my_bin, ${OpenCV_LIBS})

基本的工作流程如下:

  1. find_package在一些目录中查找OpenCV的配置文件。
  2. 找到后,find_package会将头文件目录设置到${OpenCV_INCLUDE_DIRS}中,将链接库设置到${OpenCV_LIBS}中。
  3. 设置可执行文件的链接库和头文件目录,编译文件。

Tips:

  1. The REQUIRED option stops processing with an error message if the package cannot be found.
  2. 对于find_package更加详细的指令说明可以参考官方文档:Basic SignatureFull Signature

find_package搜索路径:

The set of installation prefixes is constructed using the following steps. If NO_DEFAULT_PATH is specified all NO_* options are enabled.

  1. 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 if NO_PACKAGE_ROOT_PATH is passed or by setting the CMAKE_FIND_USE_PACKAGE_ROOT_PATH to FALSE. See policy CMP0074.

  2. 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 if NO_CMAKE_PATH is passed or by setting the CMAKE_FIND_USE_CMAKE_PATH to FALSE:

  3. 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 if NO_CMAKE_ENVIRONMENT_PATH is passed or by setting the CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH to FALSE:

  4. 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 the PATHS option.

  5. ==Search the standard system environment variables.== This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is passed or by setting the CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH to FALSE. Path entries ending in /bin or /sbin are automatically converted to their parent directories:

    • PATH
  6. Search paths stored in the CMake User Package Registry. This can be skipped if NO_CMAKE_PACKAGE_REGISTRY is passed or by setting the variable CMAKE_FIND_USE_PACKAGE_REGISTRY to FALSE or the deprecated variable CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY to TRUE.

    See the cmake-packages(7) manual for details on the user package registry.

  7. Search cmake variables defined in the Platform files for the current system. The searching of CMAKE_INSTALL_PREFIX and CMAKE_STAGING_PREFIX can be skipped if NO_CMAKE_INSTALL_PREFIX is passed or by setting the CMAKE_FIND_USE_INSTALL_PREFIX to FALSE. All these locations can be skipped if NO_CMAKE_SYSTEM_PATH is passed or by setting the CMAKE_FIND_USE_CMAKE_SYSTEM_PATH to FALSE:

    The platform paths that these variables contain are locations that typically include installed software. An example being /usr/local for UNIX based platforms.

  8. Search paths stored in the CMake System Package Registry. This can be skipped if NO_CMAKE_SYSTEM_PACKAGE_REGISTRY is passed or by setting the CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY variable to FALSE or the deprecated variable CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY to TRUE.

    See the cmake-packages(7) manual for details on the system package registry.

  9. Search paths specified by the PATHS option. These are typically hard-coded guesses.

以上是官方文档中提供的搜索路径构成方式。可以注意一下第五条,find_package是会去搜索系统环境中所有的路径的。


  1. cmake find_package路径详解 - 知乎 (zhihu.com)↩︎