News for hackers. Stuff that matters.
Re:Using Zig as cross-platform C toolchain by slimsag on Monday February 28, 2022 @02:59AM (#30497472)
"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.

Re:Using Zig as cross-platform C toolchain by cyber_kinetist on Monday February 28, 2022 @03:22AM (#30497611)
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.)

Re:Using Zig as cross-platform C toolchain by afranchuk on Monday February 28, 2022 @08:50AM (#30499757)
Best to try it to figure it out. I tried compiling a trivial main function with `zig c++` (without cross compile) and the produced binary crashed when run. I think it's far more battle tested for C. But I look forward to C++ being improved as well.
Re:Using Zig as cross-platform C toolchain by flohofwoe on Monday February 28, 2022 @07:32AM (#30499119)
Yes, Zig is also a complete C++ and ObjC/C++ toolchain. For some operating system it also comes with system headers (e.g. for building Win32 or macOS applications).

It should be possible to use Zig as clang replacement in traditional C/C++ build systems, just by replacing the compiler invocation with 'zig cc', although I heard this sometimes causes subtle problems because of the space between 'zig' and 'cc', so it probably needs to be wrapped with a script file or maybe alias.

Re:Using Zig as cross-platform C toolchain by yellowapple on Monday February 28, 2022 @05:24AM (#30498352)
> Does it also work with C++?

It wraps clang, so it supports whatever clang supports, C++ included.

> Also, does it also work with build systems like CMake flawlessly?

I haven't personally tried it, but I'd expect that if all else fails you could add a step in build.zig to run CMake and then link against whatever library you just built.

Re:Using Zig as cross-platform C toolchain by cyber_kinetist on Monday February 28, 2022 @05:41AM (#30498446)
Does this include libc++ (compiled shared library of the STL complete with headers and debug symbols?) Although I hate most parts of the STL I still find myself using some parts of it (std::vector, std::string, <algorithm>, <type_traits>, etc.). Maybe I can link libc++ as an external library but then that's too much busywork needed for Zig to be a complete C++ dev environment out-of-the-box.
Re:Using Zig as cross-platform C toolchain by a_e_k on Monday February 28, 2022 @01:39PM (#30503322)
As an experiment a while back, I had tried in on a small project of mine that uses std::vector, std::string, and <algorithm> (though not a whole lot else). It built, ran, and passed my test suite.
Re:Using Zig as cross-platform C toolchain by nicoburns on Monday February 28, 2022 @05:09AM (#30498271)
I haven't actually used it myself, but I believe yes to both. It wraps clang, and while it's not quite as simple as "just a wrapper" it should be able to do anything that clang can do.
Re:Using Zig as cross-platform C toolchain by cyber_kinetist on Monday February 28, 2022 @05:42AM (#30498450)
Repeating my other comment, but I'm also interested if the STL also works out-of-the-box (is libc++ included?). Without it you can't really say it supports C++.
Re:Using Zig as cross-platform C toolchain by nicoburns on Monday February 28, 2022 @07:37AM (#30499157)
Can't find a definitive answer to this, but this merged PR looks like it might implement support for this: https://github.com/ziglang/zig/issues/4786
Re:Using Zig as cross-platform C toolchain by cyber_kinetist on Monday February 28, 2022 @07:53AM (#30499290)
That's good news. I might try using it someday...
Re:Using Zig as cross-platform C toolchain by formerly_proven on Monday February 28, 2022 @06:44AM (#30498814)
> enabling you to target any version of glibc out of the box by building symbol mappings: https://github.com/ziglang/glibc-abi-tool/

This would be huge. How can I tell zig cc to use a particular glibc version though?

Re:Using Zig as cross-platform C toolchain by ifreund on Monday February 28, 2022 @07:46AM (#30499233)
For example:

zig cc -target x86_64-linux-gnu.2.28

Re:Using Zig as cross-platform C toolchain by formerly_proven on Monday February 28, 2022 @10:49AM (#30501158)
That's very cool. While searching for it, I only found an older blog post where it said that internally it can do that, but there wasn't an option yet.

For anyone else trying this, it seems like Autotools/configure doesn't like CC with arguments, creating a wrapper script works though.

Re:Using Zig as cross-platform C toolchain by schemescape on Monday February 28, 2022 @10:06AM (#30500576)
And the whole thing is a (much) smaller download than Clang, somehow.

I gave it a try and was very impressed.

Re:Using Zig as cross-platform C toolchain by smeenai on Monday February 28, 2022 @11:50AM (#30501981)
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.