Skip to content
Anders Langlands edited this page Mar 13, 2021 · 8 revisions

Binding tutorials

Welcome to the cppmm wiki! This will ultimately host more filled out documentation, but for now we'll just have a simple tutorial.

This tutorial will walk you through creating binding files using a very simple, header-only target library which we'll extend to add new features to as we need to demonstrate how to bind them.

This assumes you have successfully built cppmm and are currently in a build directory underneath the main repository directory. If you're working from somewhere else, you'll need to edit the example commands appropriately.

00 - cppmm architecture

Before we get started, let's take a moment to examine the cppmm architecture. Binding a C++ library is a multi-step process. First we create a binding file, which is a C++ file that describes the classes, methods and functions we want to bind, and how we want to bind them. This is read by our first binary, astgen, which spits out the AST for the binding interface as JSON. Then asttoc reads the AST and generates a C library that wraps the C++ API.

-- TODO: DIAGRAM HERE

01 - Binding a simple class

Our target "library"

What's the simplest possible C++ class we can bind to get started? Perhaps something like this:

// file: tutorial/01_simple_class/include/hello.hpp
#pragma once

#include <iostream>

namespace hello {

class Hello {
public:
    void hello() const { std::cout << "Hello, world!\n"; }
};

} // namespace hello

In order to start binding this to C, we first need to create AST, and to do that we need to create a binding file to feed to astgen

Clone this wiki locally