Problems with emscripten (WebAssembly)

Problems with emscripten (WebAssembly)

本文关键字:WebAssembly emscripten with Problems      更新时间:2023-10-16

所以我想将一个简单的c ++文件编译成js并在Web浏览器中运行它。我采取的步骤(在窗户上(:

git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
emsdk install latest
emsdk activate latest
emsdk_env.bat --global
emsdk update-tags
emcc -v

打印的最后一个命令:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.39.15
clang version 11.0.0 "

然后我尝试在main.cpp文件中编译"hello world",如下所示:

#include <iostream>
#include <emscripten.h>
int main()
{
std::cout<< "hello world!" << std::endl;
return 0;
}

所以在测试文件夹中我使用了命令:

em++ main.cpp -o 1.html

其中打印:

cache:INFO: generating system library: libcompiler_rt.a... (this will be cached in "C:Usersx.emscripten_cachewasmlibcompiler_rt.a" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libc-wasm.a... (this will be cached in "C:Usersx.emscripten_cachewasmlibc-wasm.a" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libc++-noexcept.a... (this will be cached in "C:Usersx.emscripten_cachewasmlibc++-noexcept.a" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libc++abi-noexcept.a... (this will be cached in "C:Usersx.emscripten_cachewasmlibc++abi-noexcept.a" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libdlmalloc.a... (this will be cached in "C:Usersx.emscripten_cachewasmlibdlmalloc.a" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libpthread_stub.a... (this will be cached in "C:Usersx.emscripten_cachewasmlibpthread_stub.a" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libc_rt_wasm.a... (this will be cached in "C:Usersx.emscripten_cachewasmlibc_rt_wasm.a" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libsockets.a... (this will be cached in "C:Usersx.emscripten_cachewasmlibsockets.a" for subsequent builds)
cache:INFO:  - ok

作为输出,我得到了以下文件:1.html1.js1.wasm

当我打开1.html时,我看到这些错误:

both async and sync fetching of the wasm failed
RuntimeError: abort(both async and sync fetching of the wasm failed) at jsStackTrace@file:///E:/emscripten/emsdk/test/1.js:1952:17
stackTrace@file:///E:/emscripten/emsdk/test/1.js:1969:16
abort@file:///E:/emscripten/emsdk/test/1.js:1728:44
getBinary@file:///E:/emscripten/emsdk/test/1.js:1800:10
getBinaryPromise/<@file:///E:/emscripten/emsdk/test/1.js:1822:13
getBinaryPromise@file:///E:/emscripten/emsdk/test/1.js:1821:10
instantiateArrayBuffer@file:///E:/emscripten/emsdk/test/1.js:1865:12
instantiateAsync@file:///E:/emscripten/emsdk/test/1.js:1892:14
createWasm@file:///E:/emscripten/emsdk/test/1.js:1908:3
@file:///E:/emscripten/emsdk/test/1.js:4990:11

我也在 Linux 上尝试过,但出现了完全相同的错误。旧版本的 emscripten SDK 也不起作用。

有什么提示吗?

使用file://的问题,我认为你需要运行服务器并使用http://。 请参阅:https://emscripten.org/docs/getting_started/FAQ.html#faq-local-webserver。 还有:https://emscripten.org/docs/compiling/Running-html-files-with-emrun.html

如果 Web 服务器的目录中只有 html 文件夹,也会发生此错误。 关联的 .wasm 文件必须位于 Web 服务器的文件夹中。

因此,对于hello_world示例,请复制 hello.html 文件和 hello.wasm 文件。

如果在 Chrome 浏览器中进行测试,您可以打开"开发人员工具"面板,然后选择控制台标签。 刷新页面并观察任何错误消息。