如何使用 CMake 查找 python 解释器和库的最低版本

How to find minimum versions of python interpreter and libs using CMake

我正在使用带有 CMake 的 pybind11 构建一个 C 扩展。我曾经这样做过:

find_package(Python3 3.7 REQUIRED)
find_package(PythonLibs 3.7 REQUIRED)

没有任何问题。现在我需要 python 3.8 并将其更改为:

find_package(PythonInterp 3.8 REQUIRED)
find_package(PythonLibs 3.8 REQUIRED)

然而,尽管 libpython3.8.so 与 libpython3.7m.so 在同一个文件夹中,但 CMake 没有找到它。它returns:

Could NOT find PythonLibs: Found unsuitable version "3.7.5", but required
is at least "3.8" (found /usr/lib/x86_64-linux-gnu/libpython3.7m.so)

在研究这个时,我注意到 PytonInterp 和 PythonLibs 已被弃用,因为 CMake 3.12 和 FindPython 是首选。所以我尝试了:

find_package(Python3)

这会找到我的 python 3.8 解释器,但找不到我的 python 3.8 库。因此,我仍然无法使用该软件包,因为

mypy.cpython-37m-x86_64-linux-gnu.so

基于 python 3.7 库。我怎样才能确保它也能找到我的 3.8 库?如前所述,它们在同一个文件夹中。

FindPython 模块的文档指出:

If no COMPONENTS are specified, Interpreter is assumed.

所以,是的,因为您没有指定任何 COMPONENTS,所以只找到解释器。

如果您希望 CMake 找到 Python include 目录和库 ,您应该指定 Development:

find_package(Python3 COMPONENTS Interpreter Development)