TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

217 pointsby ern0about 3 years ago
A little help for programmers, who wants to run C&#x2F;C++ code in the browser.<p>(This is my second attempt to show it, first time I got banned bcoz of my personal page domain, I don&#x27;t really understand it why is is suspicious.)

17 comments

moefhabout 3 years ago
This is really cool! I never looked into WASM before, I&#x27;m really happy to see it&#x27;s not that difficult to build a simple example like this.<p>One thing that&#x27;s bothering me about that code is the declaration of `memory` as an uint8_t when it&#x27;s clearly being used for its address:<p><pre><code> extern uint8_t memory; </code></pre> And then in a <i>lot</i> of places:<p><pre><code> uint8_t* ptr = &amp;memory; ptr[FOO] = BAR; </code></pre> Declaring `memory` as an array is much more idiomatic C, and generates the exact same assembly:<p><pre><code> extern uint8_t memory[]; </code></pre> And then<p><pre><code> memory[FOO] = BAR; </code></pre> I just tested it, and it seems to work exactly as one would expect from a plain C linker (so there&#x27;s no WASM magic I can see). Am I missing something?
评论 #30993643 未加载
评论 #30998320 未加载
aquovaabout 3 years ago
If anyone is curious, I wrote a guide on Chip-8 emulation in Rust, and the final step was building the emulator to work in a browser via WASM. I do use a Cargo package to assist with targets and compilation, but other than that it allows you to build and load a wasm module into any old website.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;aquova&#x2F;chip8-book&#x2F;blob&#x2F;master&#x2F;src&#x2F;wasm.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;aquova&#x2F;chip8-book&#x2F;blob&#x2F;master&#x2F;src&#x2F;wasm.md</a>
gsprabout 3 years ago
Very cool!<p>I&#x27;ve been desperately looking for something similar for Rust. I&#x27;m too old for all these magic frameworks and &quot;deployers&quot; :-)<p>I have some Rust code, possibly linking in some objects compiled from C. How do I compile to wasm and deploy without magic? Any pointers?
评论 #30994809 未加载
syrusakbaryabout 3 years ago
This is really awesome, good work!<p>We did a similar thing running Clang on the browser, which is now uploaded to WAPM: <a href="https:&#x2F;&#x2F;wapm.io&#x2F;syrusakbary&#x2F;clang" rel="nofollow">https:&#x2F;&#x2F;wapm.io&#x2F;syrusakbary&#x2F;clang</a>
评论 #30992562 未加载
dosshellabout 3 years ago
directly when reading this code I cannot help myself but to get into review-mode. It is an awesome example, which i will probably use myself one day.<p>However...<p>inc.cpp:27<p>Gray is not the average of RGB, it is usally approximated with `Y = 0.299 R + 0.587 G + 0.114 B`.
评论 #30993204 未加载
评论 #30993614 未加载
评论 #30992954 未加载
eatonphilabout 3 years ago
Meta comment: your committer email is not in sync with your profile so the Github UI shows the commits attached to a blank user. If you want these two in sync you can either add your committing email to your Github account or you can modify your git config to use your Github email as your commit email.<p>Or you can just leave it as is too. :)
评论 #31012668 未加载
0x20cowboyabout 3 years ago
Not trying to steal your thunder, but here is another nostdlib clang -&gt; wasm example with malloc, a few math functions, rand, and writing to a canvas doing animation.<p>=&gt; <a href="https:&#x2F;&#x2F;github.com&#x2F;robrohan&#x2F;wefx" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;robrohan&#x2F;wefx</a>
melonyabout 3 years ago
This looks good! In my experience the hardest part for compiling WebAssembly is third party dependencies. Getting arbitrary libraries to build can be rather challenging.
评论 #30992388 未加载
评论 #31012724 未加载
david2ndaccountabout 3 years ago
I do a similar thing for my side project. Note that the clang that comes with Xcode (Apple clang) doesn’t have wasm support.
评论 #31012691 未加载
pettersabout 3 years ago
See also my example I put together some time ago: <a href="https:&#x2F;&#x2F;github.com&#x2F;PetterS&#x2F;clang-wasm" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;PetterS&#x2F;clang-wasm</a><p>At the time, I could not find something like this online.<p>It&#x27;s fun to write your own malloc! :-)
flohofwoeabout 3 years ago
Since I haven&#x27;t seen it mentioned in the comments yet, here&#x27;s another interesting project in the general area of &quot;WASM without Emscripten&quot;:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;schellingb&#x2F;wajic" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;schellingb&#x2F;wajic</a><p>This provides an alternative implementation of Emscripten&#x27;s EM_JS() magic (embed Javascript snippets right in the C&#x2F;C++ source code), but without requiring a full Emscripten SDK installation. It still needs some additional tools next to Clang though, so it sits somewhere between &quot;pure Clang&quot; and &quot;full Emscripten SDK&quot;.
drfuchsabout 3 years ago
(Obligatory:) Neat! Still, a lot of copying bits around, and more JS than one might hope for. Could you send a pointer to canvas into the C code and operate on it in a direct, zero-copy way there?
评论 #30996818 未加载
bla3about 3 years ago
These are also good resources on using wasm without dependencies:<p><a href="https:&#x2F;&#x2F;depth-first.com&#x2F;articles&#x2F;2019&#x2F;10&#x2F;16&#x2F;compiling-c-to-webassembly-and-running-it-without-emscripten&#x2F;" rel="nofollow">https:&#x2F;&#x2F;depth-first.com&#x2F;articles&#x2F;2019&#x2F;10&#x2F;16&#x2F;compiling-c-to-w...</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;diekmann&#x2F;wasm-fizzbuzz" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;diekmann&#x2F;wasm-fizzbuzz</a>
hobo_markabout 3 years ago
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 &quot;Runtime error: memory access out of bounds&quot;).<p>[1] <a href="https:&#x2F;&#x2F;tbfleming.github.io&#x2F;cib&#x2F;" rel="nofollow">https:&#x2F;&#x2F;tbfleming.github.io&#x2F;cib&#x2F;</a>
评论 #30995901 未加载
评论 #31001326 未加载
评论 #30992029 未加载
评论 #30992242 未加载
eeegnuabout 3 years ago
It seems your first attempt to post this was flagged because the domain name sounds like it&#x27;ll act like a redirect, and the page itself only had an image with no elaboration and just the link to github.
pulse7about 3 years ago
Is there a similar example for C?
评论 #31012711 未加载
评论 #30995074 未加载
jhgbabout 3 years ago
This is exactly what I needed. Thanks!