我想开始使用标准网页库,但由于某种原因,我的代码无法编译。代码如下:
#[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/config 的cargo 1.41.0-nightly (626f0f40e 2019-12-03)
内部:
[target.arm-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc-7"
任何帮助将不胜感激:)
编辑:我正在使用"elementary os"(基本上是带有更酷 ui 的 ubuntu(。尝试在稳定的基础上构建会给我完全相同的错误。
编辑 2:rustup show
返回
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)
编辑3:cargo-web --version
返回cargo-web 0.6.26
链接器或编译器设置未正确设置,或以某种方式更改。
错误:未知参数:-保险丝-ld=黄金
从RUSTFLAGS
中删除黄金链接器标志:-fuse-ld=gold
,LD
环境变量或编译器选项。
使用详细标志(-v
或--verbose
(查看详细输出:
cargo web build -v --target=wasm32-unknown-unknown
WebAssembly是快速,安全,可移植的,并且是开放Web平台的一部分。
所以WebAssembly是独立于平台的(你不需要编译它来ARM
,它是基于虚拟机的,所以你需要一个主机来运行WebAssembly,例如在Web浏览器JavaScript VM中运行WebAssembly。还有WASI:WebAssembly系统接口(。
由于它是可移植的:
您可以在任何计算机上使用工作编译器构建它,然后只需将.wasm
文件复制到目标Web服务器计算机即可。
从头开始 Web hello world 示例:
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
添加到 Cargo.toml 文件结束后[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
打开您的网络浏览器 http://localhost:8000:
firefox http://localhost:8000
做。
您可以发布构建版本:
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/
现在,您可以使用自己喜欢的静态Web服务器提供stdweb-rs-js.js
和stdweb-rs-js.wasm
文件,例如 使用简单./static/index.html
文件python3 -m http.server 8080
:
<!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>
就这样。
我希望这有所帮助。