Skip to content

Instantly share code, notes, and snippets.

@adityaiitb
Last active October 22, 2020 05:41
Show Gist options
  • Save adityaiitb/83dc9810c1b0c75e5ad27476b7530882 to your computer and use it in GitHub Desktop.
Save adityaiitb/83dc9810c1b0c75e5ad27476b7530882 to your computer and use it in GitHub Desktop.
pybind11

Pybind11 Doc

  • Create binding for a simple function
#include<pybind11/pybind11.h>

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

PYBIND11_MODULE(example, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring
    m.def("add", &add, "A function which adds two numbers");
}
  • Make
g++ -O3 -Wall -shared -std=c++11 -fPIC \
    `python3 -m pybind11 --includes` \
    example.cpp -o example`python3-config --extension-suffix`
  • Call from python
import example
example.add(2,3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment