This shows how to build a nontrivial program using Zig+Emscripten or C+Emscripten.
In both cases Emscripten is only used as a linker, that is the frontend is either zig
or clang
.
"Nontrivial" here means the program uses interesting Emscripten features:
- Asyncify
- Full GLES3 support
- GLFW3 support
The Zig version can be built with something like:
zig build-obj -isystem …/emsdk/upstream/emscripten/cache/sysroot/include/ triangle.zig -target wasm32-wasi
emcc triangle.o -o zig.html -s FULL_ES3=1 -s USE_GLFW=3 -s ASYNCIFY -s STANDALONE_WASM -s EXPORTED_FUNCTIONS=_run_zig --no-entry -O3
(Note: the target is wasi and not emscripten, as it looks like the Zig frontend doesn’t have that as an option atm.)
You also need to write _run_zig()
in the dev console to start up the application, as the Zig startup code is not integrated with Emscripten yet and we need to do it manually. (You can also add that in the HTML, but it needs to run after the wasm is all ready, which is async.)
The C version can be compiled and linked with:
clang triangle.c -isystem …/emsdk/upstream/emscripten/cache/sysroot/include/ -target wasm32-unknown-emscripten -c
emcc triangle.o -o c.html -s FULL_ES3=1 -s USE_GLFW=3 -s ASYNCIFY -O3