pybind11

使用 visual studio环境

安装Visual Studio社区版:https://visualstudio.microsoft.com/zh-hans/downloads/

需要选中windows SDK,否则可能有些头文件缺失。

<p>pybind11</p>

pybind11官网:pybind/pybind11: Seamless operability between C++11 and Python (github.com), 从release中现在最新版本

示例:

在计划调用pybind11的python项目文件夹内创建一个文件夹vspybind11, 并使用该文件夹创建一个空的visual studio C++项目,项目和解决方案名称为 vspybind11, 并且在同一个文件夹内

打开VS,右键点击工程选择属性,配置属性>>常规>>配置 设置成dll

<p>pybind11</p>

配置属性>>VC++目录>>包含目录:

  • python 的 include 目录: D:\miniconda3\include
  • pybind11 的 include 目录: d:\pybind11-2.10.4\include

配置属性>>VC++目录>>库目录:D:\miniconda3\libs

链接器>>输入>>附加依赖项,查看目录 D:\miniconda3\libs ,加入对应内容:

D:\miniconda3\envs\pytest\libs\python3.lib
D:\miniconda3\envs\pytest\libs\python311.lib

新建c++文件 vspybind11.cpp, 并添加到解决方案源文件中

#include <pybind11\pybind11.h>
#include <iostream>

namespace py = pybind11;

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

double add_double(double a,double b) {
    return a + b;
}

PYBIND11_MODULE(pb4py, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring

    m.def("add_int", &add_int, "A function which adds two numbers");

    m.def("add_double", &add_double, "A function which adds two numbers");
}


<p>pybind11</p>

选择项目,点击生成(生成或重新生成!!!!),如果显示成功即可说明pybind11已经链接成功!

将生成的dll改成pyd后缀,并重命名为pybind11.pyd(与PYBIND11_MODULE名称相同),然后,复制到py同级目录中就可以调用了。可以在项目配置,生成后事件中拷贝文件:

copy $(TargetDir)$(ProjectName).dll $(ProjectDir)$(ProjectName).pyd

通过python调试:项目根目录下创建 testPybind11.py

import sys
import pybind11
print("3+4=",pybind11.add_int(3,4))
print("3.1+4.1=",pb4py.add_double(3.1,4.1))



使用Clion环境(工具链为MinGW)

1.安装 Clion 到 C:\JetBrains\CLion2023.1.4, 添加工具链 MinGW

2.创建c++库项目 testPybind11, 标准用C++23,库类型 shared, 项目目录这里设置为 D:\CLionProjects\testPybind11, 这里会自动创建 library.cpp,重构文件名为 pb11.cpp, 删除pb11.h, 创建文件夹 D:\CLionProjects\testPybind11\pb11

3.安装miniconda到d:\miniconda,

  • 安装时需要将python变量加入到windows的path
  • 给Clion添加python解释器 D:\miniconda3\python.exe

4.pybind11官网:pybind/pybind11: Seamless operability between C++11 and Python (github.com), 从release中下载最新版本,copy到 D:\CLionProjects\testPybind11\pybind11-2.10.4

5.CMakeLists.txt 文件内容:

cmake_minimum_required(VERSION 3.24)
project(testPybind11)

set(CMAKE_CXX_STANDARD 23)

add_subdirectory("D:/CLionProjects/testPybind11/pybind11-2.10.4/")

pybind11_add_module(pb11 pb11.cpp)

# 构建后拷贝指定文件到指定目录
add_custom_command(TARGET pb11
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E
        copy
        D:/CLionProjects/testPybind11/cmake-build-debug/pb11.cp310-win_amd64.pyd
        D:/CLionProjects/testPybind11/pb11/
        )

6.pb11.cpp 添加如下内容:

#include <pybind11/pybind11.h>
#include <iostream>

using namespace std;

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

double add_double(double i, double j) {
    return i + j;
}

PYBIND11_MODULE(pb11, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring

m.def("add_int", &add_int, "A function which adds two integer numbers");

m.def("add_double", &add_double, "A function which adds two double numbers");

}

7.项目根目录下创建 testPybind11.py

import sys
sys.path.append("D:/CLionProjects/testPybind11/pb11")
import pb11
print("3+4=",pb11.add_int(3,4))
print("3.1+4.1=",pb11.add_double(3.1,4.1))

构建后调用报错:

>>> import pb11
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing pb11: 找不到指定的模块。

使用 Dependencies, 打开 DependenciesGui.exe, 将构建生成的 pb11.cp310-win_amd64.pyd 拖到 DependenciesGui.exe,提示缺少库 libgcc_s_seh-1.dll, libstdc++-6.dll, 将 C:\JetBrains\CLion2023.1.4\bin\mingw\bin 下的dll 拷贝到 D:\CLionProjects\testPybind11\pb11 目录下

8.构建需要特定的python版本的dll, 如果python版本升级无法构建,清理环境、删除cmake-build-debug文件夹重新构建

9.调试:指定python解释器,运行python脚本

<p>pybind11</p>

输出:

D:\miniconda3\python.exe D:\CLionProjects\testPybind11\testPybind11.py 
3+4= 7
3.1+4.1= 7.199999999999999

正文完
 
评论(没有评论)