Why won't emscripten compile my functions?

360 Views Asked by At

I'm trying to compile some c++ code in to a wasm binary with functions included. However, even though I don't get any compilation errors or any other warnings during compilation, the files generated by emscripten don't include the functions I exported with "-s EXPORTED_FUNCTIONS=['....']"

Here's the file with functions I want to export: https://pastebin.com/B5w1R4BC

Here's the compilation command I'm using:

em++ -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 \
-Igameoflife/include -Os -DNDEBUG \
-s EXPORTED_FUNCTIONS="['_GOL_Instance_new', '_GOL_Instance_destroy', '_GOL_Init', '_GOL_Step', '_GOL_get_values']" \
-o gol.js gameoflife/src/cellmap.cpp bridge.cpp

Which runs without any issues.

However, when I import 'gol.js' into javascript, the Module object does not have access to any of the functions I'm trying to include (I am waiting for the module to get initialized before calling those functions).

TypeError: Module._GOL_Instance_new is not a function

Why can't I access these functions through wasm?

1

There are 1 best solutions below

2
On BEST ANSWER

They're likely being mangled by your C++ compiler. Declare them as extern "C" to avoid this:

extern "C"
GOL_Instance *
GOL_Instance_new() {
...