Emscripten 生成的 HTML 文件在打开时不起作用,必须预加载 wasm 二进制文件

Emscripten generated HTML file does not work when opened, wasm binary must be preloaded

我能够正确编译我的 helloWorld.c 文件:

#include <stdio.h>

int main(int argc, char ** argv) {
    printf("Hello World\n");
}

使用命令:emcc helloWorld.c -s WASM=1 -o helloWorld.html

但是当我打开文件 helloWorld.html 时,页面上出现以下错误:

on the web, we need the wasm binary to be preloaded and set on Module['wasmBinary']. emcc.py will do that for you when generating HTML (but not JS) abort("on the web, we need the wasm binary to be preloaded and set on Module['wasmBinary']. emcc.py will do that for you when generating HTML (but not JS)") at Error at jsStackTrace (file:///C:/workspaces/wasm/HelloWorld/helloWorld.js:1001:13) at stackTrace (file:///C:/workspaces/wasm/HelloWorld/helloWorld.js:1018:12) at abort (file:///C:/workspaces/wasm/HelloWorld/helloWorld.js:2414:44) at getBinary (file:///C:/workspaces/wasm/HelloWorld/helloWorld.js:1528:7) at file:///C:/workspaces/wasm/HelloWorld/helloWorld.js:1542:16

如何让我的应用程序正确加载?这里有什么问题?

生成的 hmtl 文件旨在从网络服务器提供服务。如果您检查浏览器控制台,您可能会看到类似这样的错误:

Fetch API cannot load file:///{Your wasm file path}. URL scheme must be "http" or "https" for CORS request.

要运行您的应用程序使用本地 Web 服务器,请使用 Emscripten 命令 emrun:

emrun helloWorld.html

您也可能在生产环境中看到此错误,在这种情况下,很可能是 .wasm 文件的请求返回了非 200 状态代码。