How to link the static library when compile C/C++ file to wasm?

1,442 views
Skip to first unread message

Arion Wong

unread,
Sep 2, 2018, 10:07:16 PM9/2/18
to emscripten-discuss
I am working on windows 10. And I want to compile a C++ file  to wasm. This C++ code call function from a static library.

I compile the file with this command line :emcc cppfile.cpp -Os -s WASM=1 -s SIDE_MODULE=1 -o cppfile.wasm -s RUNTIME_LINKED_LIBS=['library.lib']  -I "F:/wzm/MonoProject/include"

I load the wasm file with this code :

function loadWebAssembly(filename, imports = {}) {
 
return fetch(filename)
   
.then(response => response.arrayBuffer())
   
.then(buffer => WebAssembly.compile(buffer))
   
.then(module => {
      imports
.env = imports.env || {}
     
Object.assign(imports.env, {
        memoryBase
: 0,
        tableBase
: 0,
        memory
: new WebAssembly.Memory({ initial: 256, maximum: 256 }),
        table
: new WebAssembly.Table({ initial: 0, maximum: 0, element: 'anyfunc' })
     
})
     
return new WebAssembly.Instance(module, imports)
   
})
}

But when I load the wasm file , the console return me a error:
Uncaught (in promise) LinkError: WebAssembly Instantiation: Import #1 module="env" function="_mono_class_get_method_from_name" error: function import requires a callable
    at fetch.then.then.then.module (http://localhost:8000/loader.js:13:14)
    at <anonymous>

It mean I don't link the library.
How to link the static library?

Alon Zakai

unread,
Sep 4, 2018, 4:21:54 PM9/4/18
to emscripten-discuss
That error means it expects a function to be provided for that import, so your exports object must contain a property "env", and on that object, a property "_mono_class_get_method_from_name".

How are you generating the imports?

Based on the name of the missing import, I'm guessing you may need all of mono - the easiest thing is to build it normally with emscripten and let emscripten do the loading of the library for you. However, if your library just needs a tiny subset of the functionality, perhaps you can implement it directly somehow. I'd take a look at the list of imports for the wasm module to see.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages