{"id":247027,"date":"2020-12-22T13:37:06","date_gmt":"2020-12-22T05:37:06","guid":{"rendered":"http:\/\/4563.org\/?p=247027"},"modified":"2020-12-22T13:37:06","modified_gmt":"2020-12-22T05:37:06","slug":"ipython-%e4%b8%ad%e4%bd%bf%e7%94%a8-pybind11-%e7%94%9f%e6%88%90%e7%9a%84-example-pyd-%e4%b8%80%e5%8d%a1%e4%b8%80%e5%8d%a1%e7%9a%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=247027","title":{"rendered":"ipython \u4e2d\u4f7f\u7528 pybind11 \u751f\u6210\u7684 example.pyd \u4e00\u5361\u4e00\u5361\u7684"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  ipython \u4e2d\u4f7f\u7528 pybind11 \u751f\u6210\u7684 example.pyd \u4e00\u5361\u4e00\u5361\u7684               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : XIVN1987 <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6309\u7167\u7f51\u4e0a\u7684\u8bf4\u660e\uff0c\u5199\u4e86\u4e2a\u6700\u7b80\u5355\u7684 pybind11 \u4f8b\u7a0b\uff0c\uff0c\u64cd\u4f5c\u65b9\u6cd5\u5982\u540e\u9762\u6240\u8ff0\uff0c\uff0c\u7528\u7684\u7f16\u8bd1\u5668\u662f tdm64-gcc-9.2.0.exe \uff0c\u7f16\u8bd1\u751f\u6210\u4e86 example.pyd \uff0c\u7528 ipython \u5bfc\u5165\u540e ipython \u4e00\u5361\u4e00\u5361\u7684\u3001\u5149\u6807\u4e00\u76f4\u5728\u8f6c\u5708\uff0c\uff0c\u8c03\u7528 example.add \u5012\u662f\u4e5f\u80fd\u4ea7\u751f\u6b63\u786e\u503c\uff0c\uff0c\u53ef\u662f\u4e00\u76f4\u5f88\u5361\uff0c\u5361\u5230\u53ea\u80fd\u5173\u6389 ipython<\/p>\n<p>\u6362\u4e86\u6807\u51c6\u7684 python.exe \u548c jupyter\uff0c\u90fd\u6ca1\u6709\u5361\u987f\u7684\u73b0\u8c61<\/p>\n<p>\u731c\u6d4b\u662f\u4e0d\u662f ipython \u5bf9 xxx.pyd \u6709\u4ec0\u4e48\u989d\u5916\u8981\u6c42\uff0c\uff0c\u4e00\u76f4\u5728\u67e5\u8be2 example.pyd \u7684\u67d0\u4e2a\u5c5e\u6027\uff0c\uff0c\u5dee\u4e0d\u591a\u6240\u4ee5\u5c31\u4e00\u5361\u4e00\u5361\u7684\uff1f\uff1f<\/p>\n<p>\u54ea\u4f4d\u5927\u795e\u77e5\u9053\u539f\u56e0\uff0c\uff0c\u5e0c\u671b\u6307\u70b9\u4e00\u4e0b\uff0c\uff0c\u8c22\u8c22\u3002\u3002<\/p>\n<p>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<\/p>\n<h2>\u5165\u95e8<\/h2>\n<h3>\u5b89\u88c5<\/h3>\n<pre><code>pip3 install pybind11 <\/code><\/pre>\n<h3>\u7f16\u7801<\/h3>\n<pre><code>#include &lt;pybind11\/pybind11.h&gt;  int add(int i, int j) {     return i + j; }  PYBIND11_MODULE(example, m) {     m.doc() = \"pybind11 example plugin\";      m.def(\"add\", &amp;add, \"A function which adds two numbers\"); } <\/code><\/pre>\n<h3>\u7f16\u8bd1<\/h3>\n<pre><code>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 <\/code><\/pre>\n<p>\u7f16\u8bd1\u53ef\u80fd\u62a5\u9519\uff1a<\/p>\n<pre><code>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 <\/code><\/pre>\n<p>\u539f\u56e0\u662f\u5728 pyconfig.h \u4e2d\u5b9a\u4e49 hypot as _hypot\uff0c\u6240\u4ee5\u89e3\u51b3\u65b9\u6cd5\u662f\uff1a\u5728 cmath \u6587\u4ef6\u5934\u90e8\u6dfb\u52a0<\/p>\n<pre><code>#define _hypot hypot <\/code><\/pre>\n<h3>\u6d4b\u8bd5<\/h3>\n<pre><code>import example  example.add(2, 3) <\/code><\/pre>\n<pre><code>5 <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>ipython \u4e2d\u4f7f\u7528 pybin&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/247027"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=247027"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/247027\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=247027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=247027"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=247027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}