【发布时间】:2020-07-07 11:04:17
【问题描述】:
我正在 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
我是新手。我已阅读 in the FAQ 关于不一致版本的内容,但我认为我做的所有事情都是正确的(我不调用 find_package(PythonInterp) 或 find_package(PythonLibs),而是坚持使用 find_package(Python))。这似乎工作,似乎 find_package(pybind11) 默认为 python2.7 (如果我理解文档不正确),我不知道如何设置它。我尝试过# set(bindings_python_version 3.8) 之类的东西,但这并没有改变任何东西。
我在一台基于 Windows 的机器上工作,但它只有一个版本的 Python,所以没有混淆的机会
【问题讨论】:
-
我通过在 find_package(Python3...) 之后调用 set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE}) 解决了这个问题。我猜你可以对 find_package(Python) 做同样的事情