在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类。结构体能够被声明为变量、指针或数组等,用以实现较复杂的数据结构。结构体同时也是一些元素的集合,这些元素称为结构体的成员(member),且这些成员能够为不一样的类型,成员通常用名字访问。python
结构体、结构体指针做为函数的参数应用的很是普遍,本文介绍如何使用pybind11封装C++结构体做为参数的函数。数组
student
的结构体,有5个成员变量name,Chinese,Mathematics,English和total
,构造函数经过name生成实例,成员函数setName
能够给实例的name赋值;calc
函数接收一个student实例做为参数,经过三门课程的分数计算出总分total
;abctest
)中。//文件名: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&);
//文件名:func.cpp #include "whjy.h" #include <string> void calc(struct student& tyh){ tyh.total = tyh.Chinese + tyh.Mathematics + tyh.English; }
//文件名: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); }
#文件名: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])
在命令行执行python setup.py build_ext --inplace
,在当前路径下生成pyd动态库。数据结构
#文件名: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分
app