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

Sdílet
Vložit
  • čas přidán 18. 09. 2023
  • 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.
    ---
    Let Me Explain T-shirt: teespring.com/gary-explains-l...
    Twitter: / garyexplains
    Instagram: / garyexplains
    #garyexplains
  • Věda a technologie

Komentáře • 85

  • @SlyEcho
    @SlyEcho Před 8 měsíci +23

    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.

  • @silakanveli
    @silakanveli Před 8 měsíci +42

    Pls do a zig tutorial 🎉

  • @jsmxwll
    @jsmxwll Před 8 měsíci +10

    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.

    • @pyyrr
      @pyyrr Před 5 měsíci +2

      i tried to convert a guy to it, but they were too stubborn to even look at it. its a really nice tool to have, possibly even a must have

  • @Winnetou17
    @Winnetou17 Před 8 měsíci +8

    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!

  • @sriramvelamur
    @sriramvelamur Před 8 měsíci +11

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

  •  Před 8 měsíci +4

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

  • @juanmamani2110
    @juanmamani2110 Před 26 dny +1

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

  • @zoomosis
    @zoomosis Před 8 měsíci +7

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

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +4

      Yes, it uses Clang.

    • @kristoff-it
      @kristoff-it Před 8 měsíci +10

      Not exactly a fork, Zig calls directly into clang's main function, so there is no difference from normal clang from that point onwards. The interesting stuff happens before that though, Zig prepocesses all the flags you pass in to enable automatic libc selection for cross-compilation, builtin caching, ubsan enabled by default in debug builds, etc.

    • @109Rage
      @109Rage Před 8 měsíci +4

      Zig basically embeds Clang, but "with saner defaults" (as I've heard it put). Additionally, it includes C/C++ header files for common platforms, to make cross-compilation more seamless.

  • @PaulSpades
    @PaulSpades Před 8 měsíci +4

    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.

  • @smithnigelw
    @smithnigelw Před 8 měsíci +1

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

  • @AndrewKelley
    @AndrewKelley Před 8 měsíci +9

    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.

    • @ajithmaster1234
      @ajithmaster1234 Před 8 měsíci

      Zig might be the best/simplest thing i've used since learning C. Massive respect 🫡 ✊

    • @pietraderdetective8953
      @pietraderdetective8953 Před 4 měsíci

      @andrew that would be absolutely amazing to get Zig run easily in VScode. Yes you're correct the need to manually install makes me hesitate to introduce Zig to my students.

    • @AndrewKelley
      @AndrewKelley Před 4 měsíci

      @@pietraderdetective8953 it looks like the VSCode extension maintainers did this by the way!

  • @bazoo513
    @bazoo513 Před 8 měsíci +1

    Thanks, Gary!

  • @firasnizam
    @firasnizam Před 8 měsíci +2

    appreciate your wonderful videos

  • @ernestuz
    @ernestuz Před 8 měsíci +5

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

  • @joeisuzu2519
    @joeisuzu2519 Před 8 měsíci +1

    Yes, pls do a zig tutorial. Thx

  • @minecraftermad
    @minecraftermad Před 8 měsíci +2

    a video on zig lang itself would be hot too!

  • @naranyala_dev
    @naranyala_dev Před 7 měsíci

    thank you sir, more zig please

  • @guilherme5094
    @guilherme5094 Před 8 měsíci

    Really nice!

  • @aguijon6
    @aguijon6 Před 8 měsíci +1

    Thank you! 🤩

    • @GaryExplains
      @GaryExplains  Před 8 měsíci

      You’re welcome 😊

    • @test40323
      @test40323 Před 8 měsíci

      Zig is sick! C is my favourite language but I never knew about zig and its cross compile capabilities. Still trying to wrap my brain around Rust. Is MacOS still base on freeBSD, is that why it can reach that platform?

  • @GlobalYoung7
    @GlobalYoung7 Před 8 měsíci

    thank you .

  • @jawadmansoor6064
    @jawadmansoor6064 Před 7 měsíci

    some pip packages require c compiler, in windows it is really a pain, one needs to install visual studio with c lang support and so many things, that require over 10GB of space and around 2GB of download. can zig solve this problem for windows users? if so, how can this be done, thank you.

  • @ScottLahteine
    @ScottLahteine Před 8 měsíci +2

    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.

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +5

      If you use lots of GCC-specific flags then you will have problems, as zig uses clang as its compiler.

    • @ScottLahteine
      @ScottLahteine Před 8 měsíci +1

      @@GaryExplains Interesting. The project I manage (Marlin Firmware) uses PlatformIO, and that has so far never gotten along with Clang (e.g., when building the Marlin simulator). I’ll have to do some experiments to see what is possible. Thanks for the note!

  • @muhammadaneeqasif572

    nice, intersting

  • @johanngambolputty5351
    @johanngambolputty5351 Před 8 měsíci +4

    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?

    • @chrboesch
      @chrboesch Před 8 měsíci +5

      Yes.

    • @mgord9518
      @mgord9518 Před 8 měsíci +2

      Yes, this is what people are talking about when they talk about Zig's really good C interop.
      You can import C header files to access C types and Zig has very easy ways to export to C ABI. With C++, you first have to write C bindings, but interop with C++ is still very easy.
      C types are first class citizens in Zig, and Zig has special types for null terminated C strings, null terminated slices, etc. No need to convert to the "native" string before using

  • @muddyexport5639
    @muddyexport5639 Před 8 měsíci +1

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

  • @PUMAMicroscope
    @PUMAMicroscope Před 8 měsíci +2

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

    • @kenchilton
      @kenchilton Před 6 měsíci +2

      It works for M1 and M2 because Apple chose to contribute to and support LLVM. The Zig compiler currently leverages the LLVM backend, so everything plays nicely together.
      The C/C++ compatibility is how to get X11 or any other GUI interface, like Qt, working with your zig code. You can import C includes and call C library functions from within the zig source. Look at this capability and your X11 integration will become obvious.
      Of course, it works the other way as well. You can rewrite C modules or libraries in Zig and use them with C objects, because Zig uses the C ABI.

  • @ZombieLincoln666
    @ZombieLincoln666 Před 2 měsíci

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

  • @Chalisque
    @Chalisque Před 8 měsíci

    I never new tar xvf automatically recognises compressed archives. When did it start doing that?

    • @zoomosis
      @zoomosis Před 8 měsíci +2

      I think GNU tar has supported that for at least 15 years now.
      Incidentally these days BSD tar is based on libarchive, which supports zip, rar, 7z, iso and others. libarchive was used by Microsoft in a recent update to Windows 11 to finally support rar, 7z etc natively in Windows Explorer.

    • @Chalisque
      @Chalisque Před 8 měsíci

      @@zoomosislol. I learned tar back in the days where it was gzip and compress only. Then bzip2 was cool and you had to use j for it. After that I didn't relearn anything until the a switch came along. At least I know now.

  • @JLowe-uu8lr
    @JLowe-uu8lr Před 7 měsíci

    Yes please !=8)

  • @9SMTM6
    @9SMTM6 Před 8 měsíci

    Isn't this particular use-case under threat? There is a proposal to remove the dependency on LLVM (and with that Clang) from Zig. As far as I understood, this would lead to zig cc not being feasible any longer, at the very least not with the ease it is right now and not for C++.

    • @halfgrin
      @halfgrin Před 6 měsíci

      Using LLVM will still be an option. There's no plans to remove the zig cc functionality.

  • @Finnigann2580
    @Finnigann2580 Před 8 měsíci

    To learn to program a computer we need to use a computer language. But they are difficult to learn. Is there progression of languages one could learn in sequence that would build a skill set?

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +2

      I don't think programming languages are hard to learn. True, the advanced features are harder to learn, and most languages are hard to truly master. If you want to learn, start by downloading Thonny and learn some Python.

    • @minecraftermad
      @minecraftermad Před 8 měsíci

      @@GaryExplains i always wonder if python is a good place to start, sure it's simple, but that simplicity might be a detriment. or maybe it should be said that you can learn with python, and then move on to another language that has features for making larger projects, like types, fast compilation times, etc, which python (atleast currently) is missing.
      so perhaps the best thing is to say, start with something simple, like python, even scratch. and once you think you have somewhat of a handle on it, move on to c#,java, C, (maybe rust?),zig :)

    • @comment8767
      @comment8767 Před 8 měsíci +2

      Just learn C ... it is not that hard, and it only has about 35 key words. Python is not easier, and really looks like an elaboration of C. Get the Kerngah (sp?) and Richie book used for $5.

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +1

      Simplicity can never be a detriment. The concepts of variables, loops, flow control, data structures, strings etc are fundamental to all programming languages. Learning them in Python won't hinder learning about them and other things in a different language, rather it will help.

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +1

      I would never recommend C as the way into learning to program. Never. Always start with something simpler, and something that doesn't end in a "segmentation fault" when there is a bug. No, Python is way, way better for beginners.

  • @MellexLabs
    @MellexLabs Před 8 měsíci

    Ahhh such a relief... i really dislike the c/c++ build system... so how does make and cmake fit into all this zig compiler stuff...

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +2

      There are two approaches. One is just to use make and cmake as you would with gcc, but replace the CC command as "zig cc" rather than "gcc". You can also use the zig build system. The zig build system isn't simple, but it is comprehensive. As I say in the video llama.cpp can be compiled using the zig build system and its corresponding build.zig file.

    • @MellexLabs
      @MellexLabs Před 8 měsíci +2

      @GaryExplains The second option may just be a good replacement to move away from make... from what I have seen, the Zig build system is complicated... however, if I start writing code in Zig, then I could potentially just use one language for my application and build system...

    • @minecraftermad
      @minecraftermad Před 8 měsíci +1

      @@MellexLabs *evil laughter* we've gained another with a devious trap.

  • @cbbcbb6803
    @cbbcbb6803 Před 8 měsíci

    Are there any books on Zig that are available for beginners? I prefer books. But i can use videos, if that is what is only available. My fingers are crossed!

    • @shrin210
      @shrin210 Před 8 měsíci

      Books 😅 Old School Scholar
      Why books? Zig currently doesn't even have proper documentation. Not realised v1.0 yet.
      Internet is your only hope.

    • @mgord9518
      @mgord9518 Před 8 měsíci

      Zig isn't yet stable enough to warrant books being written. Although I guarantee once it hits 1.0, it'll have a lot of great community resources available, hopefully including a few books.
      Currently your best bet is the official docs and reading the stdlib, both are very readable and helpful

  • @LLlblKAPHO
    @LLlblKAPHO Před 8 měsíci

    В чем проблема установить компилятор на виндовс?

    • @chrboesch
      @chrboesch Před 8 měsíci

      Simple as in Linux, unpack the archive and set the path to the folder.

  • @rapsod1911
    @rapsod1911 Před 8 měsíci

    Hellow size is 168kB? Why?

    • @chrboesch
      @chrboesch Před 8 měsíci +4

      When you compile without options you have all debug information in the binary. With for instance '-Doptimize=ReleaseSmall' it will be only 5 kB.

    • @rapsod1911
      @rapsod1911 Před 8 měsíci

      @@chrboesch Yes, I understand that, but there is also separated .pdb and that thing is also huge.

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +2

      @rapsod1911 Isn't the .pdb file just debugging information? Debugging files are not shipped and are huge by design (so that you have the max amount of debug info).

  • @comment8767
    @comment8767 Před 8 měsíci +1

    Nothing can beat 8048 assembly.

  • @mrrolandlawrence
    @mrrolandlawrence Před 8 měsíci

    zig and bun are so hot right now.

    • @shrin210
      @shrin210 Před 8 měsíci

      Zig is hot but Bun is just hype how it is being presented now.

  • @zoenagy9458
    @zoenagy9458 Před 8 měsíci

    will die like golang

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +2

      I wasn't aware that golang has died, nor was I aware that C compilers in general had died 🤷‍♂️

  • @suki4410
    @suki4410 Před 8 měsíci +2

    for me, it is just a waste of time, to compile anything for windows, because it is obsolete.

    • @GaryExplains
      @GaryExplains  Před 8 měsíci +3

      Windows is obsolete, what has taken its place? Last I heard around 1.4 billion people are using Windows 10 or 11 monthly. Is that what you call obsolete?

    • @Tatar_Piano
      @Tatar_Piano Před 8 měsíci +1

      ​@@GaryExplainsprob he means it's obsolete on servers?

    • @donjindra
      @donjindra Před 8 měsíci

      Not hardly.