Last active
December 28, 2022 16:33
-
-
Save rampluto/885ed39b4c986dfa2d3f182ac4803df7 to your computer and use it in GitHub Desktop.
Creating Python Module from CPP implementation using pybind11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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