rust、WebAssembly 和传递参数以增加总内存

标签 rust asm.js webassembly

我有一个 rust 项目,我正在根据 http://asquera.de/blog/2017-04-10/the-path-to-rust-on-the-web/ 编译到 webasm

项目编译。当我在 Chrome Canary 中运行它时,它耗尽了内存并给我一条非常有用的错误消息:

abort("Cannot enlarge memory arrays. Either (1) compile with  -s 
TOTAL_MEMORY=X  with X higher than the current value 16777216, (2) compile 
with  -s ALLOW_MEMORY_GROWTH=1  which allows increasing the size at runtime, 
...

问题是,不清楚如何将这些标志传递给 rustc/构建工具链。

既不设置 EMMAKEN_CFLAGS 也不进行以下工作:

cargo  rustc -v --target=wasm32-unknown-emscripten --release -- -Clink-args="-s TOTAL_MEMORY=33554432" 

最佳答案

This博客文章提供了一个我认为也可以应用于您的案例的解决方案:

As best as I can tell there is no way to pass most linker arguments through cargo. Instead, hack around the limitation by specifying a custom linker that is actually a shell script wrapping the real linker.

创建一个类似于 emcc_link 的 shell 脚本,它使用适当的选项调用 emscripten:

emcc "-s" "TOTAL_MEMORY=33554432" $@

(您可能需要其他选项才能使其正常工作。查看 blog post 了解详细信息。)

并通过编辑/创建.cargo/config 指定将其用于您的项目:

[target.wasm32-unknown-emscripten]
linker = "/your/project/dir/emcc_sdl"

[target.asmjs-unknown-emscripten]
linker = "/your/project/dir/emcc_sdl"

我无情的假设编译环境是linux之类的。在 Windows 上,shell 脚本可能应该是批处理脚本,我不确定 .cargo/config 是否有任何差异。

免责声明:我没有尝试过这些。

关于rust、WebAssembly 和传递参数以增加总内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45807869/

相关文章:

rust - 核心中的运算符真的是循环定义的吗?

javascript - 在 OdinMonkey 中优化 asm.js

javascript - Web Worker 使用 Web Assembly 时出错

rust - 为什么对原语的引用没有隐式取消引用

rust - Rust 中 f32 的 frexp 函数在哪里?

pointers - 如何强制 Rust 获得非安全方法分配的内存的所有权?

javascript - 在 asm.js 中转换 int[ish] 和 double[ish]

javascript - 使用 Emscripten 编译 GMP/MPFR

visual-studio - Visual Studio 2019 在哪里可以找到 .net core 3.1 的 WebAssembly 模板

javascript - Chrome 扩展程序中的 Wasm 模块编译错误