Using C++ to Create an EdjFunction

We recommend using Linux or macOS for building C++ serverless functions. For Windows users, install the Windows Subsystem for Linux (WSL) and follow the instructions for Linux OS for building the functions.

Prerequisites

  1. Ensure that the following tools are installed on your system:

    • make (GNU Make)

    • git

    • wget on Linux or curl on macOS

    • tar

  2. Create an edjx directory in the user’s home directory:

    mkdir -p ~/edjx
  3. Install the EDJX C++ SDK using one of the following methods:

    • Option 1: Download and extract the EDJX C++ SDK from the edjx-cpp-sdk releases GitHub page.

      • Linux

      • macOS

      EDJX_SDK_VERSION=v22.12.1
      WASI_SDK_VERSION=12
      
      INSTALLATION_DIR="${HOME}/edjx"
      
      cd "$INSTALLATION_DIR"
      
      wget "https://github.com/edjx/edjx-cpp-sdk/releases/download/${EDJX_SDK_VERSION}/edjx-cpp-sdk-${EDJX_SDK_VERSION}-wasi-${WASI_SDK_VERSION}.tar.gz"
      tar -xvf "edjx-cpp-sdk-${EDJX_SDK_VERSION}-wasi-${WASI_SDK_VERSION}.tar.gz"
      EDJX_SDK_VERSION=v22.12.1
      WASI_SDK_VERSION=12
      
      INSTALLATION_DIR="${HOME}/edjx"
      
      cd "$INSTALLATION_DIR"
      
      curl -L -O "https://github.com/edjx/edjx-cpp-sdk/releases/download/${EDJX_SDK_VERSION}/edjx-cpp-sdk-${EDJX_SDK_VERSION}-wasi-${WASI_SDK_VERSION}.tar.gz"
      tar -xvf "edjx-cpp-sdk-${EDJX_SDK_VERSION}-wasi-${WASI_SDK_VERSION}.tar.gz"

      OR
      Option 2: Get the latest EDJX C++ SDK by cloning the edjx-cpp-sdk repository.

      INSTALLATION_DIR="${HOME}/edjx"
      
      git clone --depth 1 "https://github.com/edjx/edjx-cpp-sdk.git" "${INSTALLATION_DIR}/edjx-cpp-sdk"
      If using the git clone option, see the WASI_SDK_VERSION file for the WASI SDK version against which the EDJX C++ SDK in the repository was compiled.
      The path to the EDJX C++ SDK is expected to be ~/edjx/edjx-cpp-sdk-v22.12.1-wasi-12/include and ~/edjx/edjx-cpp-sdk-v22.12.1-wasi-12/lib.
  4. Install the WASI SDK by downloading its binary release from the wasi-sdk GitHub page. This SDK provides the necessary C and C++ standard library functions required to build C++ serverless functions.

    • Linux

    • macOS

    WASI_SDK_VERSION=12
    WASI_SDK_VERSION_FULL="${WASI_SDK_VERSION}.0"
    WASI_SDK_OS=linux
    
    INSTALLATION_DIR="${HOME}/edjx"
    
    cd "$INSTALLATION_DIR"
    
    wget "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION}/wasi-sdk-${WASI_SDK_VERSION_FULL}-${WASI_SDK_OS}.tar.gz"
    tar -xf "wasi-sdk-${WASI_SDK_VERSION_FULL}-${WASI_SDK_OS}.tar.gz"
    WASI_SDK_VERSION=12
    WASI_SDK_VERSION_FULL="${WASI_SDK_VERSION}.0"
    WASI_SDK_OS=macos
    
    INSTALLATION_DIR="${HOME}/edjx"
    
    cd "$INSTALLATION_DIR"
    
    curl -L -O "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION}/wasi-sdk-${WASI_SDK_VERSION_FULL}-${WASI_SDK_OS}.tar.gz"
    tar -xf "wasi-sdk-${WASI_SDK_VERSION_FULL}-${WASI_SDK_OS}.tar.gz"
    The EDJX C++ SDK is compiled against a specific WASI SDK version. For example, edjx-cpp-sdk-vX.Y.Z-wasi-12.tar.gz must be used with WASI SDK version 12.
    Binary releases of the WASI SDK do not have native support for Apple Silicon Macs. Users that use an Apple Silicon Mac computer need to install Rosetta on their system or build the WASI SDK manually. See the WASI SDK repository for build instructions.
    The path to the compiler bundled in the WASI SDK is expected to be ~/edjx/wasi-sdk-12.0/bin/clang++.
  5. Clone the EDJX C++ Example code GitHub repository to your environment.
    This repository contains samples to help you build C++ serverless functions. The functions can then be built and deployed to the EDJX peer-to-peer network using the EDJX Console, CLI, or VS Code Extension.

    See the README.md files in the EDJX GitHub repository for advanced installation options.

Create a Function

Build a Function

By default, the files are created with the following file structure and the resulting WASM file is saved in <application>/bin/<app>.wasm, as shown below.

Project Directory Name

|-- bin
    |-- edjfunction_example_cpp.wasm
|-- build
    |-- lib.d
    |-- lib.o
    |-- serverless_function.d
    |-- serverless_function.o
|-- src
    |-- lib.cpp
    |-- serverless_function.cpp
|-- LICENSE
|-- Makefile
|-- README.md

Where each function directory consists of the following subdirectories:

  • bin — Saves the compiled WASM executable of the example application, which is generated when compiling the function

  • build — Contains the intermediate object files of the example application, which are generated when compiling the function

  • src — Contains the source code of the example application

Deploy a Function