Live data from Hacker News

Zig cc: A drop-in replacement for GCC/Clang (2020)

andrewkelley.me

41–50 of 126 posts

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#41
post #36
post #14

I’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 a…

Omg, skip the rest but valgrind is amazing and requires almost zero learning to use. Build your code with debug symbols, find inputs that trigger a segfault, then run it under valgrind. It then reports out of bounds memory access, with a full stacktrace. It's got a billion more uses, but at the most basic, that one use-case will keep you from wasting time futzing with a debugger or print-debugging 99% of segfaults an…

ok, I'm down!

Using zig I finally decided to try GDB and it is amazing!

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#42
Another 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

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#43
I 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.

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#44
post #7

I don't understand the difference. Is "zig cc" more smart to find included files than the other compilers?

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

Although as mentioned in the article, this won't work on systems like *BSD, Solaris or macOS.

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#45
post #36

Earlier quoted context omitted.

Omg, skip the rest but valgrind is amazing and requires almost zero learning to use. Build your code with debug symbols, find inputs that trigger a segfault, then run it under valgrind. It then reports out of bounds memory access, with a full stacktrace. It's got a billion more uses, but at the most basic, that one use-case will keep you from wasting time futzing with a debugger or print-debugging 99% of segfaults an…

Have you tried ASAN? If you like valgrind, you will be in love with ASAN, it's really surpassed it at this point IMHO.

I have tried ASAN, and I'm fully prepared to love it, but last time I tried to use it with Cython, it was pretty fidgety to configure my build system to get it running -- unlike valgrind, you need to understand how programs are compiled and linked. If you want ASAN to see into the Python that's calling/consuming your Cython, you need to build a custom Python. It's certainly a sharper tool, but valgrind has the learning curve of a hammer.

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#46
post #25
post #24

Earlier quoted context omitted.

`zig cc` cross-compiles C, not Zig.

No but I mean why the Zig cross compilation story is better than Rust according to this thread

Nothing about 'zig cc' is related to language semantics, it is entirely a toolchain feature. Language semantics do have some bearing on cross compiler difficulty, but IMHO it's not a major factor, especially within the sorts of languages that Rust and Zig are. The largest factor I can think of (compile-time code execution) exists in both Zig and Rust. It's possible I'm forgetting something, though.

It doesn't exist in Rust because nobody has proposed it or implemented it, simple as that.

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#47
post #36

Earlier quoted context omitted.

Omg, skip the rest but valgrind is amazing and requires almost zero learning to use. Build your code with debug symbols, find inputs that trigger a segfault, then run it under valgrind. It then reports out of bounds memory access, with a full stacktrace. It's got a billion more uses, but at the most basic, that one use-case will keep you from wasting time futzing with a debugger or print-debugging 99% of segfaults an…

Have you tried ASAN? If you like valgrind, you will be in love with ASAN, it's really surpassed it at this point IMHO.

Be careful with ASAN on cryptographic code, though. It can introduce side channels.

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#48
post #8

Earlier quoted context omitted.

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

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

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

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#49
D 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

It works by having an extra module called 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!

Re: Zig cc: A drop-in replacement for GCC/Clang (2020)

#50
post #29

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

Actually 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/

Only for freestanding webassembly. In order to target WASI, you have to install the builtins (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.

Post reply on HN