I'm struggling with an error compiling WASM including cmath library.
What I need to do is being able to use sqrt function in JavaScript... If I delete the sqrt function and the cmath library, all works fine... Can someone, help me to understand what I'm doing wrong?
Here the C++ code:
#include <cmath>
extern "C" {
int Sum(int a, int b) {
return a + b;
}
int sub (int a, int b){
return a - b;
}
double root (int a){
return sqrt(a);
}
}
This is what I run on terminal to generating the WASM file
em++ -std=c++2b "PATH_TO_CPP_FILE" -Oz -s WASM=1 -s SIDE_MODULE=1 -s BINARYEN_ASYNC_COMPILATION=0 -o "PATH_TO_A_FOLDER\FILENAME.wasm"
This is the JavaScript code I'm usign:
const importObject = {
module: {},
env: {
memory: new WebAssembly.Memory({ initial: 256 }),
}
};
WebAssembly.instantiateStreaming(
fetch('main.wasm'),
importObject
).then(result => {
const Sum = result.instance.exports.Sum;
const sub = result.instance.exports.sub;
console.log(Sum(4, 5));
console.log(Sum(10, 10));
console.log(sub(20, 10));
});
Finally this is the error I'm currently having:
Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #0 module="env" function="_Z4sqrtIiENSt3__29enable_ifIXsr3std11is_integralIT_EE5valueEdE4typeES2_" error: function import requires a callable