使用pybind11封装C++结构体作为参数的函数实现步骤

2020-02-25 12:06:00丽君

用python编写setup脚本

#文件名:setup.py
from setuptools import setup, Extension 
 
functions_module = Extension( 
 name = 'abctest', 
 sources = ['func.cpp', 'func_wrapper.cpp'], 
 include_dirs = [r'D:softwarepybind11-masterinclude', 
     r'D:softwareAnacondainclude'] 
) 
 
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分

总结

到此这篇关于使用pybind11封装C++结构体作为参数的函数的实现步骤的文章就介绍到这了,更多相关pybind11封装C++结构体参数函数内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!