emscripten-core / emscripten-core/emscripten

Question regarding dynamic library : SIDE and MAIN module

Open
#16,719 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.8-git
clang version 15.0.0 (https://github.com/llvm/llvm-project.git 80ec0ebfdc5692a58e0832125f2c6a991df9d63f)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /usr/local/Cellar/emscripten/3.1.8/libexec/llvm/bin
```

Hi, It's a long time I did not play with Emscripten but I am back at it and I am facing some issue understanding the MAIN_MODULE and SIDE_MODULE.

In C++ I have a dynamic library "Core" with a list of Class exported using EMSCRIPTEN_BINDING
I also have another library "Module" also with a list of Class exported using EMSCRIPTEN_BINDING
Module use the library Core

The first approach I did where to use a static library of Core and then standard emscripten I was able to have my two js file and my two wasm file. The problem is some functionality (to stay simple let say the level of Logging) use static function and variable from Core and of course when using the Module.js the initialization of this variable because of the static library is not correct.

That why in C++ I am using a dynamic library.

My understanding if MAIN_MODULE and SIDE_MODULE seems to be what I am looking for. So I build the Core with
```
"-s MAIN_MODULE=1 dist/module.wasm")
```
and the Module with
```
"-s SIDE_MODULE=1")
```

So when loading the core.js in the browser I can see the signature of my function from Module but when trying to instantiate my object :
```
my_module = new namespace.Module()
```
I have this error :
```
Unhandled Promise Rejection: RuntimeError: call_indirect to a signature that does not match (evaluating 'asm[name].apply(null, arguments)')
```

My object as a default constructor with no parameters. So I have few question :

1 - Am I using the flag correctly ?
2 - I will have more module using core later is it also ok to have the MAIN_MODULE to have a list of other wasm.
3 - In C++ because the Module need Core the build go Core first and the Module. But with the flag MAIN_MODULE and SIDE_MODULE it seems I need to have the SIDE_MODULE wasm generated first otherwise I cannot build the MAIN_MODULE. Is it normal ?

Thanks for your help.

PS: As a side note I am using the emscripten CMake toolchain, it seems it's not able to understand correctly to output only a wasm file, when using SIDE_MODULE it's outputing a js file, I rename this file into .wasm
```
em++: warning: output suffix .js requested, but wasm side modules are just wasm files; emitting only a .wasm, no .js [-Wemcc]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.