Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
Calling C function from JavaScript using emscripten

I am currently trying to compile C to wasm and use it on a website. But I have had zero luck so far.

What I currently have is the following C source file:

#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE
int sum(int a, int b) {
  return a + b;
}

I compile it using the following emscripten command:

emcc test.c -o test.js -s EXPORTED_FUNCTIONS="['_sum']" -sMODULARIZE

This produces test.wasm and test.js.

Then, I have the following html code:

<html>
<!--...-->
<body>
  <script type="module">
    var _import = import('test.js');
    _import.then((instance) => {
      console.log(instance._sum());
    });
  </script>
</body>
</html>

When I start up a development server, I get the following error in the console:

Uncaught (in promise) TypeError: instance._sum is not a function
    <anonymous> http://localhost:8000/:7
    promise callback* http://localhost:8000/:6

I also tried instance.sum and instance.ccall('sum'), both with the same result.

I have no clue at this point what I am doing wrong.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda