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

151
Views
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
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!