lang:c:pybind11
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
lang:c:pybind11 [2025/08/11 10:45] – editor | lang:c:pybind11 [2025/09/06 12:45] (現在) – [Windows の場合] editor | ||
---|---|---|---|
行 1: | 行 1: | ||
====== pybind11 ====== | ====== pybind11 ====== | ||
- | ===== インストール ===== | + | C/C++ のコードを python で利用する。\\ |
- | ===== コンパイル/ | + | 個人的には、boost よりも使いやすい。 |
- | ===== 実装 ===== | ||
- | ==== 関数 ==== | ||
- | ==== クラス ==== | ||
- | ==== template ==== | + | ===== 基本 ===== |
+ | <file python exPybind11.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #include < | ||
+ | |||
+ | void functionInPython(void){ | ||
+ | std::cout << "call functionInCPP\n"; | ||
+ | } | ||
+ | |||
+ | class ClassInCpp { | ||
+ | |||
+ | public: | ||
+ | |||
+ | ClassInCpp(void) : varInClassInCpp(" | ||
+ | |||
+ | std::string varInClassInCpp ; | ||
+ | |||
+ | void functionClassInCpp(void){ | ||
+ | std::cout << "call functionClassInCpp\n"; | ||
+ | } | ||
+ | |||
+ | | ||
+ | TT1 templateFunctionInCpp(TT1 x){ | ||
+ | std::cout << "call templateFunctionInCpp -arg= " << x << " | ||
+ | |||
+ | return x * x; | ||
+ | |||
+ | } | ||
+ | |||
+ | }; | ||
+ | |||
+ | PYBIND11_MODULE(examplePybind11, | ||
+ | |||
+ | m.def(" | ||
+ | |||
+ | pybind11:: | ||
+ | .def(pybind11:: | ||
+ | .def_readwrite(" | ||
+ | .def(" | ||
+ | |||
+ | // template function | ||
+ | .def(" | ||
+ | | ||
+ | .def(" | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== complile ===== | ||
+ | ==== Linux/Mac の場合 ==== | ||
+ | c++ exPybind11.cpp -o examplePybind11.so -std=c++11 -shared -fPIC \ | ||
+ | -I" | ||
+ | `/ | ||
+ | |||
+ | ここで、**examplePybind11.so** の prefix (" | ||
+ | " | ||
+ | |||
+ | ==== Windows の場合 ==== | ||
+ | コンパイラや環境に依存すると思われる。\\ | ||
+ | 以下は、" | ||
+ | |||
+ | c++ exPybind11.cpp -o examplePybind11`python-config --extension-suffix` \ | ||
+ | -std=c++11 -shared -fPIC \ | ||
+ | -I" | ||
+ | `python-config --cflags --ldflags` | ||
+ | |||
+ | 上記の -I" | ||
+ | ---- | ||
+ | ===== python での使用方法 ===== | ||
+ | <file python example.py> | ||
+ | import os | ||
+ | import sys | ||
+ | |||
+ | import examplePybind11 | ||
+ | |||
+ | examplePybind11.functionInPython() | ||
+ | |||
+ | c = examplePybind11.classInPython() | ||
+ | |||
+ | print(c.varInPython) | ||
+ | |||
+ | c.functionClassInPython() | ||
+ | |||
+ | v = c.templateFunctionInPython(2) | ||
+ | print (v) | ||
+ | |||
+ | v = c.templateFunctionInPython(1.1) | ||
+ | print (v) | ||
+ | |||
+ | </ | ||
+ | |||
+ | ----- | ||
+ | ===== list の使用 ===== | ||
+ | <file python list.cpp> | ||
+ | |||
+ | void func(pybind11: | ||
+ | for (uint64_t i=0; i< | ||
+ | pybind11:: | ||
+ | std::string x = item.cast< | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | ===== threading/ | ||
+ | python で threading からの呼び出しでは、独立スレッドにならない。 | ||
+ | 独立スレッドにするために、以下のコードを挿入する。 | ||
+ | |||
+ | <file c++ thread.cpp> | ||
+ | void function(void){ | ||
+ | pybind11:: | ||
+ | // 処理 | ||
+ | pybind11:: | ||
+ | } | ||
+ | </ | ||
- | ===== Mac ===== | ||
- | OS でデフォルトの python では、リンク時にエラーになる場合がある。 | ||
- | そもそもデフォルトでは、python3-config がないので、 | ||
- | Homebrew で python をインストールして、そちらを使う。 | ||
lang/c/pybind11.1754909156.txt.gz · 最終更新: by editor