這篇文章主要介紹怎么使用pybind11封裝C++結構體作為參數(shù)的函數(shù),文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
五寨網站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網頁設計、網站建設、微信開發(fā)、APP開發(fā)、響應式網站建設等網站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司從2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選成都創(chuàng)新互聯(lián)公司。
python調用C/C++有不少的方法,如boost.python, swig, ctypes, pybind11等,這些方法有繁有簡,而pybind11的優(yōu)點是對C++ 11支持很好,API比較簡單,現(xiàn)在我們就簡單記下Pybind11的入門操作。
pybind11簡介
pybind11是一個輕量級的只包含頭文件的庫,它主要是用來在已有的 C++代碼的基礎上做擴展,它的語法和目標非常像Boost.Python,但Boost.Python為了兼容現(xiàn)有的基本所有的C++編譯器而變得非常復雜和龐大,而因此付出的代價是很多晦澀的模板技巧以及很多不必要的對舊版編譯器的支持。Pybind11摒棄了這些支持,它只支持python2.7以上以及C++ 11以上的編譯器,使得它比Boost.Python更加簡潔高效。
在C語言中,結構體(struct)指的是一種數(shù)據(jù)結構,是C語言中聚合數(shù)據(jù)類型(aggregate data type)的一類。結構體可以被聲明為變量、指針或數(shù)組等,用以實現(xiàn)較復雜的數(shù)據(jù)結構。結構體同時也是一些元素的集合,這些元素稱為結構體的成員(member),且這些成員可以為不同的類型,成員一般用名字訪問。
結構體、結構體指針作為函數(shù)的參數(shù)應用的非常廣泛,本文介紹如何使用pybind11封裝C++結構體作為參數(shù)的函數(shù)。
現(xiàn)有名為 student 的結構體,有5個成員變量 name,Chinese,Mathematics,English和total ,構造函數(shù)通過name生成實例,成員函數(shù) setName 可以給實例的name賦值;
calc 函數(shù)接收一個student實例作為參數(shù),通過三門課程的分數(shù)計算出總分 total ;
將student,calc封裝到包含一個student類和一個calc函數(shù)的python模塊( abctest )中。
在頭文件中定義student結構體,并聲明calc函數(shù);
在C++源文件中實現(xiàn)func.cpp函數(shù);
編寫pybind11封裝函數(shù);
用python編寫setup腳本;
編譯生成動態(tài)鏈接庫;
測試函數(shù)功能。
在頭文件中定義student結構體,并聲明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分
以上是“怎么使用pybind11封裝C++結構體作為參數(shù)的函數(shù)”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文標題:怎么使用pybind11封裝C++結構體作為參數(shù)的函數(shù)
新聞來源:http://www.chinadenli.net/article46/ippgeg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、虛擬主機、外貿建站、網站設計、網站設計公司、自適應網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)