3

I want to compile a .rs file in a Rust lib to a .wasm.

RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown

Instead of a .wasm file I get a .Rlib and .d file. What do I need to change to get a .wasm?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
SurpriseMF
  • 174
  • 9

1 Answers1

4

rustc considers .wasm files to fill the “native dynamic library” role in the WASM target. Add this to your Cargo.toml configuration to request that build instead of the default .rlib:

[lib]
crate-type = ["cdylib"]
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • if you are just using rustc (for whatever reason), the `crate-type=cdylib` flag can be used – Rafaelplayerxd YT Feb 25 '22 at 10:39
  • May i ask, what does the .rlib file useful for? Does it have any application? And how does the compiler know, when cdylib crate type is used , the output should be a .wasm file and without it the default .rlib is built for wasm32 target? – raj Oct 08 '22 at 19:31
  • @raj `.rlib` is the default kind of output for any Rust _library._ Its purpose is to be the compiled static library which the Rust compiler will use as input when compiling crates that _depend on_ this crate. – Kevin Reid Oct 08 '22 at 22:15