当前位置: 首页 > news >正文

Windows下使用pybind11教程(python调用C++代码)

1. 下载pybind11

gittub中下载,pybind下载后解压
在这里插入图片描述

2. C++生成库文件

  • 2.1.VS新建空白工程,工程名随意起

在这里插入图片描述

- 2.2更改目标文件名和配置类型

在这里插入图片描述

- 2.3更改目标文件拓展名

在这里插入图片描述

  • 2.4添加include路径和库路径
    在这里插入图片描述
    包含目录中添加刚刚下载好的pybind的include路径以及pyhon的include文件,这里我使用的时Anaconda的下python的include:
E:\Software\pybind11-master\include
E:\ProgramData\Anaconda3\include

库目录为:

E:\ProgramData\Anaconda3\libs

- 2.5添加依赖项

链接器中输入E:\ProgramData\Anaconda3\libs下的lib名
在这里插入图片描述
在这里插入图片描述

- 2.6添加main.cpp文件

在这里插入图片描述

#include "pybind11/pybind11.h"int add(int i, int j)
{return i + j;
}PYBIND11_MODULE(example, m)  // 模块名example
{m.doc() = "add plugin";m.def("add", &add, "add function  (i, j)"); // 函数名,函数地址,描述
}

注意main.cpp文件中PYBIND11_MODULE(example, m)的example要与目标文件名一致

- 2.7编译

在这里插入图片描述
无法启动是正常的,工程目录下的release路径找到example.pyd
在这里插入图片描述

3.Python调用

- 3.1python中安装pybind11

pip install pybind11 -i https://pypi.tuna.tsinghua.edu.cn/simple

将example.pyd放到与test.py同目录下

- 3.2运行test.py

import example
print(example.add(1,2))

在这里插入图片描述

http://www.lryc.cn/news/177844.html

相关文章:

  • 基于通用LLM的一次测试用例自动生成的实验
  • 【excel密码】为什么工作表不能移动、复制了?
  • 软考高级之系统架构师之计算机基础
  • Mysql生产随笔
  • 现代卷积网络实战系列2:PyTorch构建训练函数、LeNet网络
  • leetCode 62.不同路径 动态规划 + 空间复杂度优化
  • 在 .NET 8 Release Candidate 1 中推出 .NET MAUI:质量
  • Spring 学习(八)事务管理
  • CodeTON Round 6 (Div 1 + Div 2, Rated, Prizes!)(A - E)
  • Spring 源码分析(五)——Spring三级缓存的作用分别是什么?
  • Django基于类视图实现增删改查
  • matplotlib绘图实现中文宋体的两种方法(亲测)
  • 非常有用的JavaScript高阶面试技巧!
  • windows 安装Linux子系统 Ubuntu 并配置python3
  • pytorch的pixel_shuffle转tflite文件
  • sentinel-dashboard-1.8.0.jar开机自启动脚本
  • c++堆排序-建堆-插入-删除-排序
  • 使用代理后pip install 出现ssl错误
  • 护眼灯什么价位的好?最具性价比的护眼台灯推荐
  • vue event bus 事件总线
  • 深信服云桌面用户忘记密码后的处理
  • Cocos Creator3.8 实战问题(一)cocos creator prefab 无法显示内容
  • 朴素贝叶斯深度解码:从原理到深度学习应用
  • RUST 每日一省:闭包
  • Ubuntu下文件的解压缩操作:常用zip和unzip
  • Linux学习第22天:Linux中断驱动开发(一): 突如其来
  • IDEA 2019 Springboot 3.1.3 运行异常
  • 【JAVA】飞机大战
  • Midjourney 生成油画技巧
  • 26559-2021 机械式停车设备 分类