【问题标题】:WebAssembly LinkError: _sprintf function import requires a callableWebAssembly LinkError:_sprintf 函数导入需要可调用
【发布时间】:2017-07-23 02:12:29
【问题描述】:

在 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 时如何避免?

【问题讨论】:

    标签: emscripten webassembly emcc


    【解决方案1】:

    Emscripten 是否不将 sprintf 作为 libc 的一部分包含在内?

    您正在使用SIDE_MODULE=1 进行编译,根据定义,它不会链接到系统库中。

    您可以提供自己的 sprintf 实现或停止编译为侧模块并让 emscripten 为您处理。

    【讨论】:

      猜你喜欢
      • 2017-10-21
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 2019-10-17
      相关资源
      最近更新 更多