top | item 30488979

Using Zig as cross-platform C toolchain

196 points| insraq | 4 years ago |ruoyusun.com

38 comments

order
[+] emidoots|4 years ago|reply
"So, a clang wrapper?" is a common thought, so here's how Zig differs from clang out of the box:

* Links MachO binaries for Apple Silicon via the custom zld linker it ships. LLVM cannot do this currently.

* Provides (deduplicated) libc headers for pretty much every platform, including macOS and glibc/musl. https://github.com/ziglang/zig/tree/master/lib/libc/include

* Provides a libc implementation (libSystem for macOS, musl and glibc, mingw for Windows, and WASI)

* 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: https://github.com/ziglang/glibc-abi-tool/

And that doesn'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.

[+] cyber_kinetist|4 years ago|reply
Does it also work with C++? If so, it seems like a very good alternative compared to the MSVC + clang-cl toolchain in Windows (which you need to install gigabytes of libraries before doing any development).

Also, does it also work with build systems like CMake flawlessly? (I know that you can use Zig as a build system, but I would still want to leverage the ecosystem of C++ libraries compatible with CMake.)

[+] schemescape|4 years ago|reply
And the whole thing is a (much) smaller download than Clang, somehow.

I gave it a try and was very impressed.

[+] smeenai|4 years ago|reply
Not disagreeing with the rest, but LLD can link for Apple Silicon as well, and you could use Clang with zld as well if you wanted.
[+] RodgerTheGreat|4 years ago|reply
TIL about clang's -fsanitize=undefined, and immediately discovered and corrected a handful of subtle bugs lurking in one of my projects. Thanks, Zig, and thanks, Clang.
[+] slimsag|4 years ago|reply
Crazy how often this catches undefined behavior. Compiling GLFW with Zig we've found like ~4 separate UB issues, one I described here[0]

Wish it could default to on with clang, but probably it'd break too many things. Really wonder how many severe issues we'd find just from turning this on by default everywhere, though.

[0] https://devlog.hexops.com/2021/perfecting-glfw-for-zig-and-f...

[+] brandmeyer|4 years ago|reply
GCC supports many of the same sanitizers as well, since they were originally developed for GCC. We use GNU's ubsan and asan in our automated test suite.
[+] haberman|4 years ago|reply
I recently learned that Clang supports this kind of cross-compiling out of the box. https://mcilloni.ovh/2021/02/09/cxx-cross-clang/

The main difference is that Clang does not ship with headers/libraries for different platforms, as Zig appears to do. You need to give Clang a "sysroot" -- a path that has the headers/libraries for the platform you want to compile for.

If you create a bunch of sysroots for various architectures, you can do some pretty "easy" 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: https://github.com/pypa/manylinux). Gone are the days when you had to build a separate GCC cross-compiler for each platform you want to target.

[+] nicoburns|4 years ago|reply
Zig'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.

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/C/C++/Rust, and perhaps Fortran/ADA too.

[+] githubholobeat|4 years ago|reply
"...an absolute joy to work with" 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.
[+] synergy20|4 years ago|reply
I'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.

what about the mac-os(or linux,etc) native libraries on windows where you'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?

[+] _0w8t|4 years ago|reply
Apple does not allow to use their SDK on non-Apple hardware. So if you need to use MacOS headers and libraries, the hardware where the compiler runs must be from Apple. Apple has no problem if you run Linux on that and cross-compile from it to Mac.

At work we could not for that reason use the same compilation server setup to cross-compile for Linux, MacOS, Windows and ended up with a separated MacPro box for the compilation farm.

EDIT: cross-compilation from Windows works after one gets Windows SDK and Visual Studio libraries (the later is not free, but free alternatives often are sufficient). The biggest headache is that Windows headers assume a case-insensitive file system and includes, for example, windows.h when the file is Windows.h. One can either use case-insensitive mount option for ext4 or symlink relevant files to the proper case.

[+] faraaz98|4 years ago|reply
I'm really liking the enthusiasm about zig these days. Kinda mirrors what first had when it was starting to become popular
[+] jedisct1|4 years ago|reply
Zig can also target WebAssembly (standalone and WASI), so you don't need to download and install yet another LLVM toolchain, or fiddle with libclang_rt.builtins-wasm32.a.
[+] extheat|4 years ago|reply
So basically a clang wrapper?
[+] rbehrends|4 years ago|reply
Its caching system is also smart enough to handle #include dependencies, so for simple C/C++ projects, it can also function as a make alternative (without having to write a Makefile).

Plus, it makes cross-compilation really easy.

[+] faraaz98|4 years ago|reply
At first glance, yeah probably. But the advantage with zig is that it has c interoperability and the concept around the programmable build system (build.zig)
[+] user-the-name|4 years ago|reply
It's a clang wrapper that makes clang just work for cross-compilation.

In addition to being its own programming language and build system as well.