在 JS 中从 Emscripten 编译的 WASM 创建 WebAssembly.Instance,其中包括对 sprintf 的调用,会导致此错误:
Uncaught (in promise) LinkError: WebAssembly.Instance(): Import #1 module="env" function="_sprintf" error: function import requires a callable...
sprintf 不是 included by Emscripten作为 libc 的一部分?
代码:
#include <stdio.h>
extern "C" {
int main() {
char buffer [50];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
return 0;
}
}
编译命令:
emcc src/test.cpp -O3 -s WASM=1 -s SIDE_MODULE=1 -o out/test.wasm
emcc 编译运行没有错误。
注释掉 sprintf 行运行没有错误,按预期返回 0。
这个错误的原因是什么,在使用 sprintf 时如何避免?
最佳答案
Is sprintf not included by Emscripten as part of libc?
您正在使用
SIDE_MODULE=1
进行编译根据定义,它不会链接到系统库中。您可以提供自己的
sprintf
实现或者停止编译为侧模块并让 emscripten 为您处理它。
关于emscripten - WebAssembly 链接错误 : _sprintf function import requires a callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45260941/