A rust project with C++ code compatible with wasm. Uses libsodium.
- Download and install
wasi-sdk
from the official GitHub repository. Choose the appropriate version for your system. - After downloading, extract the
wasi-sdk
package and set up the environment variables:
export WASI_SDK_PATH=~/Downloads/wasi-sdk-22.0
export PATH=$WASI_SDK_PATH/bin:$PATH
- You may also need to install
rustup
- Run
sh prepare-sodium.sh
to preparelibsodium
for wasm env. You may need to installzig
in advance. - Run
sh prepare-openssl.sh
to preparelibssl
for wasm env. Also, in some of your C++ file you need to definepid_t getpid(void) {return 1;}
since WASI lacks process identifiers, so we nede to define some stub. - Run
sh prepare-boost.sh
to prepareboost
for wasm env.
cargo build --target=wasm32-wasi --release
wasmedge --reactor target/wasm32-wasi/release/my_rust_app.wasm test
This should print the following:
[test] Hello from Rust
[hello] Hello, World from C++! (std::cout)
[hello] Hello World from C++! (printf)
The sodium is initialized!
sodium randombytes: 714298213