Hacker News new | ask | show | jobs
by ptato 1155 days ago
Zig embeds clang to compile C code. This doesn't add a new dependency since Zig already depends on LLVM. If there is a future where the self-hosted Zig backend is good enough to not depend on LLVM anymore, there might be a reason to use a C compiler written in Zig (possibly https://github.com/Vexu/arocc)
2 comments

Also worth noting that Zig embeds C stdlib source code (musl if I'm not mistaken). That means it is easier to cross compile C projects using zig since you don't need to install a cross toolchain. This is why some golang/rust projects use Zig when they need to cross compile.
It adds a new dependency - the c language frontend - aka clang - surely the zig compiler emits LLVM IR, not C code?
zig supports importing C headers via the special built-in `@cImport`. That feature requires a C language front end to operate. https://ziglang.org/documentation/0.10.1/#Import-from-C-Head...
Zig has a C backend that supports something like 99% of all Zig if I'm remembering the number correctly.

Zig has almost completely solved the bootstrap problem.

This isn't quite correct. Yes, it can output C code; however the result is not very readable at all, and fails the DFSG on generated code. It _is_ useful for compiling Zig code to targets which aren't supported by LLVM, however.
Do you have a reference for this? Does it use a C backend that is part of LLVM, or is it something Zig-specific? What are its limitations? Can it compile libraries to C, or only entire applications?

I have wanted Rust and Zig to support compile-to-C for a while, so this is exciting news for me.

One thing that would particularly interest me is if functions intended to be inlined could be emitted into .h files.