我想开始使用stdweb库,但是由于某些原因,我的代码无法编译。这是代码:
#[macro_use]
extern crate stdweb;
fn main() {
stdweb::initialize();
let message = "Hello, 世界!";
js! {
alert( @{message} );
}
stdweb::event_loop();
}
这是我运行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`.
一些我认为可能有用的附加信息:
货物版本:cargo 1.41.0-nightly (626f0f40e 2019-12-03)
〜/ .cargo / config的内部:
[target.arm-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc-7"
任何帮助将不胜感激:)
编辑:我正在使用“ elementary os”(基本上是带有较酷ui的ubuntu)。尝试在稳定版本上构建会给我完全相同的错误。
WebAssembly是快速,安全,便携式,并且是开放Web平台的一部分。因此,WebAssembly是与平台无关。注释掉(删除)~/.cargo/config
文件中的这些链接器行:
[target.arm-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc-7"
从头开始创建Web hello世界示例:
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
文件:
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();
}
构建并运行Web服务器以服务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
打开您的Web浏览器http://localhost:8000:
firefox http://localhost:8000
[在线试用here
我的开源WebAssembly项目:https://wasmup.github.io/wasmup/