Is there an easy way to ask emscripten emit wasm & not mangling its names?

Viewed 115

Here is my C code:

#include <emscripten.h>
#ifdef __cplusplus
extern "C" {
#endif

EMSCRIPTEN_KEEPALIVE int add(int a, int b) {
    return a + b;
}
#ifdef __cplusplus
}
#endif

I run it with such command: emcc -O3 add.c -s EXPORTED_FUNCTIONS=_add, it gave me the wasm output which is close to what I want, but the name is mangled, the dumped output is something like below:


Type[2]:
 - type[0] () -> nil
 - type[1] (i32, i32) -> i32
Function[2]:
 - func[0] sig=0 <b>
 - func[1] sig=1 <c>
Table[1]:
 - table[0] type=funcref initial=1 max=1
Memory[1]:
 - memory[0] pages: initial=256 max=256
Export[4]:
 - memory[0] -> "a"
 - func[0] <b> -> "b"
 - func[1] <c> -> "c"
 - table[0] -> "d"
Code[2]:
 - func[0] size=3 <b>
 - func[1] size=7 <c>

Here c is the mangled add, is there any way to tell emcc to not mangle its name?

1 Answers

The JS library output by emscripten will export the unmangled name so you can still use externally.

To save space the names exported from wasm to JS are minified but the JS knows to then export them to outside by their original name. You can think of these exports from WebAssembly as the internal contract between the JS and the WebAssembly generated by emscripten. They are not designed to be accessed directly by you.

If you want to emscripten to generate a WebAssembly module that is designed for direct/external consumption and you don't want emscripten to generate the JS loader code you can output a standalone wasm file using by using one of the following methods:

  • Specify an output name that ends with .wasm (e.g. -o out.wasm)
  • Pass -sSTANDALONE_WASM

In this mode you should not see the export named minified.

Related