【问题标题】:Web Assembly - TypeError: NetworkError when attempting to fetch resourceWeb 程序集 - 类型错误:尝试获取资源时出现网络错误
【发布时间】:2020-04-20 01:30:28
【问题描述】:

我正在尝试将一个简单的 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

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

【问题讨论】:

    标签: c webassembly


    【解决方案1】:

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

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

    emrun --no_browser --port 8080 .
    

    (并在浏览器中打开该网站,即localhost:8080

    (2) 如果没有,请尝试使用此git repo for emsdk

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

    然后按照方法(1)尝试emrun

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

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

    最后,要真正运行程序,我们不能简单地打开 HTML 在 Web 浏览器中打开文件,因为不支持跨域请求 对于文件协议方案。我们必须实际服务于输出 通过 HTTP 的文件。

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

    【讨论】:

    • emrun 和最小的 python 服务器解决方案都可以工作。似乎提供 WASM 的唯一方法是通过 Web 服务器。
    • 正确,开发者文档说: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.
    猜你喜欢
    • 2019-09-04
    • 2017-07-31
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2021-05-23
    • 2020-03-29
    相关资源
    最近更新 更多