Bindings

We currently provide the pybind11 library for producing Python bindings. While this is integrated into the virtual environment build system, there are some caveats. Namely, we build bindings with cpython 3.10, so if your system python cannot read those bindings, you will have to use our version of cpython to run your programs.

Simple Example

A very simple example of using pybind11:

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

PYBIND11_MODULE(my_module_name, m)
{
    m.doc() = "My module";

    py::class_<MyExampleClass>(m, "MyExampleClass")
        .def(py::init<>())
        .def_readwrite("field1", &MyExampleClass::field1);
}

And you can build with:

add_library(
    my_py_module MODULE
    my_py_module.cc)

target_link_libraries(
    my_py_module
    PRIVATE pybind11::pybind11)

set_target_properties(
    my_py_module
    PROPERTIES PREFIX "")

This will produce a my_py_module.so which can be imported into Python. Please see the pybind11 docs for further information on using pybind 11.

Virtual Environments

If you take the module constructed above, but want to use it in a virtual environment that is built with the Ark build system, you can add a reference to it. For example:

add_python_venv(
    example_venv
    BINDINGS
    my_py_module)

This will create a library in the site-packages that can be imported as my_py_module. For example:

import my_py_module

object = my_py_module.MyExampleClass()
print(object.field1)