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

Поділитися
Вставка
  • Опубліковано 18 вер 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
  • Наука та технологія

КОМЕНТАРІ • 85

  • @SlyEcho
    @SlyEcho 8 місяців тому +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 8 місяців тому +42

    Pls do a zig tutorial 🎉

  • @jsmxwll
    @jsmxwll 8 місяців тому +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 5 місяців тому +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 8 місяців тому +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 8 місяців тому +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. ❤❤

  •  8 місяців тому +4

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

  • @juanmamani2110
    @juanmamani2110 28 днів тому +1

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

  • @PaulSpades
    @PaulSpades 8 місяців тому +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 8 місяців тому +1

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

  • @zoomosis
    @zoomosis 8 місяців тому +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  8 місяців тому +4

      Yes, it uses Clang.

    • @kristoff-it
      @kristoff-it 8 місяців тому +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 8 місяців тому +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.

  • @AndrewKelley
    @AndrewKelley 8 місяців тому +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 8 місяців тому

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

    • @pietraderdetective8953
      @pietraderdetective8953 4 місяці тому

      @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 4 місяці тому

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

  • @bazoo513
    @bazoo513 8 місяців тому +1

    Thanks, Gary!

  • @firasnizam
    @firasnizam 8 місяців тому +2

    appreciate your wonderful videos

  • @minecraftermad
    @minecraftermad 8 місяців тому +2

    a video on zig lang itself would be hot too!

  • @ernestuz
    @ernestuz 8 місяців тому +5

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

  • @joeisuzu2519
    @joeisuzu2519 8 місяців тому +1

    Yes, pls do a zig tutorial. Thx

  • @naranyala_dev
    @naranyala_dev 7 місяців тому

    thank you sir, more zig please

  • @guilherme5094
    @guilherme5094 8 місяців тому

    Really nice!

  • @GlobalYoung7
    @GlobalYoung7 8 місяців тому

    thank you .

  • @aguijon6
    @aguijon6 8 місяців тому +1

    Thank you! 🤩

    • @GaryExplains
      @GaryExplains  8 місяців тому

      You’re welcome 😊

    • @test40323
      @test40323 8 місяців тому

      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?

  • @ScottLahteine
    @ScottLahteine 8 місяців тому +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  8 місяців тому +5

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

    • @ScottLahteine
      @ScottLahteine 8 місяців тому +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!

  • @johanngambolputty5351
    @johanngambolputty5351 8 місяців тому +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 8 місяців тому +5

      Yes.

    • @mgord9518
      @mgord9518 8 місяців тому +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

  • @jawadmansoor6064
    @jawadmansoor6064 7 місяців тому

    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.

  • @muhammadaneeqasif572
    @muhammadaneeqasif572 3 дні тому

    nice, intersting

  • @PUMAMicroscope
    @PUMAMicroscope 8 місяців тому +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 6 місяців тому +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 2 місяці тому

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

  • @muddyexport5639
    @muddyexport5639 8 місяців тому +1

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

  • @Chalisque
    @Chalisque 8 місяців тому

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

    • @zoomosis
      @zoomosis 8 місяців тому +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 8 місяців тому

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

  • @Finnigann2580
    @Finnigann2580 8 місяців тому

    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  8 місяців тому +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 8 місяців тому

      @@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 8 місяців тому +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  8 місяців тому +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  8 місяців тому +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.

  • @JLowe-uu8lr
    @JLowe-uu8lr 7 місяців тому

    Yes please !=8)

  • @MellexLabs
    @MellexLabs 8 місяців тому

    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  8 місяців тому +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 8 місяців тому +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 8 місяців тому +1

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

  • @9SMTM6
    @9SMTM6 8 місяців тому

    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 6 місяців тому

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

  • @cbbcbb6803
    @cbbcbb6803 8 місяців тому

    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 8 місяців тому

      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 8 місяців тому

      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 8 місяців тому

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

    • @chrboesch
      @chrboesch 8 місяців тому

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

  • @rapsod1911
    @rapsod1911 8 місяців тому

    Hellow size is 168kB? Why?

    • @chrboesch
      @chrboesch 8 місяців тому +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 8 місяців тому

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

    • @GaryExplains
      @GaryExplains  8 місяців тому +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 8 місяців тому +1

    Nothing can beat 8048 assembly.

  • @mrrolandlawrence
    @mrrolandlawrence 8 місяців тому

    zig and bun are so hot right now.

    • @shrin210
      @shrin210 8 місяців тому

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

  • @zoenagy9458
    @zoenagy9458 8 місяців тому

    will die like golang

    • @GaryExplains
      @GaryExplains  8 місяців тому +2

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

  • @suki4410
    @suki4410 8 місяців тому +2

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

    • @GaryExplains
      @GaryExplains  8 місяців тому +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 8 місяців тому +1

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

    • @donjindra
      @donjindra 8 місяців тому

      Not hardly.