我正在 Ubuntu (20.04) 上的 VSCode (1.46.1) 中使用 CMake (3.16.3) 和 pybind11 (2.4.3) 创建一个入门项目,默认情况下同时包含 Python 2.7 和 3.8。我想为Python3构建一个模块。当我在 CMakeLists.txt 中使用以下两行时
find_package(pybind11)
find_package(Python COMPONENTS Interpreter Development REQUIRED)
CMake 配置是
[cmake] -- Found PythonInterp: /usr/bin/python (found version "2.7.18")
[cmake] -- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so
[cmake] -- Found Python3: /usr/bin/python3.8 (found version "3.8.2") found components: Interpreter Development
切换 find_package 语句的顺序
find_package(Python COMPONENTS Interpreter Development REQUIRED)
find_package(pybind11)
提供相同的 python 链接,但使用新的顺序
[cmake] -- Found Python: /usr/bin/python3.8 (found version "3.8.2") found components: Interpreter Development
[cmake] -- Found PythonInterp: /usr/bin/python (found version "2.7.18")
[cmake] -- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so
我是新来的。我读过了在常见问题解答中 https://pybind11.readthedocs.io/en/stable/faq.html#inconsistent-detection-of-python-version-in-cmake-and-pybind11关于不一致的版本,但我认为我正在做所有正确的事情(我不调用 find_package(PythonInterp) 也不调用 find_package(PythonLibs) ,而是坚持使用 find_package(Python) )。这似乎有效,似乎 find_package(pybind11) 默认为 python2.7 (如果我理解文档,则错误),我不知道如何设置它。我尝试过类似的事情# set(bindings_python_version 3.8)
但这并没有改变任何事情。
我在一台基于 Windows 的机器上运行该程序,但该机器上只有一个版本的 Python,因此不会产生混淆
所以最后,调用set(PYBIND11_PYTHON_VERSION 3.8 CACHE STRING "")
打电话之前find_package(pybind11)
解决了我的问题,尽管它不能帮助我理解为什么 pybind11 默认为 2.7。如果有人能给我一个解释,我将非常感激。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)