当使用 (C) 编译 C/C++ .wasm 代码时clang
- 它在 Chrome 中加载并且运行良好,但是使用clang++
(C++) 时 - wasm 加载失败并出现错误(在 JS 控制台中):
Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #1 module="wasi_snapshot_preview1" function="fd_close" error: function import requires a callable
为什么?
WASM 编译参数:
"clang", <=== I only changed this to "clang++" - and it fails
"-O0",
// "-std=c++14",
"--target=wasm32-unknown-wasi",
"--sysroot C:\\OpenGL\\wasi-sdk-11.0-mingw.tar\\wasi-sdk-11.0\\share\\wasi-sysroot",
"-fno-exceptions",
"-nostartfiles",
"-Wl,--import-memory",
"-Wl,--no-entry",
"-Wl,--export-all",
"-o templates/my-app/public/hello_wasm.wasm",
"wasm/hello_wasm.cpp"
JS wasm 加载代码:
const response = await fetch("./hello_wasm.wasm");
const bytes = await response.arrayBuffer();
const { instance } = await WebAssembly.instantiate(bytes, {
env: { memory: this.memory },
},
});
this.instance = instance;
console.log("c" + instance);
})();
hello_wasm.cpp(编译没有错误):
#include <math.h>
// extern "C"
// {
int int_sqrt(int x)
{
return sqrt(x);
};
float *run_sin(float x[], int n)
{
// float *a = new float[n];
float *a = (float *)malloc(sizeof(float) * n);
for (int i = 0; i < n; i++)
{
a[i] = x[i] * 2;
}
return a;
}
LLVM v10 我使用来自https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk-11.0-mingw.tar.gz的 wasi sysroot
也在这里讨论这个问题https://github.com/WebAssembly/wasi-sdk/issues/145