Hacker News Re-Imagined

`Zig Cc`: A Powerful Drop-In Replacement for GCC/Clang

5 hours ago

Created a post 210 points @AlexeyBrin

`Zig Cc`: A Powerful Drop-In Replacement for GCC/Clang

@manish_gill 1 hour

Replying to @AlexeyBrin 🎙

I literally started learning Zig today after watching a talk on youtube by Andrew Kelly. The drop-in C compiler replacement lured me.

But what surprised me immediately and the thing I'm absolutely loving is the error handling (https://twitter.com/mgill25/status/1416720958988210177) and the comptime features.

I've done Rust and while I love what it's doing, getting into low-level C-world without the overhead of a language like Rust feels liberating.

Reply


@tempodox 4 hours

Replying to @AlexeyBrin 🎙



@arusahni 1 hour

Replying to @AlexeyBrin 🎙

Another thing that's really cool, here: they've published the language (and toolchain) to PyPi. So, with a simple

    pip install ziglang
You can do

    python3 -m ziglang cc my_c_prog.c -o my_c_prog
    ./my_c_prog
A great way to do compilation on a system without needing to get a C toolchain installed, if you already have Python available

Reply


@IgorPartola 1 hour

Replying to @AlexeyBrin 🎙

I looked at Zig a little while ago. What I really liked was some of the clever features of the language and thr C interop. What made me not pursue it for now is that I mostly use C for embedded development and Zig doesn’t have meaningful support for the ESP8266 platform. Also, not fully memory safe which obviously not the end of the world for me but if I’m going to switch away from C then I kind of want that as a thing.

Reply


@ksec 3 hours

Replying to @AlexeyBrin 🎙

From Release Note 0.8 Roadmap [1],

>The major theme of the 0.9.0 release cycle will be stabilizing the language, creating a first draft of the language specification, and self-hosting the compiler.

So I am guessing we could see 1.0 release within 12 months time? Or will there be 0.10 or 0.11 after that?

I am just eager to hear people's comments and opinions for using it in production within their context and edge cases.

And as a sort of side note, I quite like the momentum and the way Andrew has been handling Zig's PR. I just hope and pray HN keep that tradition, dont over hype it.

[1] https://ziglang.org/download/0.8.0/release-notes.html#Roadma...

Reply


@vlovich123 1 hour

Replying to @AlexeyBrin 🎙

Can it be used for c++ code? Also we build LLVM tip of tree from scratch. Is it easy to get the version of zig VC built against the newer version or does one have to wait for the project authors to update to the latest (I’m assuming my it’s a non-trivial amount of work?)

Reply


@flohofwoe 44 minutes

Replying to @AlexeyBrin 🎙

What's usually not mentioned is that zig cc is also an ObjC/ObjC++ compiler, which is important on macOS to talk to system framework APIs. Zig 0.8.0 still has some warts on macOS (for instance one needs to tell Zig to use the Apple system linker), but those will be fixed in 0.9.0 (AFAIK it already works in the main branch).

Reply


@lou1306 3 hours

Replying to @AlexeyBrin 🎙

It does look impressive! Does anyone know if Zig exposes some APIs to extract/manipulate/analyze the AST of the C program? It might be a nice alternative to LLVM, which I find a little overwhelming.

Reply


@fractalb 4 hours

Replying to @AlexeyBrin 🎙

`zcc` would be even better.

Reply


@linux2647 3 hours

Replying to @AlexeyBrin 🎙

> Take a moment to appreciate what just happened here - I downloaded a Windows build of Zig, ran it in Wine, using it to cross compile for Linux, and then ran the binary natively. Computers are fun!

Reply


@_benj 3 hours

Replying to @AlexeyBrin 🎙

I’ve been learning Zig for a few months now and what I’m in love with is the ability to just use Zig to interact with whatever C libraries I want, but doing so with the conveniences that Zig offers.

I want to use SDL2 and cairo and pango? No problem! GTK4, easy peasy! There’s a C lib that comes as a single .c file that makes it easier to interact with SDL? Drop it on the project folder and compile it with Zig along all the other .zig files!

As somebody that has been avoiding C as much as I can, I’m loving this approach since it has open the doors to the world of C for me without having to learn autotools, make, valgrind, et al

Reply


@jedisct1 3 hours

Replying to @AlexeyBrin 🎙

`zig cc` is also amazing to compile C and C++ code to WebAssembly. For standalone WebAssembly or WASI.

No need to install yet another toolchain, this is just another target Zig can compile to, out of the box.

Reply


@pornel 4 hours

Replying to @AlexeyBrin 🎙

I really like what they've done for cross-compilation. Automatically selecting appropriate libc/crt implementation is such a relief.

Compared to Zig, cross-compilation in C/C++/Rust seems downright user-hostile: you have to obtain the correct cross-sdk, but it won't tell you which one, or how to get it (some combos don't even exist), or even where to put it. If you don't guess everything correctly, it will fail with inscrutable errors, or worse, only fail at runtime on the target machine.

Reply


@WalterBright 1 hour

Replying to @AlexeyBrin 🎙

D is taking a different approach. D is getting a builtin C compiler (called ImportC) that is a from scratch C compiler, now in prototype form.

https://dlang.org/spec/importc.html

It works by having an extra module called cparse.d

https://github.com/dlang/dmd/blob/master/src/dmd/cparse.d

which parses Standard C. The lexer and semantic routines of the D compiler are then tweaked to support C semantics. The same optimizer and code generator is used.

As a side effect of using the D compiler's innards, ImportC can do things like handle forward references, and even execute C code at compile time!

Reply


@gus_massa 4 hours

Replying to @AlexeyBrin 🎙

I don't understand the difference. Is "zig cc" more smart to find included files than the other compilers?

Reply


About Us

site design / logo © 2021 Box Piper