Using ZIG as a Drop-In Replacement C Compiler on Windows, Linux, and macOS!

preview_player
Показать описание
Zig is a general-purpose, open source programming language for Windows, Linux, and macOS. Its features include no hidden control flow or a preprocessor. However it can also be used as a zero-dependency, drop-in C/C++ compiler that supports cross-compilation out-of-the-box. In this video I demonstrate how to use it as a C/C++ compiler and how to do cross compilation.
---

#garyexplains
Рекомендации по теме
Комментарии
Автор

It's nice to see someone finding something I did useful! as I was the one who wrote the llama.cpp Zig instructions. Anyway, some more comments about Zig:

1. You can write a build.zig file for building a complex project and it is using a real programming language as opposed to Make.
2. You can mix C and Zig code and compile them together and not just linking on the function level but Zig code can import C headers directly and use the functions and types from them in Zig.
3. As well as using different C libraries like glibc and musl, it can also use different versions of them.
4. The Zig download page is available in JSON format so you can create scripts to automate downloading it for jobs like CI.
5. It is possible to use it with Make and CMake too with CC='zig cc' and so on.

SlyEcho
Автор

zig is outstanding. it really simplifies a lot of compilation steps. i use the zig build system for most of my c projects and zig itself has mostly replaced c in any new personal projects i am building. it's good stuff. zig interops with the c abi so easily that it's almost transparent.

jsmxwll
Автор

Yes, more videos on Zig please!

From what I've seen some time ago, it's good when you need specific memory management, it makes doing that easy, especially if you need multiple "ways" of allocation and deallocation in the same program.
Also, if I remember correctly they have this approach of not having things done "behind the back" something like. That is, to be explicit on most (all?) things. On principle, I like that!

Winnetou
Автор

That was a live saver, Gary. Thanks a lot for that.

I've been running around a while for a solution to generate x86-86-linux binaries from my Mac work machine and almost gave up when this one surfaced on the feed! Thanking my stars and your video for this. ❤❤

sriramvelamur
Автор

Excellent video. Really helped me understand the cross compiler. Zig is a monster in waiting!

TheMachoMan
Автор

I knew about this, but it's a great real-world demonstration. Maybe try and port that thread testing tool to zig and let us know what you think about the language :)
Zig needs more content on youtube, the tutorials are either bare bones or very confusing.

PaulSpades
Автор

Nice examples of how Zig makes cross-compilation JUST WORKS!

Автор

Thanks Gary. Nice video. Zig is very cool. I hope you can show us all some more about zig.

smithnigelw
Автор

This was really nice! Thank you for making this video. I think it would be great if the Zig VSCode extension automatically installed Zig for you, and then it would be just that much simpler to get started for Windows users. I can imagine this would be pretty handy for computer science courses.

AndrewKelley
Автор

a video on zig lang itself would be hot too!

minecraftermad
Автор

"zig cc -v" suggests the C compiler is just a fork of Clang, which is a little less exciting, though not a bad thing. Zig's simple cross-compilation is nice.

zoomosis
Автор

Best 17m invested of the day! really thanks for sharing

juanmamani
Автор

Great!, I never heard of Zig, and here I am now, writing some test programs :)

ernestuz
Автор

zig is getting to be very impressive technology indeed. If I understand the docs correctly you can create a library using zig with a C ABI so that C can call into Zig code and all of its nice advanced data structures transparently.

PaulFurber
Автор

Hmmm, I knew zig was supposed to be quite compatible with c and c++, but I assumed it was through a standard FFI, but does this mean that you can compile zig, c and c++ together and access structs/functions in one language almost like they are defined natively in the other? I.e. without as much of the boilerplate/data wrangling, converting to and from c_types and such?

johanngambolputty
Автор

Every Zig for great justice! It'll be interesting to see if my multi--platform embedded project would build with "zig cc" …. It uses lots of GCC-specific flags.

ScottLahteine
Автор

that's a pretty big hellow.exe executable cross compiled for windows!

DavidDyck
Автор

Zag the Zig for more info/content and thank you very much!

muddyexport
Автор

Thanks for this. Can it really cross compile a binary for M1 macOS (I mean if you are working on Linux can you '-target' it to compile a binary to work on macOS)? I thought you needed proprietary Apple Xcode libs, sdks and all that. Does all that come with the 'single folder' zig download? Seems too good to be true (and so probably is - unless you have tried it and can confirm otherwise?).
UPDATE: I did manage to cross-compile standard terminal/console-based programs that work on macOS M1 with this zig cc - so I can confirm it works. No idea how to compile with Xlib/X11 though for GUI - if anyone finds out please let me know.
Interestingly the same program I cross-compiled for macOS ran about the same speed as a native linux compile using gcc BUT when I do a native linux compile with zig cc for Linux of the same prog it runs about 5 times slower than native gcc binary or macOS M1 zig cc cross compiled binary. So, if speed matters for native Linux progs I would not use zig cc as a 'drop in' compiler but it is useful as a cross-compiler to macOS M1 for Linux people who don't have direct access to Apple hardware (like me).

PUMAMicroscope
Автор

will do this on windows instead of fussing with MSVC or mingw

ZombieLincoln
join shbcf.ru