1. Download pybind11
https://github.com/pybind/pybind11
2. Create a new empty project in vs2019
1. Modify the output file format and file name ( need to be the same as the module name, which is critical )
There is only one main.cpp function
#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)"); // 函数名,函数地址,描述
}
2. Configure related header files
The specific operation steps can be viewed vs2013 reference third-party dynamic link library, set include, lib, dll path problem_cbzhunian's blog-CSDN blog
Configure the header file of pybind11, configure the header file of python environment
(It is best to create a new virtual environment and configure the virtual environment to see conda install Pytorch (GPU)_cbzhunian's blog-CSDN blog_conda install pytorch gpu )
3. Introduce the Python library (in the virtual environment)
4. Build file (x64 version)
example.pyd
3. Use this module
1. Copy the example.pyd file to the Lib folder in the virtual environment
(test is my own virtual environment)
2. Use the module
import example
print(example.add(1, 2))