Skip to content

Instantly share code, notes, and snippets.

@rampluto
Last active December 28, 2022 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rampluto/885ed39b4c986dfa2d3f182ac4803df7 to your computer and use it in GitHub Desktop.
Save rampluto/885ed39b4c986dfa2d3f182ac4803df7 to your computer and use it in GitHub Desktop.
Creating Python Module from CPP implementation using pybind11
import p1
from p1 import Rlenv
a = 123
b = 234
print(a,b)
print("Adding ",a,b)
res = p1.add(a,b)
print("Result is",res)
class Home(Rlenv):
pass
h = Home()
print(h.sub(4,3))
#include<vector>
#include<iostream>
#include<pybind11/pybind11.h>
#include<pybind11/stl.h>
using namespace std;
namespace py = pybind11;
class Rlenv{
public:
Rlenv(){}
int sub(int a, int b){
return a-b;
}
};
int add(int a, int b){
return a+b;
}
PYBIND11_MODULE(p1, m){
m.doc() = "Example pybind11 plugin";
m.def("add",&add);
py::class_<Rlenv>(m,"Rlenv")
.def(py::init<>())
.def("sub",&Rlenv::sub);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment