4

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.