ipython 中使用 pybind11 生成的 example.pyd 一卡一卡的
資深大佬 : XIVN1987 3
按照网上的说明,写了个最简单的 pybind11 例程,,操作方法如后面所述,,用的编译器是 tdm64-gcc-9.2.0.exe ,编译生成了 example.pyd ,用 ipython 导入后 ipython 一卡一卡的、光标一直在转圈,,调用 example.add 倒是也能产生正确值,,可是一直很卡,卡到只能关掉 ipython
换了标准的 python.exe 和 jupyter,都没有卡顿的现象
猜测是不是 ipython 对 xxx.pyd 有什么额外要求,,一直在查询 example.pyd 的某个属性,,差不多所以就一卡一卡的??
哪位大神知道原因,,希望指点一下,,谢谢。。
pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code
入门
安装
pip3 install pybind11
编码
#include <pybind11/pybind11.h> int add(int i, int j) { return i + j; } PYBIND11_MODULE(example, m) { m.doc() = "pybind11 example plugin"; m.def("add", &add, "A function which adds two numbers"); }
编译
C:Ubuntubingcc-winbing++.exe -shared -std=c++11 -DMS_WIN64 -fPIC -Wall -IC:Python36include -IC:Python36Libsite-packagespybind11include -LC:Python36libs example.cc -o example.pyd -lPython36
编译可能报错:
C:/Ubuntu/bin/gcc-win/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/cmath:1121:11: error: '::hypot' has not been declared
原因是在 pyconfig.h 中定义 hypot as _hypot,所以解决方法是:在 cmath 文件头部添加
#define _hypot hypot
测试
import example example.add(2, 3)
5
大佬有話說 (0)