install Emscripten
Example C/C++ Runtime Code
create file hello_world.c
#include <stdio.h>
int main() {
printf("hello, world!\n");
return 0;
}Compiling to JavaScript
emcc hello_world.c
this should generate 2 files:
- a.out.js - JavaScript file
- a.out.wasm - WebAssembly file
you can run them using Node.js
node a.out.js
this prints “hello, world!” to console
Generating HTML
emcc hello_world.c -o hello.html
you can open hello.html in a web browser
Optimizing Code
-01 minor optimization
emcc -01 hello_world.c
-02 aggressive optimization
emcc -02 hello_world.c