最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)實(shí)現(xiàn)步驟

 更新時(shí)間:2020年02月25日 09:29:04   作者:追憶  
這篇文章主要介紹了用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)實(shí)現(xiàn)步驟,本文分步驟通過(guò)實(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的入門操作。

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),且這些成員可以為不同的類型,成員一般用名字訪問。

結(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ò)三門課程的分?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. 編寫pybind11封裝函數(shù);
  4. 用python編寫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; 
}

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

編譯生成動(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分

總結(jié)

到此這篇關(guān)于使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)pybind11封裝C++結(jié)構(gòu)體參數(shù)函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

曲水县| 名山县| 芷江| 城步| 衢州市| 丹江口市| 玛多县| 富川| 英吉沙县| 登封市| 桑日县| 隆昌县| 溆浦县| 屏东县| 察隅县| 云安县| 宜君县| 汝南县| 五河县| 南开区| 汉中市| 洞口县| 元阳县| 马公市| 柳州市| 乌拉特后旗| 临泽县| 博客| 承德市| 定结县| 庐江县| 云林县| 天门市| 正定县| 宁乡县| 平山县| 三门峡市| 元朗区| 绍兴县| 崇左市| 洛阳市|