Create a .wasm File Written in C++
To create .wasm files written in C++ you must have the EDJX C++ SDK and the corresponding WASI SDK installed on your environment.
To install the required SDKs:
-
Make the
/opt/edjx-cpp-sdk
directory. -
Download the EDJX C++ SDK using one of the following methods:
-
Clone the latest EDJX C++ SDK.
git clone --depth 1 https://github.com/edjx/edjx-cpp-sdk.git /opt/edjx-cpp-sdk
OR
-
Make a
/opt/edjx-cpp-sdk-$<EDJX_SDK_VERSION>-wasi-sdk-$<WASI_SDK_VERSION>
directory.Where,
$<EDJX_SDK_VERSION>
is the EDJX version and$<WASI_SDK_VERSION>
is the WASI version matching the EDJX release.
For example,v21.11.1-wasi-12
andwasi-sdk-12.0
, respectively.Download and upzip the EDJX C++ SDK from the edjx-cpp-sdk build GitHub page.
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 -C /opt ln -s /opt/edjx-cpp-sdk-${EDJX_SDK_VERSION}-wasi-${WASI_SDK_VERSION} /opt/edjx-cpp-sdk
-
-
Make the
/opt/wasi-sdk
directory. -
Install the WASI SDK to
/opt/wasi-sdk
(so that the path to the bundled compiler is /opt/wasi-sdk/bin/clang++).- NOTE
-
At this point you should have two directories under the
/opt
directory,
edjx-cpp-sdk
oredjx-cpp-sdk-v21.11.1-wasi-12-wasi-sdk-12.0
andwasi-sdk
.
-
Download the WASI C++ SDK from the WebAssembly/wasi-sdk/releases GitHub repository.
For example,wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz
This repository provides the necessary C and C++ standard library functions required to build C++ serverless functions.
-
Unzip the matching release number (
wasi-sdk-12.0
) file to the/opt/wasi-sdk
directory.
Make sure the WASI SDK has the same version identifier as the EDJX SDK.
For example,wasi-sdk-12.0
andedjx-cpp-sdk-version#-wasi-12.0
.- NOTE
-
At this point you should have two directories and one tar file under the
/opt
directory. Delete the tar file, as desired. - IMPORTANT
-
Make sure that the bundled compiler is in the
/opt/wasi-sdk/bin/clang++
directory.
-
Clone the EDJX C++ Example code repo 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 from advanced installation options.
To create, build, and deploy EDJX functions written in C++:
-
Create the function:
-
Console - Create Function
-
CLI - function init command
-
VS Code - Add a Function
-
-
Build the function:
-
Console - Create Function
-
CLI - function build command
-
VS Code - Build a Function
See also Add a Function and Deploy 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 |-- edjconfig.yaml |-- 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 is generated when compiling the function -
src/
— Contains the source code of the example application
-
-
Deploy the function:
-
Console - Create Function
-
CLI - function deploy command
-
VS Code - Deploy a Function
-