python - 如何强制库(pybind11)包含Python3中的<Python.h>?

标签 python c++ pybind11

我正在使用pybind11为我的 C++ 代码创建 Python 绑定(bind)的库。

当我编译包含 <pybind11/pybind11.h> 的绑定(bind)代码文件时,它会生成以下错误:

/usr/local/include/pybind11/detail/common.h:112:10: fatal error: 'Python.h' file
      not found
#include <Python.h>

我可以通过将其更改为 #include <Python/Python.h> 来修复此错误但该库使用 Python 2.7 来生成绑定(bind)。

所以我尝试将其更改为 #include "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/include/python3.7m/Python.h"现在该库使用 Python 3.7 生成绑定(bind),这正是我想要的。

虽然这种方法有效,但我想知道是否有任何更简洁的方法可以使库始终包含来自 Python3 而不是 Python2 的 header 。

提前谢谢您!

PS:我使用的是 macOS 10.15.2

最佳答案

有几种方法,但据我所知,没有一种方法在所有平台上都是一致的(这就是为什么像 cmake (参见: https://github.com/pybind/cmake_example )这样的方法通常是首选的原因)。

首先是python-config ,即添加:

`python-config --includes`

(带反引号)到 CLI。我的问题是,它是通过 $PATH 找到的(因此,如果该安装没有 python ,则不需要匹配您正在运行的 python-config 版本)并且根据发行版,可能会有 python-config 。和python3-config对于 python2python3分别。

其次,有模块 distutils:

`python3 -c 'import distutils.sysconfig as ds; print(ds.get_python_inc())'`

它的优点是从实际的 python 运行你选择的。一般来说,distutils 跨平台也不完全一致,但是 get_python_inc是一个安全的选择。

关于python - 如何强制库(pybind11)包含Python3中的<Python.h>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59572655/

相关文章:

python - 如何从 nltk 中的 freqDist 获取元组列表

python - 为什么我在使用 fabric 时会收到提示?

python - setuptools_scm 可用时是否需要bumpversion(或bump2version)?

c++ - boost unordered_map 反向 foreach

c++ - 需要类型名错误(模板相关错误)

python - 如何在 pybind11 中将 python 函数转换/转换为 std::function<double(double*)> ?

python - 在 Python SimpleHTTPServer 中下载整个目录

c++ - QPixmap 图像不出现

c++ - 不确定使用函数宏作为 C++ 程序入口点的概念,

python - 在 python 中使用 C++ 对象与 pybind11