如何强制库(pybind11)包含来自Python3的<Python.h>?
How to force a library(pybind11) to include <Python.h> from Python3?
我正在使用pybind11库为我的C++代码创建Python绑定。
当我编译包含<pybind11/pybind11.h>
的绑定代码文件时,它会生成以下错误:
/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 生成绑定。
所以我尝试将其更改为#include "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/include/python3.7m/Python.h"
,现在库使用 Python 3.7 生成绑定,这就是我想要的。
虽然这种方法有效,但我想知道是否有任何更干净的方法可以使库始终包含来自 Python3 而不是 Python2 的标头。
提前谢谢你!
PS:我使用的是macOS 10.15.2
有几种方法,但 AFAIK,没有一种在所有平台上都是一致的(这就是为什么像 cmake 这样的东西(参见:https://github.com/pybind/cmake_example(通常是首选(。
首先,有python-config
,即添加:
`python-config --includes`
(带反引号(到 CLI。我的问题是,它是通过$PATH
找到的(因此,如果该安装没有python-config
,则不需要与您正在运行的python
版本匹配(,并且根据发行版的不同,python2
和python3
可能同时存在python-config
和python3-config
。
其次,有模块特性:
`python3 -c 'import distutils.sysconfig as ds; print(ds.get_python_inc())'`
它的优点是从您选择的实际python
运行。一般来说,distutils在各个平台上也不完全一致,但get_python_inc
是一个安全的选择。
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- Pybind11:将元组列表从Python传递到C++
- 如何在c++中使用引用实现类似python的行为
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 递归列出所有目录中的C++与Python与Ruby的性能
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- IPC使用多个管道和分支进程来运行Python程序
- 从python中调用C++函数并获取返回值
- Python 3.7 和 excess_args 的 SWIG 问题
- Python中的for循环与C++有何不同
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 使用Pybind11向Python公开Eigen::张量
- <<操作员在下面的行中工作
- Python str to C++ to Python str
- 如何使用Python从C++中读取谷物序列化数据
- 如何在C++中使用pybind11加载一个pickle python列表
- 如何在c++中使用system()来运行包含空格的python脚本
- python集合的C++等价物是什么.计数器
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python