TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

217 点作者 ern0大约 3 年前
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 条评论

moefh大约 3 年前
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 未加载
aquova大约 3 年前
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>
gspr大约 3 年前
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 未加载
syrusakbary大约 3 年前
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 未加载
dosshell大约 3 年前
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 未加载
eatonphil大约 3 年前
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 未加载
0x20cowboy大约 3 年前
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>
melony大约 3 年前
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 未加载
david2ndaccount大约 3 年前
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 未加载
petters大约 3 年前
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! :-)
flohofwoe大约 3 年前
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;.
drfuchs大约 3 年前
(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 未加载
bla3大约 3 年前
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_mark大约 3 年前
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 未加载
eeegnu大约 3 年前
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.
pulse7大约 3 年前
Is there a similar example for C?
评论 #31012711 未加载
评论 #30995074 未加载
jhgb大约 3 年前
This is exactly what I needed. Thanks!