Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

First steps

Generate bindings for functions

Using litgen is straightforward. The simplest use case is:

  1. import litgen

  2. instantiate some options

  3. convert some code into C++ binding code and python declarations (stubs)

  4. use the generated code

Below we import litgen, define the C++ code we want to bind, and run litgen.generate_code

The generated code contains several elements:

  • stub_code contains the python declarations, including all comments. They enable to have a flawless code navigation and completion inside IDEs.

  • pydef_code contains the C++ binding code. This code is specific to pybind11

  • glue_code: optional additional C++ code that is emitted in speficic advanced cases (more details later in this manual). Most of the time, it will be empty

Let’s see the python declarations corresponding to the C++ code: they should be copied into a python stub file (*.pyi) that describes the bindings offered by your library.

Note: below, we import litgen_demo to be able to display code in this page. You will not need it: you can simply call print(generated_code.stub_code)

Loading...

Binding a simple class or struct

For a simple class, public members and methods will be exposed. For a simple struct, a constructor with named parameters will be exposed in Python.

Loading...

Generator options

There are numerous generations options that can be set, and they are explained in details later in this manual: See full list

About glue code

In some cases, some additional “glue code” will be emitted. This is the case for example when we want to bind a function that wants to modify a parameter whose type is immutable in python.

In this case, we will need to set one option (options.fn_params_replace_modifiable_immutable_by_boxed__regex )

Loading...