How can you link a Rust library with a C app into one WASM file? #17294
-
I have a project that links a C library with a C app using Emscripten. I would like to replace the C library with a library written in Rust. How can this be accomplished? I know that there are two relevant WASM targets in Rust, the normal wasm32-unknown-unknown one and the wasm32-unknown-emscripten one. I'm guessing the latter target is necessary for it to work with the Emscripten compiler? But how do I tell Emscripten to use a .wasm library rather than a .a library? There may be other ways of linking Rust and C code together into a .wasm but I think I do need to stick with Emscripten as I require its Asyncify feature (and in the future will use its WASMFS system too.) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Given you need only pure computation from the Rust side, IIUC (no I'm not sure how to get Rust to emit a A risk, however, is if Rust uses libc code, and the Rust compiler's idea of what that ABI is differs from emcc. I think Rust has that hardcoded in some form, and there is no simple way to make it use current emcc's version. In Zig for example you can tell it to use the emscripten sysroot, but I don't know if there's a simple way to do that in Rust. cc @tlively who knows this the best |
Beta Was this translation helpful? Give feedback.
To produce a Rust .a file, set the
crate-type
field tostaticlib
in your cargo.toml: https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field