2

I wanted to get started with the stdweb library but for some reason my code doesn't compile. Here's the code:

#[macro_use]
extern crate stdweb;

fn main() {
    stdweb::initialize();

    let message = "Hello, 世界!";
    js! {
        alert( @{message} );
    }

    stdweb::event_loop();
}

And here's the error I get when i run cargo web start --target=wasm32-unknown-unknown:

error: linking with `rust-lld` failed: exit code: 1
  |
  = note: "rust-lld" "rust-lld" "-flavor" "wasm" "--no-threads" "-z" "stack-size=1048576" "--stack-first" "--allow-undefined" "--fatal-warnings" "--no-demangle" "--export-dynamic" "--no-entry" "-L" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.16ntzdg2n8bx81q6.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.2xfx8uklq7n12q67.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.48klqndjiid3k9rj.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.48le17nhrmqft97k.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.4b580liojrglk551.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.4rrrlrglrmdphj5c.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.4vuc8rinrzvzrs2v.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.50srrlytlsid8m35.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.5fgjih7crwhwn4xr.rcgu.o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.5fkqw2idrlv7jos5.rcgu.o" "-o" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.wasm" "--export" "main" "--export" "__web_malloc" "--export" "__web_free" "--export=__heap_base" "--export=__data_end" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/rust_snake.2l9dobmrlqco0sju.rcgu.o" "--gc-sections" "-O0" "-L" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps" "-L" "/home/kuba/Projects/rust-snake/target/debug/deps" "-L" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/libstdweb-ef476852d27a12c5.rlib" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/libdiscard-a83cebb0a142eb8f.rlib" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/libstdweb_internal_runtime-0bce6e61d1817cf7.rlib" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/libserde_json-1c98a67e94d08af3.rlib" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/libryu-322b81c9b49f7e9f.rlib" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/libitoa-a7c2d7644820b152.rlib" "/home/kuba/Projects/rust-snake/target/wasm32-unknown-unknown/debug/deps/libserde-c2f4e30465155451.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libstd-9c867bc4c16ee4c2.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libpanic_abort-ed0cd729c2f3d142.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libdlmalloc-030f419e714a0cc6.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libhashbrown-a12a1f881dba75e1.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/librustc_std_workspace_alloc-35aa06e58878157c.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libbacktrace-6636ad5f40fe0c6e.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/librustc_demangle-3c1019358081106e.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libunwind-b93526bae6789131.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libcfg_if-12a659b7ffc8c645.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/liblibc-62025a1d48e0630e.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/liballoc-80234d57a1442351.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/librustc_std_workspace_core-d429e78abfaecaef.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libcore-c485e95495d6739c.rlib" "/home/kuba/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libcompiler_builtins-53d1b7f6a2bd6c6f.rlib" "-fuse-ld=gold"
  = note: rust-lld: error: unknown argument: -fuse-ld=gold


error: aborting due to previous error
error: could not compile `rust-snake`.

Some additional info I think might be useful:

cargo version: cargo 1.41.0-nightly (626f0f40e 2019-12-03)
insides of ~/.cargo/config:

[target.arm-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc-7"

Any help would be appreciated :)

Edit: I'm using "elementary os" (basicly ubuntu with cooler ui). And trying to build on stable gives me exactly the same error.

Edit2: rustup show returns

rustup home:  /home/kuba/.rustup

installed toolchains
--------------------

stable-x86_64-unknown-linux-gnu (default)
nightly-x86_64-unknown-linux-gnu

installed targets for active toolchain
--------------------------------------

arm-unknown-linux-gnueabihf
armv7-unknown-linux-gnueabihf
wasm32-unknown-unknown
x86_64-unknown-linux-gnu

active toolchain
----------------

stable-x86_64-unknown-linux-gnu (default)
rustc 1.40.0 (73528e339 2019-12-16)

Edit3: cargo-web --version returns cargo-web 0.6.26

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
smolus
  • 115
  • 12
  • On what platform are you building the project? Are you tied to that nightly version of the compiler? – E_net4 Dec 27 '19 at 13:50
  • @E_net4thesafedownvoter I build on linux. To be precise its "elementary os" which is basicly just ubuntu with cooler ui but systemwise everything works like on ubuntu. Could you explain what do you mean by "tied to"? Cheers – smolus Dec 27 '19 at 16:05
  • As in, do you really need to use that version of the Rust compiler? It could be that you just stumbled upon a particular nightly toolchain which doesn't work well with stdweb. Also, don't forget to edit your question with those details. – E_net4 Dec 27 '19 at 16:09

1 Answers1

1

Your linker or compiler settings are not set correctly, or changed somehow.

error: unknown argument: -fuse-ld=gold

Remove the gold linker flag: -fuse-ld=gold from your RUSTFLAGS, LD environmental variables or the compiler options.

Use verbose flag (-v or --verbose) to see verbose output:

cargo web build -v --target=wasm32-unknown-unknown

WebAssembly is fast, safe, portable, and part of the open Web platform.
So the WebAssembly is platform independent (You don't need to compile it to ARM, and it is virtual machine based so you need a host to run WebAssembly, e.g. inside a web browser JavaScript VM for WebAssembly. Also there is WASI: WebAssembly System Interface).

Since it is portable:
You may build it using your working compiler on any machine and then just copy .wasm file(s) to the target web server machine.


To start a web hello world sample from scratch:

rustup target add wasm32-unknown-unknown

# https://github.com/koute/stdweb
cargo search cargo-web
# cargo-web = "0.6.26"          # A Cargo subcommand for the client-side Web
# cargo install cargo-web --version 0.6.26
cargo install cargo-web
file ~/.cargo/bin/cargo-web
cargo-web --version
# cargo-web 0.6.26

# create new project e.g. "123stdweb-rs-js"
cargo new 123stdweb-rs-js
cd 123stdweb-rs-js

cargo search stdweb

Add to Cargo.toml file end after [dependencies]:

stdweb = "0.4.20"  # A standard library for the client-side Web

main.rs file:

use stdweb;
use stdweb::js;

fn main() {
    stdweb::initialize();
    let message = "Hello from stdweb.";
    let result = js! {
        alert( @{message} );
        return 2 + 2 * 2;
    };
    println!("2 + 2 * 2 = {:?}", result);
    stdweb::event_loop();
}

Build and run web server to serve target/wasm32-unknown-unknown/debug/stdweb-rs-js.js:

# Compile to WebAssembly using Rust's native WebAssembly backend:
cargo web start --target=wasm32-unknown-unknown

Open your web browser http://localhost:8000:

firefox http://localhost:8000

Done.


You may release build:

cargo web build --release --target=wasm32-unknown-unknown
mkdir static
cp target/wasm32-unknown-unknown/release/stdweb-rs-js.js ./static/
cp target/wasm32-unknown-unknown/release/stdweb-rs-js.wasm ./static/

Now you may serve stdweb-rs-js.js and stdweb-rs-js.wasm files using your favorite static web server e.g. python3 -m http.server 8080 using simple ./static/index.html file:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    Right click <b>Inspect</b> then click <b>Console</b> tab
    <hr />

    <script src="stdweb-rs-js.js"></script>
  </body>
</html>

That's all.


I hope this helps.

wasmup
  • 14,541
  • 6
  • 42
  • 58
  • Thanks for the reply! Sadly, after following all steps I still get the same error :( – smolus Jan 01 '20 at 10:06
  • And I don't think its my fault since I got it to work with no problems on my windows machine – smolus Jan 01 '20 at 10:08
  • Yes it does. I think it's just cargo web that's failing. – smolus Jan 01 '20 at 10:11
  • Building the repository you linked fails with the same error as the original question – smolus Jan 01 '20 at 10:38
  • 1
    Hey i tried few things and finally thanks to you I found out that i can make it work by using "RUSTFLAGS='-C linker=rust-lld' cargo web start --target=wasm32-unknown-unknown". Is there any way I could make it do that by default? – smolus Jan 02 '20 at 23:45
  • Also I dont have a "LD" enviroment variable and I dont know how to change compiler options but if you could explain maybe that's a more elegant solution – smolus Jan 02 '20 at 23:48
  • You should find -fuse-ld=gold and remove it. Check this commands first [echo $RUSTFLAGS] [env] [echo $LDFLAGS]. search for -fuse-ld or LDFLAGS on your environment variables [search for LDFLAGS here too](https://lld.llvm.org/), [Configuration](https://doc.rust-lang.org/cargo/reference/config.html), [Environment](https://doc.rust-lang.org/cargo/reference/environment-variables.html), [RUSTFLAGS](https://github.com/rust-lang/cargo/issues/4423), [linker](https://github.com/rust-lang/rust/issues/52362), [custom](https://stackoverflow.com/questions/55628741/specifying-custom-linker-executable-to-g). – wasmup Jan 03 '20 at 09:39
  • 1
    Ok I found it! For some reason my RUSTFLAGS enviroment variable was set to "-C link-args='-fuse-ld=gold'" by default. I have no idea why did it do that but after clearing RUSTFLAGS in /etc/enviroment everything works perfectly fine. Thanks for help :) – smolus Jan 03 '20 at 18:56