Skip to content

Instantly share code, notes, and snippets.

@sandeepkumar-skb
Created October 20, 2020 03:34
Show Gist options
  • Save sandeepkumar-skb/c89e30ac9ee7e1795150796afc3ccc41 to your computer and use it in GitHub Desktop.
Save sandeepkumar-skb/c89e30ac9ee7e1795150796afc3ccc41 to your computer and use it in GitHub Desktop.
PYBIND11 example
#include <pybind11/pybind11.h>
int add(int i, int j){
return i+j;
}
PYBIND11_MODULE(pybind11_example, m) {
m.doc() = "pybind11 example to add 2 integers";
m.def("add", &add, "A function to add 2 integers");
}
@sandeepkumar-skb
Copy link
Author

sandeepkumar-skb commented Oct 20, 2020

compile:

c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` pybind11_example.cpp -o pybind11_example`python3-config --extension-suffix`
>>> import pybind11_example as pb
>>> pb.add(1,2)
3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment