Zig as a C and C++ compiler

Hello everyone, so in my search of trying to find cross compiling for C and C++, I came across Zig. I heard of Zig as a language before, but then I came across a video and sources (example: Can you bring your own C compiler with the zig build system? - #2 by dimdin) saying the Zig comes with a whole toolchain to compile C and C++ code as a drop in replacement for any C/C++ compiler you may be using now, with a great benefit of eastly cross compiling.

This seemed great because, at least on Windows natively, the MinGW-w64 GCC only provides the toolchain for Windows, and clang can cross compile but needs you to provide sysroot and libc for the platform. This got me interested as even if I don’t use the Zig language itself (for now), it provides a easy way to cross compile C and C++ with zig cc and zig c++ to many platforms quite easily from the looks of things.

Now during a bit more searching I found out that this is LLVM under the hood with all the libc and such already provided which seemed fine. Then I came across this open issue on Github: make the main zig executable no longer depend on LLVM, LLD, and Clang libraries · Issue #16270 · ziglang/zig · GitHub, by the looks of it, it looks like the Zig team want to move away from LLVM. Now, I don’t really understand a lot of the technical stuff being said, so I come with a simple question.

How does this affect Zig to compile C and C++ code? Will zig cc and zig c++ still work the same, and will be able to cross compile, and will it still be the drop in replacement as it was before?

For short answer, C and C++ (cross) compilation is not going anywhere.

That being said, there was some effort to modularize / specialize the Zig build system. It should not remove any functionality, but might move it a bit around, especially when it comes to translating C to Zig code

LLVM is intended to be still around as an alternative backend, but not the only one anymore (new backend defaultly for Debug builds, Release ones possibly in the future).

If i remember correctly, there was some talk about the new backend supporting it in the future too via arocc, which is also used in the translate-c above (or mabye it works even now, i am not sure how new backend handles C/C++).

Robert :blush:

3 Likes

Even Zig itself will remain dependent on zig cc/zig c++ for the foreseeable future because we need them to build musl, libc++, etc. They’re definitely not going anywhere. There may be some minor changes to how you obtain and invoke them, but the functionality itself is staying.

3 Likes