首页 > 编程语言 >python/c++ 混合编程

python/c++ 混合编程

时间:2023-01-20 16:00:21浏览次数:59  
标签:name python age 编程 c++ Pet api C++ pybind11

官方简介

pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. Its goals and syntax are similar to the excellent Boost.Python library by David Abrahams: to minimize boilerplate code in traditional extension modules by inferring type information using compile-time introspection.
google translate---->
pybind11是一个轻量级的“Header-only”的库,它将C++的类型暴露给Python,反之亦然。主要用于将已经存在的C++代码绑定到Python。pybind11的目标和语法都类似于boost.python库。通过使用编译时内省推断类型信息,最大限度地减少传统扩展模块中的样板代码。

C++代码api.cpp

#include <pybind11/pybind11.h> // 导入 pybind11 的 C++ 库
#include <iostream>

namespace py = pybind11;

int add(int i, int j) {
   return i + j;
}


class Pet
{
public:
    std::string name;
    int age;
    Pet() {}
    Pet(const std::string name_, const int age_){
        name = name_;
        age = age_;
    }
    ~Pet(){
        std::cout << "destructed" << std::endl;
    }
    void setName(std::string name_)
    {
        name = name_;
    }
    const std::string getName()
    {
        return name;
    }

};

PYBIND11_MODULE(api, m) {                                                                           // 创建一个 Python 模块,名为 api ,用变量 m 表示
    py::class_<Pet>(m, "Pet")                                                                       // 用 class_ 可以绑定一个 C++ 的 class 或 struct
        .def(py::init<const std::string, const int>(), py::arg("name") = "tom", py::arg("age") = 2) // 带有默认值的、关键字参数 构造函数初始化
        .def_readwrite("name", &Pet::name)                                                          // 绑定类变量
        .def_readonly("age", &Pet::age)                                                             // 绑定类变量并限制为只读(修改时会抛出 AttributeError 异常)
        .def("setName", &Pet::setName)                                                              // 绑定类方法
        .def("getName", &Pet::getName);
    m.doc() = "pybind11 example plugin";                                                            // 可选的模块说明
    m.def("add", &add, "a function which adds two numbers",                                         // 绑定函数
                py::arg("i") = 2,
                py::arg("j") = 3);
}

编译so文件

g++ api.cpp -o api.so -O3 -Wall -std=c++11 -shared -fPIC `python3 -m pybind11 --includes`

python调用

进入python终端后

Python 3.7.0 (default, Jun 28 2018, 13:15:42) 
[GCC 7.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import api
>>> api.add(i = 4,j = 5)
9
>>> p = api.Pet(age = 2233)
>>> p.name
'tom'
>>> p.age
2233
>>> 

参考链接

pybind11官方文档
demo参考

标签:name,python,age,编程,c++,Pet,api,C++,pybind11
From: https://www.cnblogs.com/liuyechang/p/17062826.html

相关文章

  • python 控制鼠标操作
    importtimeimportpyautogui##必须以管理员身份运行此程序,不然鼠标点击会没有效果!!!defmoveAndClick():print("startapp")x,y=976,748#鼠标需要......
  • python基础
    字面量被写下来的固定的值,成为字面量常见的6种值数字intfloatcomplex(复数,以j结尾表示复数),bool控制精度m.nm控制宽度(小数点也计入),设置的宽度小于数字自身不生......
  • 安装python
    1.官网安装地址: https://www.python.org/downloads/自定义安装:  python添加环境变量forallusers    AI学习建议安装版本:python:v3.8.10虚拟环......
  • 怎样借助WWW理解并较好地实现编程中的相关功能点
    首先分享一个课程https://www.icourse163.org/learn/SICNU-1002031014借助WWW来学习编程知识是必须的路径,那么怎样才能够如标题那样更好地实现呢?首先我们分析一下如果......
  • Python写一个简单的端口扫描器
    前言在日常的信息收集工作中,我们可以用Nmap来对目标进行信息收集,但这只是一般情况,在特殊情况中,比如我们没有任何工具来帮助我们收集信息,(假设)我们有Python环境,我们就需要自......
  • Python-sklearn初步实践
    Python-sklearn实践1sklearn实践Python有关于机器学习的库sklearn,我们可以使用sklearn来快速得到想要的机器学习算法,它不仅包括了很多学习算法,还包含了预处理、微调和评......
  • C++获取含有中文字符的string长度
    :前言造车轮的时候要用到中文字符串的长度辨别,发现char的识别不准,进行了一番研究。>开始研究在Windows下,中文字符在C++中的内存占用为2字节,此时采用字符串长度获取函......
  • Python图片识别之名片自动录入
    系统设计编写python程序,实现自动录入名片图片,识别名片上的文字信息,并附加到excel表内用pyqt设计名片录入框和信息显示框用汉王云识别名片图片,获取文字信息用pandas将......
  • Java/JS/Python/Go语言设计模式大全【精品源码】
    DesignPattern23种经典设计模式源码详解经典设计模式源码详解,用不同语言来实现,包括Java/JS/Python/TypeScript/Go等。结合实际场景,充分注释说明,每一行代码都经过检验,确......
  • python桌面应用自动化,uiautomation模块的Depth和searchDepth心得
    最近在学习yinkaisheng大神写的uiautomation模块,Depth和searchDepth一直使用不好,明明Depth=3,居然可以用searchDepth=1找到,网上也没找到答案,就自己试验了多次,终于发现了问题......