跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • ipython 中使用 pybind11 生成的 example.pyd 一卡一卡的
未分類
22 12 月 2020

ipython 中使用 pybind11 生成的 example.pyd 一卡一卡的

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)

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具