0

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?

Abhishek Sharma
  • 109
  • 1
  • 8

1 Answers1

0

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

extern "C"
GOL_Instance *
GOL_Instance_new() {
...
Bernard
  • 45,296
  • 18
  • 54
  • 69
  • Woah, this worked like a charm. Functions are viewable by js now. I didn't find any mention of this anywhere. How would I have figured this out? – Abhishek Sharma Oct 22 '18 at 23:47
  • It's mentioned [here](https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html), but as more of an aside. – Bernard Oct 23 '18 at 01:25