DEV Community

Discussion on: Passing strings from C++ to JavaScript in Web Assembly

Collapse
 
nebrius profile image
Bryan Hughes

Good point about the dangers of -s ERROR_ON_UNDEFINED_SYMBOLS=0, thanks!

I'll confess, I haven't explored using the --js-library flag yet. A quick read suggested it wouldn't do what I needed it to do, but I also may just not have read deeply into it enough.

Collapse
 
golinvauxb profile image
Benjamin Golinvaux

The thing is:
if, in your C++ code, you define something as:

extern int GetFoo();

Then, if you build the code, you'll get an undefined symbol error, UNLESS this function can be "seen" by emscripten. It can only see it if you put it in a js file that it knows of. That is, a file that is passed to the --js-library flag.

What I usually do is something like:

C++: extern "C" void SomeFunction();

JS (in library.js) : SomeFunction : function() { _SomeFunction() }

And _SomeFunction is declared in the JavaScript project where I load the wasm module in.

You can find more details here:

emscripten.org/docs/porting/connec...

HTH