Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • howto-wasm-minimal

    How to create minimal wasm module with plain C/C++

  • For future readers, this is the code that is being referred:

    https://github.com/ern0/howto-wasm-minimal/blob/master/inc.c...

    It should be:

       uint32_t gray = 0.299 * ptr[i] + 0.587 * ptr[i+1] + 0.114 * ptr[i+2];

  • v86

    x86 PC emulator and x86-to-wasm JIT, running in the browser

  • I'm investigating v86 [1] to get a c compiler in the browser. There's an example with a 5.5 mb Build Root Linux example [2] where you can pass files and commands between the browser and vm with a Lua compiler. The vm boots within a few seconds in Firefox on my Pixel 2 XL. There's also an example where Lua code is passed from the host, but you have to download the GitHub repo to see it [3]

    [1] https://github.com/copy/v86

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • cib

    clang running in browser (wasm)

  • That is half of what I would need for a project, the other half being Clang itself running in the browser (to use for teaching), in theory there is [1] since many years, but in practice it never worked for me (even now I get "Runtime error: memory access out of bounds")

    [1] https://tbfleming.github.io/cib/

  • wefx

    Basic WASM graphics package to draw to an HTML Canvas using C. In the style of the gfx library

  • Not trying to steal your thunder, but here is another nostdlib clang -> wasm example with malloc, a few math functions, rand, and writing to a canvas doing animation.

    => https://github.com/robrohan/wefx

  • wasm-fizzbuzz

    WebAssembly from Scratch: From FizzBuzz to DooM.

  • These are also good resources on using wasm without dependencies:

    https://depth-first.com/articles/2019/10/16/compiling-c-to-w...

    https://github.com/diekmann/wasm-fizzbuzz

  • clang-wasm

    How to build webassembly files with nothing other than standard Clang/llvm.

  • See also my example I put together some time ago: https://github.com/PetterS/clang-wasm

    At the time, I could not find something like this online.

  • chip8-book

    An introduction to Chip-8 emulation using Rust

  • I made some emulators in Rust as a learning project during the start of the pandemic, and ran into the exact same issue when I wanted to make a wasm version to run in a browser. Eventually, I was able to figure out how to do it, although I do use the 'wasm-pack' Cargo package to assist with it (I think you can get away without it if you're really motivated, you just need to set up the targets and other elements yourself). Basically you define some Rust API to expose whatever you need from your project, then that and the project get compiled into one .wasm binary and some (surprisingly readable) JavaScript "glue" gets generated which allows for easy inclusion into a web page. It works well for code in the std, but I've had issues with 3rd party packages.

    It's focused on emulation development, but I wrote a document that describes the process I followed: https://github.com/aquova/chip8-book/blob/master/src/wasm.md

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • matrix-algebra

    Some simple Matrix Algebra in WebAssembly

  • llvm-project

    This is the canonical git mirror of the LLVM subversion repository. The repository does not accept github pull requests at this moment. Please submit your patches at http://reviews.llvm.org. (by binji)

  • This was a topic at CppCon'19 (seems like exactly the same use case as you are describing: teaching!). Take a look at https://www.youtube.com/watch?v=5N4b-rU-OAA. I think it relies on the LLVM fork, so not sure how well it'll work for you, quick web search points to: https://github.com/binji/llvm-project.

  • wajic

    WebAssembly JavaScript Interface Creator

  • Since I haven't seen it mentioned in the comments yet, here's another interesting project in the general area of "WASM without Emscripten":

    https://github.com/schellingb/wajic

    This provides an alternative implementation of Emscripten's EM_JS() magic (embed Javascript snippets right in the C/C++ source code), but without the Emscripten SDK. It still needs some additional tools next to Clang, so it sits somewhere between "pure Clang" and "full Emscripten SDK".

  • KolorWheel.js

    🌈 Color palette generator JavaScript library

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts