12 ms·
Zig cc: A drop-in replacement for GCC/Clang (2020)
- gus_massa 5y agoI don't understand the difference. Is "zig cc" more smart to find included files than the other compilers?
- wffurr 5y agoThe article goes into great detail of the differences.
- wyldfire 5y agozig distributes critical stuff like C library headers and libs. So it's not smarter, just more convenient. Built in support for several OS/architecture combos.
- notaplumber 5y agoAlthough as mentioned in the article, this won't work on systems like *BSD, Solaris or macOS.
- kubkon 5y agoOne tiny correction that we do support seamless C/C++ cross-compilation to macOS now with the advent of our own in-house linker `zig ld`. :-)
- azakai 5y agoIn addition, zig cc uses a remarkable compile+cache mechanism, which makes it possible to distribute a single build that can be used to cross-compile anything. It will cross-compile the necessary files as you need them, transparently - without downloading a new toolchain for each target, and quickly. zig cc is really, really cool, and it's incredibly generous that it provides zig's benefits to users of C and C++ as well.
- jlarocco 5y agoWhen you put it that way, this is an unfair comparison and the downsides are more obvious. With GCC and Clang, I only pay for cross compilation if I need it, so for a fair comparison, the cross-compiler libraries should be installed for GCC and Clang. Also with GCC and Clang, I don't have to worry about a third party (the Zig team) incorporating bug and security fixes from upstream. And with GCC I get support for more platforms. If I have chosen to use cross-compilation, building for alternative architectures is just as easy as for Zig.
- tempodox 5y agoPrevious discussion: https://news.ycombinator.com/item?id=22679138 https://news.ycombinator.com/item?id=22679138
- fractalb 5y ago`zcc` would be even better.
- tempodox 5y ago$ alias zcc='zig cc' Only works interactively, though.
- Andoryuuta 5y agoHaving "cc" as a sub-command with a space has caused some issues with Go (cgo), make, autotools, etc [0]. The latest proposal from andrewrk is a new zig subcommand which would automatically generate wrappers ("zig-cc", "zig-c++", etc) for for the target, then set them in the current env ("CC=zig-cc", "CXX=zig-c++") [1]. [0]: https://github.com/ziglang/zig/issues/8716 https://github.com/ziglang/zig/issues/8716 [1]: https://github.com/ziglang/zig/issues/8973 https://github.com/ziglang/zig/issues/8973
- pornel 5y agoI 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.
- pjmlp 5y agoWhat this kind of stories always miss is that the host platform needs all system libraries for the target platform for anything beyond the basic examples using the standard library. E.g. try to cross compile Metal shading example for iOS from Linux with zig, or do a Win2D UWP rendering application from Linux with zig.
- zamadatix 5y agoI'm not sure what's really "missed", if you call a library other than the stdlib of course you're going to need to provide the appropriate headers/libraries for that as that's got nothing to do with a C compiler.
- pjmlp 5y agoIn general, it isn't so clear when these kinds of examples are posted. And to go back to my examples, headers and libraries alone won't make it, without the remaining toolchain to create the appropriate package.
- zamadatix 5y agoWell for your particular examples UWP is C++/C#/a few others but it has no direct c binding and iOS APIs are all defined for Objective-C and Swift so sure, you're not going to get far with a C compiler before you even start thinking about a c cross compiler. If you want to cross compile C stuff e.g. a Win32 GUI app with Direct X or get OpenGL bindings for SDL though those all work just fine if you provide the SDK files same as you would compiling natively. The advantage being now you can do all builds from a single build instance just by feeding it the libraries and a target platform.
- lou1306 5y agoIt 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.
- bionade24 5y agozig cc uses LLVM under the hood.
- msingle 5y agoright now - my understanding is once the self-hosted compiler is done, LLVM will be optional.
- adamdusty 5y agoI think LLVM will still be required for release builds.
- csande17 5y agoI believe the plan is to make LLVM optional for compiling Zig code, but features that work with C code will still require Clang to be compiled in. Who knows, though. Someone might end up implementing a C compiler on top of the self-hosted Zig compiler's backend!
- Zababa 5y agoFrom what I understand, LLVM will still be used for release builds, while the self-holsted compiler will be used for quick feedback.
- linux2647 5y ago> 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!
- _benj 5y agoI’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
- skohan 5y agoThis is one of the things I really liked about working with Swift. You just got 50 years of C libraries for free, with high level features on top of it, which means there is a lot you can do.
- rhn_mk1 5y agoNote that none of autotools, make, and valgrind are required for C. You could be building with meson and ninja. Valgrind is also more useful than just for C, although admittedly it's most useful for binaries that have parts written in unchecked languages like C.
- tialaramex 5y agoRight. Even in an entirely safe language you can have leaks, and valgrind is an effective way to find those leaks if you can afford the virtualisation overhead. If you can't afford the virtualisation overhead, and you need to find leaks you should try what Raymond Chen suggests in "The poor man's way of identifying memory leaks" (not bothering to link since Microsoft will only move it anyway, they have several times since I read it). If you are too lazy to do it by hand, or find the technique works but wish it less manual, this is what Leakdice does: https://github.com/tialaramex/leakdice https://github.com/tialaramex/leakdice OR https://github.com/tialaramex/leakdice-rust https://github.com/tialaramex/leakdice-rust
- 5y ago
- jedisct1 5y ago`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.
- pjmlp 5y agoActually you can do the same with clang. Emscripten just installs a bunch of stuff to make it easier to use existing libraries. If you just want to use standard library, no need for extra stuff. https://surma.dev/things/c-to-webassembly/ https://surma.dev/things/c-to-webassembly/
- jedisct1 5y agoOnly for freestanding webassembly. In order to target WASI, you have to install the builtins (https://github.com/jedisct1/libclang_rt.builtins-wasm32.a https://github.com/jedisct1/libclang_rt.builtins-wasm32.a), as well as the WASI libc, and fiddle with `—-sysroot`. Zig can target both without having to install anything else.
- pjmlp 5y agoAssuming that you even need them. Zig packages them, naturally you don't need to install them a second time.
- ksec 5y agoFrom 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#Roadmap https://ziglang.org/download/0.8.0/release-notes.html#Roadma...
- kristoff_it 5y ago> 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? 1.0 is going to take a lot longer than that.
- geodel 5y agoI guess at least 2 major things after 0.9 would be package manager, library standardization and cleanup which would take many releases and perhaps need to be done before 1.0
- arusahni 5y agoAnother 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
- IgorPartola 5y agoI 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.
- WalterBright 5y agoD 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 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 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!
- msoucy 5y agoI miss playing with D. It looks like you've done a lot of neat stuff recently, but I just don't have a project that I can use it for. I remember being thoroughly impressed with its metaprogramming capabilities in college when I was working on dproto.
- Quekid5 5y agoThis is getting super-weird, love it! One assumes it is because C is so hard to rid ourselves of because POSIX (etc) that you're even entertaining this idea? I mean, "it's fun" could also work as motivation, I guess. I'm just curious!
- mhh__ 5y agoA decent subset of D programmers have used C and have been majorly bitten by it before, so it's definitely not a marriage of love put it that way. ImportC is, in my view, about optics and a little convenience - using C with D is already trivial if you understand how C works, ImportC just makes it offensively easy to do. There are some less theoretical things too, i.e. no more keeping kernel headers up to date (apart from ones that use inline ASM that we have to be careful with). With ImportC, D will be one of the better and non-hackier ways to interface C with other languages - i.e. introspection and metaprogramming but in a real language rather than some brain-dead DSL
- 5y ago
- manish_gill 5y agoI 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 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.
- coder543 5y agoThe thing I dislike about Zig error handling is that there's no way to associate informational context with an error. Bubbling the errors up, you'll eventually print out a string to the console that says "error: AccessDenied". The sysadmin/user running your software will be left baffled. Access denied... to what?! Zig is 90% of the way there with error handling, but it is extremely obvious to me that you need to be able to have error context carried with the error... it is apparently not obvious to the core Zig team[0], and this is a major impediment to my interest. Sure, if you're compiling to a microcontroller, maybe you're willing to sacrifice usability to avoid error values ever allocating... but most developers can spare an allocation for errors, especially given that error handling is generally not the hot path. [0]: https://github.com/ziglang/zig/issues/2647 https://github.com/ziglang/zig/issues/2647 (among other issues)
- dilap 5y agoTotally agree; it's almost perfect, but this is defnly needed to get to full perfection. While you can argue (as people do on that issue) that there are workarounds to carry error payload, the truth is if you don't make it ergonomic, many libraries will not do it, and that will filter down into software using those libraries, thus hurting the final resulting software. I'm hopeful this feature will eventually get added. (See also https://news.ycombinator.com/item?id=25798578 https://news.ycombinator.com/item?id=25798578)
- kristoff_it 5y ago> it is apparently not obvious to the core Zig team I think it really is not obvious once you start thinking about all the implications. Right now the recommended way of reporting extra information alongside errors is by using a "diagnostics" struct. You can see an extremely effective example of that in zig-clap, for example. https://github.com/Hejsil/zig-clap https://github.com/Hejsil/zig-clap Also this pattern was also mentioned in the issue you mentioned: https://github.com/ziglang/zig/issues/2647#issuecomment-589829306 https://github.com/ziglang/zig/issues/2647#issuecomment-5898... The advantage of this system is that the caller can decide whether error diagnostics are desirable or not. If they're not, then zig-clap doesn't waste cycles nor memory keeping track of the context in which an error happened. It's the best of both worlds in my opinion.
- vlovich123 5y agoCan it be used for c++ code? Also we build LLVM tip of tree from scratch. Is it easy to get the version of zig cc 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?)
- O_H_E 5y ago> Can it be used for c++ code? Yup! https://github.com/ziglang/zig/commit/db17c0d88c44183b3060993d85657e7637b43d68 https://github.com/ziglang/zig/commit/db17c0d88c44183b306099... > Also we build LLVM tip of tree from scratch... I would have also thought it would be non-trivial (Julia uses LLVM and has some non-backwards compatible patches) but this paragraph intrigued me: >> At the time of this writing, LLVM 10 was just released two hours ago. It will take days or weeks for it to become available in various system package managers. But you can already download a master branch build of Zig and play with the new features of Clang/LLVM 10. For example, improved RISC-V support! I found this for LLVM 12 (released April 14) https://github.com/ziglang/zig/commit/5a3ea9beced660c9cc463b921a1581cc07855dd6 https://github.com/ziglang/zig/commit/5a3ea9beced660c9cc463b... https://github.com/ziglang/zig/commit/e248de93a07fd6a7f48581ce0d9f6f77a69c76de https://github.com/ziglang/zig/commit/e248de93a07fd6a7f48581... https://github.com/ziglang/zig/commit/6a7f21d1b8ca5c082c804e0bbc4659d4e9b558ae https://github.com/ziglang/zig/commit/6a7f21d1b8ca5c082c804e... https://github.com/ziglang/zig/commit/1438c324a460789f1004a307a3267bcca3ee0bd3 https://github.com/ziglang/zig/commit/1438c324a460789f1004a3... Andrew seem to be following along their release pretty closely. Disclaimer: I don't know squat what I'm talking about
- flohofwoe 5y agoWhat'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).
- gigatexal 5y agoZig looks really compelling. Anyone use it in production?
- ifreund 5y agoThis person has been for many months now: https://www.youtube.com/watch?v=124wdTckHNY https://www.youtube.com/watch?v=124wdTckHNY We hope to be using TigerBeetle which is written in zig in production in 2022: https://github.com/coilhq/tigerbeetle https://github.com/coilhq/tigerbeetle
- O_H_E 5y agointeresting paragraph on why the builds are so small. > You can download a 45 MiB tarball, unpack it, and you're done. You can even have multiple versions at the same time, no problem......Compare this to downloading Clang, which has 380 MiB Linux-distribution-specific tarballs. Zig's Linux tarballs are fully statically linked, and therefore work correctly on all Linux distributions. The size difference here comes because the Clang tarball ships with more utilities than a C compiler, as well as pre-compiled static libraries for both LLVM and Clang. Zig does not ship with any pre-compiled libraries; instead it ships with source code, and builds what it needs on-the-fly.