title: Why must a WASM library in Rust set the crate-type to cdylib?
body: <p>The <a href="https://doc.rust-lang.org/reference/linkage.html" rel="noreferrer">Rust reference states</a>:</p>
  
  <blockquote>
    <p>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 <code>*.so</code> files on Linux, <code>*.dylib</code> files on macOS, and <code>*.dll</code> files on Windows.</p>
  </blockquote>
  
  <p>My WASM is not a <code>*.dylib</code>, <code>*.dll</code>, or <code>*.so</code>... so why must the crate type be set to cdylib? What is really happening under the hood?</p>
  
  <p>lib.rs</p>
  
  <pre class="lang-rust prettyprint-override"><code>#[no_mangle]
  pub extern fn add_one(x: u32) -&gt; u32 {
      x + 1
  }
  </code></pre>
  
  <p>Cargo.toml</p>
  
  <pre><code>[package]
  name = "utils"
  version = "0.1.0"
  authors = [""]
  edition = "2018"
  
  [dependencies]
  
  [lib]
  crate-type = ["cdylib"] // Why is this needed
  </code></pre>
  
  <p>index.html:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;script&gt; 
        fetch("utils.gc.wasm")
          .then(response =&gt; response.arrayBuffer())
          .then(result =&gt; WebAssembly.instantiate(result))
          .then(wasmModule =&gt; {
            const result = wasmModule.instance.exports.add_one(2);
            const text = document.createTextNode(result);
            document.body.appendChild(text);
          });
      &lt;/script&gt;
    &lt;head&gt;
    &lt;body&gt;&lt;/body&gt;
  &lt;html&gt;
  </code></pre>
  
  <p>Terminal:</p>
  
  <pre class="lang-none prettyprint-override"><code>$ cd utils
  $ cargo build --target wasm32-unknown-unknown --release
  $ wasm-gc target/wasm32-unknown-unknown/release/utils.wasm -o utils.gc.wasm
  $ http
  </code></pre>