Creating the WebAssembly.Instance in JS from a Emscripten compiled WASM which included a call to sprintf, results in this error:
Uncaught (in promise) LinkError: WebAssembly.Instance(): Import #1 module="env" function="_sprintf" error: function import requires a callable...
Is sprintf not included by Emscripten as part of libc?
The code:
#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;
}
}
The compilation command:
emcc src/test.cpp -O3 -s WASM=1 -s SIDE_MODULE=1 -o out/test.wasm
The emcc compilation runs without error.
Commenting out the sprintf line runs without error returning 0 as expected.
What's the reason for this error and how can it be avoided when using sprintf?
Is sprintf not included by Emscripten as part of libc?
You're compiling with SIDE_MODULE=1
which by definition does not link in system libraries.
You can provide your own implementation of sprintf
or stop compiling as a side module and allow emscripten to take care of it for you.