Live data from Hacker News

Using Zig as cross-platform C toolchain

ruoyusun.com

21–30 of 42 posts

Re: Using Zig as cross-platform C toolchain

#21
post #9

"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 macO…

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.)

> 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

#22

Earlier quoted context omitted.

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.)

> 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.

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, , , 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

#23

Earlier quoted context omitted.

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.)

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.

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

#24
post #9

"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 macO…

> 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

#25
post #13

TIL about clang's -fsanitize=undefined, and immediately discovered and corrected a handful of subtle bugs lurking in one of my projects. Thanks, Zig, and thanks, Clang.

Crazy how often this catches undefined behavior. Compiling GLFW with Zig we've found like ~4 separate UB issues, one I described here[0] Wish it could default to on with clang, but probably it'd break too many things. Really wonder how many severe issues we'd find just from turning this on by default everywhere, though. [0] https://devlog.hexops.com/2021/perfecting-glfw-for-zig-and-f...

UBSAN adds a lot of runtime overhead. Turning it on my default would mean most software built with clang (or GCC, which has also had it for years) would suddenly be dog slow, like MSVCC in debug mode. Few developers have any idea how to use their toolchain correctly, so the defaults need to be chosen wisely.

Re: Using Zig as cross-platform C toolchain

#27

Earlier quoted context omitted.

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.)

> 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.

[deleted]

Re: Using Zig as cross-platform C toolchain

#28
post #9

"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 macO…

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.)

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

#29

Earlier quoted context omitted.

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.

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++.

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

#30
post #9

"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 macO…

> 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?

For example:

zig cc -target x86_64-linux-gnu.2.28

Post reply on HN