lang:c:pybind11
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| lang:c:pybind11 [2025/08/17 09:06] – 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"; | ||
| + | } | ||
| + | |||
| + | template < | ||
| + | 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" | ||
| ---- | ---- | ||
| - | ==== multithread | + | ===== python での使用方法 ===== |
| - | thread で pybind11 の module を実行しても、multithread にならない。 | + | <file python example.py> |
| - | source に以下を記述する。 | + | 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< | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| ---- | ---- | ||
| - | ===== Mac ===== | + | ===== threading/ |
| - | OS でデフォルトの python | + | python |
| - | デフォルトでは、python3-config がないので、 | + | 独立スレッドにするために、以下のコードを挿入する。 |
| - | Homebrew で python をインストールして、そちらを使う。 | + | |
| + | <file c++ thread.cpp> | ||
| + | void function(void){ | ||
| + | pybind11:: | ||
| + | // 処理 | ||
| + | pybind11:: | ||
| + | } | ||
| + | </ | ||
lang/c/pybind11.1755421585.txt.gz · 最終更新: by editor
