carlos.baez carlos.baez - 1 year ago 86
C++ Question

Generated code via Emscripten doesn't return values

I try to implement a very simple example to return values from an array. My example seems simple but I couldn't understand why it is not working:

This my code in C++

static int results[20];

extern "C" {
void setValue(int index, int value {
results[index] = value;
int getValue (int index) {
return results[index];

My code in .html:

var val = Module.ccall('getValue','number',['number'],1);

My command for compiling:

emcc --bind -o face_quick_example.js face_quick_example.cpp -s EXPORTED_FUNCTIONS="['_setValue','_getValue']"

I checked the tutorial and some stackoverflow answers, but I couldn't it do working

Answer Source

When using ccall the arguments to be passed to the C function must be inside an array, so your JavaScript for calling setValue and getValue should be:

Module.ccall('setValue', null, ['number', 'number'], [1, 1]);
var val = Module.ccall('getValue', 'number', ['number'], [1]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download