Compiling webassembly with Emscripten

Source: Internet
Author: User
Tags emcc emscripten

Need to compile the C language demo program ADD.C

#include <stdio.h>
int add (int a,int b)
{
    return a+b;
}
int main (void)
{
    printf ("%d\n", add);
}

1, can be compiled with HTML output file, you can directly open the HTML file to see the effect

EMCC add.c-s Wasm=1-o add.html

Where-s wasm=1 must be added, otherwise the default generated file is not *.wasm but JS file

2, most of the situation is not recommended to output HTML files, directly generate wasm files and JS files. The generated JS file has the interface called webassembly, we just need to call the JS file interface.

EMCC add.c-s wasm=1-s Side_module=1-o add.js

3, if you want to write JS call webassembly, there are many ways, one of the following:

<script>
 fetch (' add.wasm '). Then (res =
  Res.arraybuffer ()
 ). Then (buf = {let
  m_buf = new Uint8array (BUF);
  Let API = Wasm.instantiatemodule (M_BUF);
  Console.log ("123+1024=", Api.exports.add (123,1024));
 });
</script> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.