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

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 90

  • @SlyEcho
    @SlyEcho Год назад +31

    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.

  • @jsmxwll
    @jsmxwll Год назад +12

    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 10 месяцев назад +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

  • @silakanveli
    @silakanveli Год назад +44

    Pls do a zig tutorial 🎉

  • @Winnetou17
    @Winnetou17 Год назад +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 Год назад +13

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

  •  Год назад +4

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

  • @PaulSpades
    @PaulSpades Год назад +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 Год назад +1

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

  • @AndrewKelley
    @AndrewKelley Год назад +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 Год назад

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

    • @pietraderdetective8953
      @pietraderdetective8953 9 месяцев назад

      @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 9 месяцев назад

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

  • @zoomosis
    @zoomosis Год назад +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  Год назад +4

      Yes, it uses Clang.

    • @kristoff-it
      @kristoff-it Год назад +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 Год назад +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.

  • @ernestuz
    @ernestuz Год назад +5

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

  • @juanmamani2110
    @juanmamani2110 5 месяцев назад +1

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

  • @minecraftermad
    @minecraftermad Год назад +2

    a video on zig lang itself would be hot too!

  • @firasnizam
    @firasnizam Год назад +2

    appreciate your wonderful videos

  • @joeisuzu2519
    @joeisuzu2519 Год назад +1

    Yes, pls do a zig tutorial. Thx

  • @bazoo513
    @bazoo513 Год назад +1

    Thanks, Gary!

  • @PaulFurber
    @PaulFurber 4 месяца назад

    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.

  • @naranyala_dev
    @naranyala_dev 11 месяцев назад

    thank you sir, more zig please

  • @ScottLahteine
    @ScottLahteine Год назад +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  Год назад +5

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

    • @ScottLahteine
      @ScottLahteine Год назад +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 Год назад +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 Год назад +5

      Yes.

    • @mgord9518
      @mgord9518 Год назад +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

  • @PUMAMicroscope
    @PUMAMicroscope Год назад +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 11 месяцев назад +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.

  • @jawadmansoor6064
    @jawadmansoor6064 11 месяцев назад

    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.

  • @aguijon6
    @aguijon6 Год назад +1

    Thank you! 🤩

    • @GaryExplains
      @GaryExplains  Год назад

      You’re welcome 😊

    • @test40323
      @test40323 Год назад

      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?

  • @guilherme5094
    @guilherme5094 Год назад

    Really nice!

  • @GlobalYoung7
    @GlobalYoung7 Год назад

    thank you .

  • @ZombieLincoln666
    @ZombieLincoln666 7 месяцев назад

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

  • @muddyexport5639
    @muddyexport5639 Год назад +1

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

  • @9SMTM6
    @9SMTM6 Год назад

    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 11 месяцев назад

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

  • @Chalisque
    @Chalisque Год назад

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

    • @zoomosis
      @zoomosis Год назад +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 Год назад

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

  • @muhammadaneeqasif572
    @muhammadaneeqasif572 4 месяца назад

    nice, intersting

  • @MellexLabs
    @MellexLabs Год назад

    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  Год назад +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 Год назад +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 Год назад +1

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

  • @cbbcbb6803
    @cbbcbb6803 Год назад

    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 Год назад

      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 Год назад

      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

  • @Finnigann2580
    @Finnigann2580 Год назад

    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  Год назад +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 Год назад

      @@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 Год назад +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  Год назад +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  Год назад +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 Год назад

    Yes please !=8)

  • @LLlblKAPHO
    @LLlblKAPHO Год назад

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

    • @chrboesch
      @chrboesch Год назад

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

  • @rapsod1911
    @rapsod1911 Год назад

    Hellow size is 168kB? Why?

    • @chrboesch
      @chrboesch Год назад +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 Год назад

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

    • @GaryExplains
      @GaryExplains  Год назад +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 Год назад

    zig and bun are so hot right now.

    • @shrin210
      @shrin210 Год назад

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

  • @comment8767
    @comment8767 Год назад +1

    Nothing can beat 8048 assembly.

  • @zoenagy9458
    @zoenagy9458 Год назад

    will die like golang

    • @GaryExplains
      @GaryExplains  Год назад +2

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

  • @codingsafari
    @codingsafari Год назад

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

    • @GaryExplains
      @GaryExplains  Год назад +2

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

    • @codingsafari
      @codingsafari Год назад

      @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  Год назад +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.

  • @suki4410
    @suki4410 Год назад +2

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

    • @GaryExplains
      @GaryExplains  Год назад +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 Год назад +1

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

    • @donjindra
      @donjindra Год назад

      Not hardly.