Tutorial: Calculator end-to-end
This tutorial uses the included samples/calculator crate and shows how to
generate artifacts and run platform examples.
1) Generate artifacts
weaveffi generate samples/calculator/calculator.yml -o generated
This writes headers and templates under generated/:
generated/c— C header and convenience C filegenerated/swift— SwiftPM System Library (CWeaveFFI) and Swift wrapper (WeaveFFI)generated/android— Kotlin wrapper + JNI shims + Gradle skeletongenerated/node— N-API addon loader +.d.tsgenerated/wasm— minimal loader stub
2) Build the Rust sample
cargo build -p calculator
This produces a shared library:
- macOS:
target/debug/libcalculator.dylib - Linux:
target/debug/libcalculator.so
3) Run the C example
macOS
cd examples/c
cc -I ../../generated/c main.c -L ../../target/debug -lcalculator -o c_example
DYLD_LIBRARY_PATH=../../target/debug ./c_example
Linux
cd examples/c
cc -I ../../generated/c main.c -L ../../target/debug -lcalculator -o c_example
LD_LIBRARY_PATH=../../target/debug ./c_example
4) Run the Node example
macOS
cp target/debug/libindex.dylib generated/node/index.node
cd examples/node
DYLD_LIBRARY_PATH=../../target/debug npm start
Linux
cp target/debug/libindex.so generated/node/index.node
cd examples/node
LD_LIBRARY_PATH=../../target/debug npm start
5) Try Swift (macOS)
cargo build -p calculator
cd examples/swift
swiftc \
-I ../../generated/swift/Sources/CWeaveFFI \
-L ../../target/debug -lcalculator \
-Xlinker -rpath -Xlinker ../../target/debug \
Sources/App/main.swift -o .build/debug/App
DYLD_LIBRARY_PATH=../../target/debug .build/debug/App
On Linux, replace DYLD_LIBRARY_PATH with LD_LIBRARY_PATH.
6) Android and WASM
- Open
generated/androidin Android Studio and build the:weaveffiAAR. - Build for WASM:
cargo build --target wasm32-unknown-unknown --releaseand load withgenerated/wasm/weaveffi_wasm.js.