Page tree
Skip to end of metadata
Go to start of metadata

find_package  可以非常方便的引入外部依赖包,示例:

find_package(PythonLibs 2.7 REQUIRED)

然后就可以获取到 Python 相关的变量了,比如:

find_package(PythonLibs 2.7 REQUIRED)
message(STATUS "Python Include = ${PYTHON_INCLUDE_DIRS}")
message(STATUS "Python LIBRARIES = ${PYTHON_LIBRARIES}")

# output:
# -- Python Include = C:/Python27/include
# -- Python LIBRARIES = C:/Python27/libs/libpython27.a

find_package 是如何找到对应的依赖库呢?

  • Module模式(优先):查找 Find<LibraryName>.cmake 文件;
    • 可以通过 CMAKE_MODULE_PATH 来指定查找路径
  • Config模式:查找<LibraryName>Config.cmake 文件;


cmake文件一般可以在系统目录下找到,比如:

  • Linux:/usr/share/cmake-3.10/Modules
  • CLion:
    • C:\Program Files\JetBrains\CLion 2020.2.1\bin\cmake\cygwin\share\cmake-3.17.3\Modules
    • C:\Program Files\JetBrains\CLion 2020.2.1\bin\cmake\win\share\cmake-3.17\Modules

如果手动编译/自定义安装的软件,可以指定对应的目录 XXX_DIR,比如:

set(Caffe_DIR /path/to/caffe)


通过查看对应的cmake文件,可以看到相关的变量定义,比如:

FindPythonLibs.cmake
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

#.rst:
# FindPythonLibs
# --------------
#
# Find python libraries
#
# This module finds if Python is installed and determines where the
# include files and libraries are. It also determines what the name of
# the library is. This code sets the following variables:
#
# ::
#
# PYTHONLIBS_FOUND - have the Python libs been found
# PYTHON_LIBRARIES - path to the python library
# PYTHON_INCLUDE_PATH - path to where Python.h is found (deprecated)
# PYTHON_INCLUDE_DIRS - path to where Python.h is found
# PYTHON_DEBUG_LIBRARIES - path to the debug library (deprecated)
# PYTHONLIBS_VERSION_STRING - version of the Python libs found (since CMake 2.8.8)
#
#
#
# The Python_ADDITIONAL_VERSIONS variable can be used to specify a list
# of version numbers that should be taken into account when searching
# for Python. You need to set this variable before calling
# find_package(PythonLibs).
#
# If you'd like to specify the installation of Python to use, you should
# modify the following cache variables:
#
# ::
#
# PYTHON_LIBRARY - path to the python library
# PYTHON_INCLUDE_DIR - path to where Python.h is found
#
# If calling both ``find_package(PythonInterp)`` and
# ``find_package(PythonLibs)``, call ``find_package(PythonInterp)`` first to
# get the currently active Python version by default with a consistent version
# of PYTHON_LIBRARIES.

(具体的查找逻辑……)





  • No labels
Write a comment...