Live data from Hacker News

Aro – Zig's new C compiler

github.com

11–20 of 130 posts

Re: Aro – Zig's new C compiler

#11

I'm not all that familiar with the Zig tool chain. Am I understanding correctly that this is a regular C compiler used as a component of the Zig compiler chain allowing compilation of C files alongside Zig? Or does Zig generate C and therefore need a C compiler under the hood? Looking at the documentation suggests it is the former, not the latter.

It says it's a C frontend for `zig translate-c` which translates C code to Zig code. `zig translate-c` is not required for most Zig projects or even for most Zig projects that interop with C.

Re: Aro – Zig's new C compiler

#12

The dlang D compiler includes a full C compiler, so you can mix and match C and D code. You can import C code from D, and D code from C. dmd hello.c ./hello hello world This helps enormously in binding to existing C libraries.

Is it possible to do what Zig does and cross compile to any OS/arch, from any OS/arch?

Last I tried, I found some compiler options and commands to do it, but they were badly explained, I tried for hours and it just didn't work at all, so I gave up.

Re: Aro – Zig's new C compiler

#13
post #12

The dlang D compiler includes a full C compiler, so you can mix and match C and D code. You can import C code from D, and D code from C. dmd hello.c ./hello hello world This helps enormously in binding to existing C libraries.

Is it possible to do what Zig does and cross compile to any OS/arch, from any OS/arch? Last I tried, I found some compiler options and commands to do it, but they were badly explained, I tried for hours and it just didn't work at all, so I gave up.

The dmd D compiler can cross-compile to any of the targets that it supports. I use it all the time to develop on other platforms.

I am currently adding AArch64 support to DMD's code generator. dmd doesn't run on my Raspberry Pi (yet!), so I use sshfs to access the Pi's filesystem from Ubuntu, compile programs on Ubuntu, transfer the .o file to the Pi, link it on the Pi, and run it on the Pi.

I'm sorry you're having trouble with this, so I recommend posting on the D forums, and people are there to help!

https://forum.dlang.org/

Re: Aro – Zig's new C compiler

#14

I'm not all that familiar with the Zig tool chain. Am I understanding correctly that this is a regular C compiler used as a component of the Zig compiler chain allowing compilation of C files alongside Zig? Or does Zig generate C and therefore need a C compiler under the hood? Looking at the documentation suggests it is the former, not the latter.

It says it's a C frontend for `zig translate-c` which translates C code to Zig code. `zig translate-c` is not required for most Zig projects or even for most Zig projects that interop with C.

Thank you.

I also started reading through the Zig overview [1] and near the end of that document it says this: "Not only can Zig compile C code, but there is a very good reason to use Zig as a C compiler: Zig ships with libc."

From the doc, IIUC, compilation of header files and other C code allows Zig to do direct interop with no intermediaries. Seems useful.

[1] https://ziglang.org/learn/overview/

Re: Aro – Zig's new C compiler

#16

The dlang D compiler includes a full C compiler, so you can mix and match C and D code. You can import C code from D, and D code from C. dmd hello.c ./hello hello world This helps enormously in binding to existing C libraries.

D is so underrated. Keep up the good work Walter.

Re: Aro – Zig's new C compiler

#17

I'm not all that familiar with the Zig tool chain. Am I understanding correctly that this is a regular C compiler used as a component of the Zig compiler chain allowing compilation of C files alongside Zig? Or does Zig generate C and therefore need a C compiler under the hood? Looking at the documentation suggests it is the former, not the latter.

It says it's a C frontend for `zig translate-c` which translates C code to Zig code. `zig translate-c` is not required for most Zig projects or even for most Zig projects that interop with C.

translate-c is not required for compiling pure Zig code. However, the plan is to remove the "@cImport" built in. So if your project is importing a c header, in the future you'll add build step translating it from c to Zig, and then you import it into your Zig code as a module.

Re: Aro – Zig's new C compiler

#18
post #12

The dlang D compiler includes a full C compiler, so you can mix and match C and D code. You can import C code from D, and D code from C. dmd hello.c ./hello hello world This helps enormously in binding to existing C libraries.

Is it possible to do what Zig does and cross compile to any OS/arch, from any OS/arch? Last I tried, I found some compiler options and commands to do it, but they were badly explained, I tried for hours and it just didn't work at all, so I gave up.

Generally, the hard part isn't the cross compiler (nor, these days, the cross binutils), but the cross libraries. There are at least 3 ways (/usr/lib/$ARCH, /usr/$ARCH/lib, /$CHROOT/usr/lib/) to set up the libraries, all incompatible with each other.

Zig is notable for putting a lot of work into hard-coding basic C libraries, though of course you still need a scattering of other C libraries for most real-world programs.

(for a sufficiently advanced language ecosystem, the language's own libraries should be easy enough)

Re: Aro – Zig's new C compiler

#20

Very nice, honestly the tooling around Zig is even more impressive than the language itself.

The language itself is the reason the tooling exists though, because it attracts a certain kind of developer that probably would not have done the same kind of work for other languages.
Post reply on HN