A dynamic system library will be produced. This is used when compiling a dynamic library to be loaded from another language. This output type will create
*.so
files on Linux,*.dylib
files on macOS, and*.dll
files on Windows.
My WASM is not a *.dylib
, *.dll
, or *.so
... so why must the crate type be set to cdylib? What is really happening under the hood?
lib.rs
#[no_mangle]
pub extern fn add_one(x: u32) -> u32 {
x + 1
}
Cargo.toml
[package]
name = "utils"
version = "0.1.0"
authors = [""]
edition = "2018"
[dependencies]
[lib]
crate-type = ["cdylib"] // Why is this needed
index.html:
<!DOCTYPE html>
<html>
<head>
<script>
fetch("utils.gc.wasm")
.then(response => response.arrayBuffer())
.then(result => WebAssembly.instantiate(result))
.then(wasmModule => {
const result = wasmModule.instance.exports.add_one(2);
const text = document.createTextNode(result);
document.body.appendChild(text);
});
</script>
<head>
<body></body>
<html>
Terminal:
$ cd utils
$ cargo build --target wasm32-unknown-unknown --release
$ wasm-gc target/wasm32-unknown-unknown/release/utils.wasm -o utils.gc.wasm
$ http