Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda