Web Assembly - TypeError: NetworkError when attempting to fetch resource

Web Assembly - TypeError: NetworkError when attempting to fetch resource

我正在尝试将一个简单的 hello world 程序从 C 语言编译成 Web 程序集。我一直在遵循 MDN (https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm) 提供的指南。通过 Firefox 71 导航到 HTML 页面后,我收到 TypeError: “NetworkError when attempting to fetch resource.” 错误。

这是 C 代码:

#include <stdio.h>

int
main(int argc, char *argv[]) {
    printf("hello, world!");
    return 0;
}

以及用于编译的命令:

emcc hello.c -s WASM=1 -o hello.html

我错过了什么吗?还有其他人遇到过这个问题吗?提前谢谢你。

我遇到了同样的问题,我尝试了一些解决方案,这些似乎有效:

(1) 查看 emrun 网络服务器是否可以通过 运行 文件:

emrun --no_browser --port 8080 .

(然后在浏览器中打开网站,即 localhost:8080

(2) 如果没有,则尝试将此 git 存储库用于 emsdk :

git clone https://github.com/juj/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh

然后尝试emrun方法(1)。

CORS 已启用,这会阻止下载 .wasm 文件。所以 运行 使用小型服务器访问 html 文件是可行的(这包括通过 python3 -m http.server 的简单 python 服务器!)

开发者指南中也提到了这一点:

Finally, to actually run the program, we cannot simply open the HTML file in a web browser because cross-origin requests are not supported for the file protocol scheme. We have to actually serve the output files over HTTP.

您可以从这里参考所有这些信息:https://webassembly.org/getting-started/developers-guide/