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
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 90

  • @SlyEcho
    @SlyEcho 9 หลายเดือนก่อน +26

    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 10 หลายเดือนก่อน +41

    Pls do a zig tutorial 🎉

  • @jsmxwll
    @jsmxwll 9 หลายเดือนก่อน +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 6 หลายเดือนก่อน +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 10 หลายเดือนก่อน +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 10 หลายเดือนก่อน +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. ❤❤

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน +2

      Glad it helped!

  •  10 หลายเดือนก่อน +4

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

  • @AndrewKelley
    @AndrewKelley 9 หลายเดือนก่อน +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 9 หลายเดือนก่อน

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

    • @pietraderdetective8953
      @pietraderdetective8953 5 หลายเดือนก่อน

      @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 5 หลายเดือนก่อน

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

  • @PaulSpades
    @PaulSpades 10 หลายเดือนก่อน +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.

  • @zoomosis
    @zoomosis 10 หลายเดือนก่อน +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  10 หลายเดือนก่อน +4

      Yes, it uses Clang.

    • @kristoff-it
      @kristoff-it 10 หลายเดือนก่อน +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 10 หลายเดือนก่อน +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.

  • @juanmamani2110
    @juanmamani2110 2 หลายเดือนก่อน +1

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

  • @smithnigelw
    @smithnigelw 9 หลายเดือนก่อน +1

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

  • @bazoo513
    @bazoo513 10 หลายเดือนก่อน +1

    Thanks, Gary!

  • @firasnizam
    @firasnizam 10 หลายเดือนก่อน +2

    appreciate your wonderful videos

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน

      Glad you like them!

  • @ernestuz
    @ernestuz 10 หลายเดือนก่อน +5

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

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน +1

      Have fun!

  • @joeisuzu2519
    @joeisuzu2519 10 หลายเดือนก่อน +1

    Yes, pls do a zig tutorial. Thx

  • @guilherme5094
    @guilherme5094 10 หลายเดือนก่อน

    Really nice!

  • @naranyala_dev
    @naranyala_dev 8 หลายเดือนก่อน

    thank you sir, more zig please

  • @minecraftermad
    @minecraftermad 10 หลายเดือนก่อน +2

    a video on zig lang itself would be hot too!

  • @GlobalYoung7
    @GlobalYoung7 9 หลายเดือนก่อน

    thank you .

  • @PaulFurber
    @PaulFurber 24 วันที่ผ่านมา

    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.

  • @aguijon6
    @aguijon6 10 หลายเดือนก่อน +1

    Thank you! 🤩

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน

      You’re welcome 😊

    • @test40323
      @test40323 10 หลายเดือนก่อน

      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?

  • @muhammadaneeqasif572
    @muhammadaneeqasif572 หลายเดือนก่อน

    nice, intersting

  • @ZombieLincoln666
    @ZombieLincoln666 3 หลายเดือนก่อน

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

  • @ScottLahteine
    @ScottLahteine 10 หลายเดือนก่อน +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  10 หลายเดือนก่อน +5

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

    • @ScottLahteine
      @ScottLahteine 10 หลายเดือนก่อน +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!

  • @jawadmansoor6064
    @jawadmansoor6064 8 หลายเดือนก่อน

    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.

  • @muddyexport5639
    @muddyexport5639 10 หลายเดือนก่อน +1

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

  • @johanngambolputty5351
    @johanngambolputty5351 10 หลายเดือนก่อน +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 10 หลายเดือนก่อน +5

      Yes.

    • @mgord9518
      @mgord9518 9 หลายเดือนก่อน +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

  • @JLowe-uu8lr
    @JLowe-uu8lr 8 หลายเดือนก่อน

    Yes please !=8)

  • @PUMAMicroscope
    @PUMAMicroscope 9 หลายเดือนก่อน +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 8 หลายเดือนก่อน +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.

  • @Chalisque
    @Chalisque 10 หลายเดือนก่อน

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

    • @zoomosis
      @zoomosis 10 หลายเดือนก่อน +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 9 หลายเดือนก่อน

      @@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 10 หลายเดือนก่อน

    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  10 หลายเดือนก่อน +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 10 หลายเดือนก่อน

      @@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 10 หลายเดือนก่อน +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  10 หลายเดือนก่อน +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  10 หลายเดือนก่อน +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 10 หลายเดือนก่อน

    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  10 หลายเดือนก่อน +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 10 หลายเดือนก่อน +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 10 หลายเดือนก่อน +1

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

  • @9SMTM6
    @9SMTM6 9 หลายเดือนก่อน

    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 7 หลายเดือนก่อน

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

  • @cbbcbb6803
    @cbbcbb6803 9 หลายเดือนก่อน

    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 9 หลายเดือนก่อน

      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 9 หลายเดือนก่อน

      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 10 หลายเดือนก่อน

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

    • @chrboesch
      @chrboesch 10 หลายเดือนก่อน

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

  • @rapsod1911
    @rapsod1911 10 หลายเดือนก่อน

    Hellow size is 168kB? Why?

    • @chrboesch
      @chrboesch 10 หลายเดือนก่อน +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 10 หลายเดือนก่อน

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

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน +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).

  • @mrrolandlawrence
    @mrrolandlawrence 10 หลายเดือนก่อน

    zig and bun are so hot right now.

    • @shrin210
      @shrin210 9 หลายเดือนก่อน

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

  • @comment8767
    @comment8767 10 หลายเดือนก่อน +1

    Nothing can beat 8048 assembly.

  • @codingsafari
    @codingsafari 10 หลายเดือนก่อน

    I always start to cry within, when I see people putting the v flag on tar

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน +2

      Why? I rarely use tar without the v flag. I don't understand why you would want to cry about it 🤷‍♂️

    • @codingsafari
      @codingsafari 10 หลายเดือนก่อน

      @GaryExplains do you run all your tools with verbose output? Even if so, people can do it, if they want it. For some reason, you see the v flag in every video and tutorial. I think its just cargo culture at this point, and no one actually knows why they put it there.

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน +4

      I don't run every tool with verbose output no, but most file based tools, like rsync, tar, zip, etc I run with verbose as I like to double-check that I got the command right and it is doing what I think it is! This has saved me countless times.

  • @zoenagy9458
    @zoenagy9458 10 หลายเดือนก่อน

    will die like golang

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน +2

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

  • @suki4410
    @suki4410 10 หลายเดือนก่อน +2

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

    • @GaryExplains
      @GaryExplains  10 หลายเดือนก่อน +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 10 หลายเดือนก่อน +1

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

    • @donjindra
      @donjindra 10 หลายเดือนก่อน

      Not hardly.