Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
C++ Build WASM including libraries

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
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!