使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)實現(xiàn)步驟
python調(diào)用C/C++有不少的方法,如boost.python, swig, ctypes, pybind11等,這些方法有繁有簡,而pybind11的優(yōu)點是對C++ 11支持很好,API比較簡單,現(xiàn)在我們就簡單記下Pybind11的入門操作。
pybind11簡介
pybind11是一個輕量級的只包含頭文件的庫,它主要是用來在已有的 C++代碼的基礎(chǔ)上做擴展,它的語法和目標非常像Boost.Python,但Boost.Python為了兼容現(xiàn)有的基本所有的C++編譯器而變得非常復(fù)雜和龐大,而因此付出的代價是很多晦澀的模板技巧以及很多不必要的對舊版編譯器的支持。Pybind11摒棄了這些支持,它只支持python2.7以上以及C++ 11以上的編譯器,使得它比Boost.Python更加簡潔高效。
在C語言中,結(jié)構(gòu)體(struct)指的是一種數(shù)據(jù)結(jié)構(gòu),是C語言中聚合數(shù)據(jù)類型(aggregate data type)的一類。結(jié)構(gòu)體可以被聲明為變量、指針或數(shù)組等,用以實現(xiàn)較復(fù)雜的數(shù)據(jù)結(jié)構(gòu)。結(jié)構(gòu)體同時也是一些元素的集合,這些元素稱為結(jié)構(gòu)體的成員(member),且這些成員可以為不同的類型,成員一般用名字訪問。
結(jié)構(gòu)體、結(jié)構(gòu)體指針作為函數(shù)的參數(shù)應(yīng)用的非常廣泛,本文介紹如何使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)。
一.需求分析
- 現(xiàn)有名為 student 的結(jié)構(gòu)體,有5個成員變量 name,Chinese,Mathematics,English和total ,構(gòu)造函數(shù)通過name生成實例,成員函數(shù) setName 可以給實例的name賦值;
- calc 函數(shù)接收一個student實例作為參數(shù),通過三門課程的分數(shù)計算出總分 total ;
- 將student,calc封裝到包含一個student類和一個calc函數(shù)的python模塊( abctest )中。
二.實現(xiàn)步驟
- 在頭文件中定義student結(jié)構(gòu)體,并聲明calc函數(shù);
- 在C++源文件中實現(xiàn)func.cpp函數(shù);
- 編寫pybind11封裝函數(shù);
- 用python編寫setup腳本;
- 編譯生成動態(tài)鏈接庫;
- 測試函數(shù)功能。
三.代碼實現(xiàn)
在頭文件中定義student結(jié)構(gòu)體,并聲明calc函數(shù)
//文件名:whjy.h #include <string> using namespace std; struct student{ string name; int Chinese; int Mathematics; int English; int total; student(string n){ this->name = n; } void setName(string stuName){ this->name = stuName; } }; void calc(struct student&);
在C++源文件中實現(xiàn)func.cpp函數(shù)
//文件名:func.cpp #include "whjy.h" #include <string> void calc(struct student& tyh){ tyh.total = tyh.Chinese + tyh.Mathematics + tyh.English; }
編寫pybind11封裝函數(shù)
//文件名:func_wrapper.cpp #include <pybind11/pybind11.h> #include "whjy.h" namespace py = pybind11; PYBIND11_MODULE(abctest, m){ m.doc() = "simple example"; py::class_<student>(m, "student") .def(py::init<string>()) .def("setName", &student::setName) .def_readonly("name", &student::name) .def_readwrite("Chinese", &student::Chinese) .def_readwrite("Mathematics", &student::Mathematics) .def_readwrite("English", &student::English) .def_readwrite("total", &student::total); m.def("calc", &calc); }
用python編寫setup腳本
#文件名:setup.py from setuptools import setup, Extension functions_module = Extension( name = 'abctest', sources = ['func.cpp', 'func_wrapper.cpp'], include_dirs = [r'D:\software\pybind11-master\include', r'D:\software\Anaconda\include'] ) setup(ext_modules = [functions_module])
編譯生成動態(tài)鏈接庫
在命令行執(zhí)行 python setup.py build_ext --inplace
,在當前路徑下生成pyd動態(tài)庫。
測試函數(shù)功能
#文件名:test.py import abctest s = abctest.student("小明") s.Chinese = 100 s.Mathematics = 110 s.English =120 abctest.calc(s) print(s.name + ":" + str(s.total) + "分") print("----------------------") s.setName("小紅") print(s.name + ":" + str(s.total) + "分")
output:
小明:330分
----------------------
小紅:330分
總結(jié)
到此這篇關(guān)于使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)pybind11封裝C++結(jié)構(gòu)體參數(shù)函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中的static和const的關(guān)鍵字用法詳解
這篇文章主要介紹了C++中的static和const的關(guān)鍵字用法詳解,這是一道經(jīng)常在面試中被問到的知識,本文給大家詳細介紹下,需要的朋友可以參考下2023-06-06C++算法之在無序數(shù)組中選擇第k小個數(shù)的實現(xiàn)方法
這篇文章主要介紹了C++算法之在無序數(shù)組中選擇第k小個數(shù)的實現(xiàn)方法,涉及C++數(shù)組的遍歷、判斷、運算等相關(guān)操作技巧,需要的朋友可以參考下2017-03-03