溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-05-24 11:24:49 來(lái)源:億速云 閱讀:343 作者:小新 欄目:編程語(yǔ)言

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

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

pybind11簡(jiǎn)介

pybind11是一個(gè)輕量級(jí)的只包含頭文件的庫(kù),它主要是用來(lái)在已有的 C++代碼的基礎(chǔ)上做擴(kuò)展,它的語(yǔ)法和目標(biāo)非常像Boost.Python,但Boost.Python為了兼容現(xiàn)有的基本所有的C++編譯器而變得非常復(fù)雜和龐大,而因此付出的代價(jià)是很多晦澀的模板技巧以及很多不必要的對(duì)舊版編譯器的支持。Pybind11摒棄了這些支持,它只支持python2.7以上以及C++ 11以上的編譯器,使得它比Boost.Python更加簡(jiǎn)潔高效。

在C語(yǔ)言中,結(jié)構(gòu)體(struct)指的是一種數(shù)據(jù)結(jié)構(gòu),是C語(yǔ)言中聚合數(shù)據(jù)類型(aggregate data type)的一類。結(jié)構(gòu)體可以被聲明為變量、指針或數(shù)組等,用以實(shí)現(xiàn)較復(fù)雜的數(shù)據(jù)結(jié)構(gòu)。結(jié)構(gòu)體同時(shí)也是一些元素的集合,這些元素稱為結(jié)構(gòu)體的成員(member),且這些成員可以為不同的類型,成員一般用名字訪問(wèn)。

結(jié)構(gòu)體、結(jié)構(gòu)體指針作為函數(shù)的參數(shù)應(yīng)用的非常廣泛,本文介紹如何使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)。

一.需求分析

  • 現(xiàn)有名為 student 的結(jié)構(gòu)體,有5個(gè)成員變量 name,Chinese,Mathematics,English和total ,構(gòu)造函數(shù)通過(guò)name生成實(shí)例,成員函數(shù) setName 可以給實(shí)例的name賦值;

  • calc 函數(shù)接收一個(gè)student實(shí)例作為參數(shù),通過(guò)三門(mén)課程的分?jǐn)?shù)計(jì)算出總分 total ;

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

二.實(shí)現(xiàn)步驟

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

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

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

  4. 用python編寫(xiě)setup腳本;

  5. 編譯生成動(dòng)態(tài)鏈接庫(kù);

  6. 測(cè)試函數(shù)功能。

三.代碼實(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++源文件中實(shí)現(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; 
}

編寫(xiě)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編寫(xiě)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])

編譯生成動(dòng)態(tài)鏈接庫(kù)

在命令行執(zhí)行 python setup.py build_ext --inplace ,在當(dāng)前路徑下生成pyd動(dòng)態(tài)庫(kù)。

測(cè)試函數(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)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI