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.

Using Zig as cross-platform C toolchain

196 pointsby insraqabout 3 years ago

10 comments

slimsagabout 3 years ago
&quot;So, a clang wrapper?&quot; is a common thought, so here&#x27;s how Zig differs from clang out of the box:<p>* Links MachO binaries for Apple Silicon via the custom zld linker it ships. LLVM cannot do this currently.<p>* Provides (deduplicated) libc headers for pretty much every platform, including macOS and glibc&#x2F;musl. <a href="https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;zig&#x2F;tree&#x2F;master&#x2F;lib&#x2F;libc&#x2F;include" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;zig&#x2F;tree&#x2F;master&#x2F;lib&#x2F;libc&#x2F;include</a><p>* Provides a libc implementation (libSystem for macOS, musl and glibc, mingw for Windows, and WASI)<p>* Deals with lots of the deep depths of hell, like enabling you to target any version of glibc out of the box by building symbol mappings: <a href="https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;glibc-abi-tool&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;glibc-abi-tool&#x2F;</a><p>And that doesn&#x27;t mention the most important part, IMO, which is that it lets you cross compile _out of the box_. No fiddling with sysroots, system packages, etc. to get a cross compiling toolchain working.
评论 #30497611 未加载
评论 #30498814 未加载
评论 #30500576 未加载
评论 #30501981 未加载
RodgerTheGreatabout 3 years ago
TIL about clang&#x27;s -fsanitize=undefined, and immediately discovered and corrected a handful of subtle bugs lurking in one of my projects. Thanks, Zig, and thanks, Clang.
评论 #30497529 未加载
评论 #30502655 未加载
cube2222about 3 years ago
There&#x27;s a great article about using Zig as a cross-platform C toolchain for compiling CGo, to bring easy cross-platform compilation to it, which is usually a pain: <a href="https:&#x2F;&#x2F;dev.to&#x2F;kristoff&#x2F;zig-makes-go-cross-compilation-just-work-29ho" rel="nofollow">https:&#x2F;&#x2F;dev.to&#x2F;kristoff&#x2F;zig-makes-go-cross-compilation-just-...</a>
评论 #30498196 未加载
评论 #30499021 未加载
habermanabout 3 years ago
I recently learned that Clang supports this kind of cross-compiling out of the box. <a href="https:&#x2F;&#x2F;mcilloni.ovh&#x2F;2021&#x2F;02&#x2F;09&#x2F;cxx-cross-clang&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mcilloni.ovh&#x2F;2021&#x2F;02&#x2F;09&#x2F;cxx-cross-clang&#x2F;</a><p>The main difference is that Clang does not ship with headers&#x2F;libraries for different platforms, as Zig appears to do. You need to give Clang a &quot;sysroot&quot; -- a path that has the headers&#x2F;libraries for the platform you want to compile for.<p>If you create a bunch of sysroots for various architectures, you can do some pretty &quot;easy&quot; cross-compiling with just a single compiler binary. Docker can be a nice way of packaging up these sysroots (especially combined with Docker images like manylinux: <a href="https:&#x2F;&#x2F;github.com&#x2F;pypa&#x2F;manylinux" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pypa&#x2F;manylinux</a>). Gone are the days when you had to build a separate GCC cross-compiler for each platform you want to target.
评论 #30497565 未加载
nicoburnsabout 3 years ago
Zig&#x27;s cross-compilation tooling and C interop is really impressive. I love to see projects that put so much emphasis on solving real-world practical problems like this and implement it so well.<p>It makes me wonder whether cross-language tooling could be made to work more broadly. It would be awesome if you could have one compiler that would seamlessly compile zig&#x2F;C&#x2F;C++&#x2F;Rust, and perhaps Fortran&#x2F;ADA too.
githubholobeatabout 3 years ago
&quot;...an absolute joy to work with&quot; I agree. I used zig to make a game in WASM and had a blast. I also had some spare Arduino Nanos laying around and so I build a simple firmware in zig to test it. It worked great, so you can use zig for your embedded projects as well.
synergy20about 3 years ago
I&#x27;m almost sold, I checked zig briefly at its home page in the past and never used it. the cross platform build(similar to golang and rust) for c is very interesting.<p>what about the mac-os(or linux,etc) native libraries on windows where you&#x27;re building the demo code? The 60MB zig compiler and native clang on Windows will not have them, how is that done? Is it totally on clang side?
评论 #30497351 未加载
faraaz98about 3 years ago
I&#x27;m really liking the enthusiasm about zig these days. Kinda mirrors what first had when it was starting to become popular
jedisct1about 3 years ago
Zig can also target WebAssembly (standalone and WASI), so you don&#x27;t need to download and install yet another LLVM toolchain, or fiddle with libclang_rt.builtins-wasm32.a.
extheatabout 3 years ago
So basically a clang wrapper?
评论 #30497479 未加载
评论 #30497341 未加载
评论 #30498351 未加载