怎么使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)

這篇文章主要介紹怎么使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù),文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

五寨網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司從2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。

python調(diào)用C/C++有不少的方法,如boost.python, swig, ctypes, pybind11等,這些方法有繁有簡,而pybind11的優(yōu)點是對C++ 11支持很好,API比較簡單,現(xiàn)在我們就簡單記下Pybind11的入門操作。

pybind11簡介

pybind11是一個輕量級的只包含頭文件的庫,它主要是用來在已有的 C++代碼的基礎(chǔ)上做擴展,它的語法和目標(biāo)非常像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ù),通過三門課程的分?jǐn)?shù)計算出總分 total ;

  • 將student,calc封裝到包含一個student類和一個calc函數(shù)的python模塊( abctest )中。

二.實現(xiàn)步驟

  1. 在頭文件中定義student結(jié)構(gòu)體,并聲明calc函數(shù);

  2. 在C++源文件中實現(xiàn)func.cpp函數(shù);

  3. 編寫pybind11封裝函數(shù);

  4. 用python編寫setup腳本;

  5. 編譯生成動態(tài)鏈接庫;

  6. 測試函數(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 ,在當(dāng)前路徑下生成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++結(jié)構(gòu)體作為參數(shù)的函數(shù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文標(biāo)題:怎么使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)
新聞來源:http://www.muchs.cn/article46/ippgeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、虛擬主機外貿(mào)建站、網(wǎng)站設(shè)計、網(wǎng)站設(shè)計公司、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站