ユーザ用ツール

サイト用ツール


lang:c:pybind11

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
lang:c:pybind11 [2025/08/17 09:35] editorlang:c:pybind11 [2025/09/06 12:45] (現在) – [Windows の場合] editor
行 1: 行 1:
 ====== pybind11 ====== ====== pybind11 ======
-===== インストル =====+C/C++ のコドを python で利用する。\\  
 +個人的には、boost よりも使いやすい。
  
----- 
-===== コンパイル/リンク ===== 
-リンク時に以下を追加する 
-  `python3-config --cflags --ldflags` -fPIC -shared 
  
----- +===== 基本 ===== 
-===== 実装 ===== +<file python exPybind11.cpp> 
-==== 関数 ==== +#include <iostream> 
-==== クラス ====+#include <cstdio> 
 +#include <cstdlib>
  
-==== template ====+#include <pybind11/pybind11.h>
  
 +void functionInPython(void){
 +  std::cout << "call functionInCPP\n";
 +}
 +
 +class ClassInCpp {
 +
 +public:
 +
 +  ClassInCpp(void) : varInClassInCpp("varInClassInCpp"){ }
 +
 +  std::string varInClassInCpp ;
 +
 +  void functionClassInCpp(void){
 +    std::cout << "call functionClassInCpp\n";
 +  }
 +
 +  template <typename TT1>
 +  TT1 templateFunctionInCpp(TT1 x){
 +    std::cout << "call templateFunctionInCpp -arg= " << x << "\n";
 +
 +    return x * x;
 +
 +  }
 +
 +};
 +
 +PYBIND11_MODULE(examplePybind11, m) {
 +
 +  m.def("functionInPython", &functionInPython);
 +
 +  pybind11::class_<ClassInCpp>(m, "classInPython")
 +    .def(pybind11::init<>())
 +    .def_readwrite("varInPython", &ClassInCpp::varInClassInCpp)
 +    .def("functionClassInPython", &ClassInCpp::functionClassInCpp)
 +
 +    // template function
 +    .def("templateFunctionInPython",
 +         (uint64_t (ClassInCpp::*)(uint64_t)) &ClassInCpp::templateFunctionInCpp)
 +    .def("templateFunctionInPython",
 +         (float (ClassInCpp::*)(float )) &ClassInCpp::templateFunctionInCpp);
 +}
 +</file>
 +
 +===== complile =====
 +==== Linux/Mac の場合 ====
 +  c++ exPybind11.cpp -o examplePybind11.so -std=c++11 -shared -fPIC \
 +  -I"pybindIncludeDir" \
 +  `/usr/bin/python3-config --cflags --ldflags`
 +  
 +ここで、**examplePybind11.so** の prefix (".so" を除外した部分) は、\\ 
 +"PYBIND11_MODULE(examplePybind11, m)" の第一引数と同じこと。
 +
 +==== Windows の場合 ====
 +コンパイラや環境に依存すると思われる。\\ 
 +以下は、"UCRT64/mingw-w64-ucrt-x86_64-python 3.12.11-1" の場合。
 +
 +  c++ exPybind11.cpp -o examplePybind11`python-config --extension-suffix` \
 +  -std=c++11 -shared -fPIC \
 +  -I"pybind11IncludeDir" \
 +  `python-config --cflags --ldflags`
 +
 +上記の -I"pybind11IncludeDir" は環境にあわせる。
 ---- ----
-==== 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)
 +
 +</file>
 +
 +-----
 +===== list の使用 =====
 +<file python list.cpp>
 +
 +void func(pybind11:list pylist){
 +  for (uint64_t i=0; i<pylist.size(); i++){
 +    pybind11::handle item = pylist[i];
 +    std::string x = item.cast<std::string>();
 +  }
 +
 +</file>
 ---- ----
-===== Mac ===== +===== threading/pthead などでの利用 ===== 
-OS デフォルトの python では、リンク時にエラーになる場合がある。 +python で threading から呼び出しでは、独立スレッドになない 
-デフォルトでは、python3-config がないので、 +独立レッドにするために以下のコード挿入する 
-Homebrew で python をイントールしてそちら使う+ 
 +<file c++ thread.cpp> 
 +void function(void){ 
 +  pybind11::gil_scoped_release release; 
 +  // 処理 
 +  pybind11::gil_scoped_acquire acquire; 
 +
 +</file> 
  
  
lang/c/pybind11.1755423325.txt.gz · 最終更新: by editor

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki