Skip to content

Commit d85a646

Browse files
authored
Added basic example of using pybind11 (#86)
* added basic example of using pybind11 Signed-off-by: Ross <[email protected]> * cleaned up module.bazel and removed Workspace Signed-off-by: Ross <[email protected]> * cleaned up module.bazel and lock file Signed-off-by: Ross <[email protected]> * renamed cpp and py files, and simplifed the example Signed-off-by: Ross <[email protected]> * updated build.bazel to be easier to understand and simplier Signed-off-by: Ross <[email protected]> * removed python toolchain Signed-off-by: Ross <[email protected]> --------- Signed-off-by: Ross <[email protected]> Co-authored-by: Ross <[email protected]>
1 parent ea71d97 commit d85a646

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

examples/basic/BUILD.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
2+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
3+
4+
pybind_extension(
5+
name = "basic",
6+
srcs = ["basic.cpp"],
7+
)
8+
py_library(
9+
name = "basic_lib",
10+
data = [":basic"],
11+
imports = ["."],
12+
)
13+
py_test(
14+
name = "basic_test",
15+
srcs = ["basic_test.py"],
16+
deps = [":basic_lib"],
17+
)

examples/basic/MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bazel_dep(name = "pybind11_bazel", version = "2.12.0")
2+
bazel_dep(name = "rules_python", version = "0.33.1")
3+

examples/basic/basic.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <pybind11/pybind11.h>
2+
3+
int add(int i, int j) {
4+
return i + j;
5+
}
6+
7+
PYBIND11_MODULE(basic, module) {
8+
module.doc() = "A basic pybind11 extension";
9+
module.def("add", &add, "A function that adds two numbers");
10+
}

examples/basic/basic_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
3+
import basic
4+
5+
6+
class TestBasic(unittest.TestCase):
7+
def test_add(self):
8+
self.assertEqual(basic.add(1, 2), 3)
9+
self.assertEqual(basic.add(2, 2), 4)
10+
11+
12+
if __name__ == "__main__":
13+
unittest.main()

0 commit comments

Comments
 (0)