Most active commenters

    ←back to thread

    Zig is hard but worth it

    (ratfactor.com)
    401 points signa11 | 15 comments | | HN request time: 0.621s | source | bottom
    1. ldelossa ◴[] No.36150624[source]
    I'm surprised that the reason I'm mostly interested in Zig is not mentioned.

    This is C interop.

    I work with C quite a bit and I enjoy it, however writing a large project in C can be tiresome.

    Having an option like Zig which can import C headers and call C functions without bindings is pretty attractive, especially when you want to write something a big larger but still stay in C world.

    replies(6): >>36151152 #>>36151354 #>>36151700 #>>36153013 #>>36153780 #>>36158610 #
    2. workethics ◴[] No.36151152[source]
    Not only that, but zig makes linking against old glibc versions easy. For example, to make an x86_64 linux build linked against glibc 2.9 when using zig build all you have to do is pass:

      -Dtarget=x86_64-linux-gnu.2.9
    replies(3): >>36152369 #>>36158596 #>>36184449 #
    3. ziml77 ◴[] No.36151354[source]
    The thing that's espcially nice about that interop is that Zig includes its own C compiler. That eliminates the pain of having a build script locate an installed C compiler and figure out what options should be passed to it.
    replies(1): >>36156867 #
    4. Fiahil ◴[] No.36151700[source]
    C interop is not as much a "killer feature" as it used to be. When you deal with data science, web and other similar domains, reading and writing JSON ergonomically is much more important than being able to call a C function directly. It's just a nice-to-have.
    replies(1): >>36152631 #
    5. GordonS ◴[] No.36152369[source]
    Damn, that's a magnificent feature! Maybe I should have another look at Zig...
    6. afdbcreid ◴[] No.36152631[source]
    Yeah, but when you're doing systems programming (which Zig aims at if I understand correctly), easy interop with C is much more important than easy JSON marshalling.
    7. esjeon ◴[] No.36153013[source]
    C-interop is pretty perfect for most usecases. The only trouble I had was that, since command line arguments are converted to slices in Zig, I have to convert them back to null-terminated strings whenever I call C functions using any of the arguments. Nothing difficult but slightly painful.
    8. billfruit ◴[] No.36153780[source]
    What about c++ interop?
    replies(1): >>36154376 #
    9. Conscat ◴[] No.36154376[source]
    You can call internal and external linkage C++ functions from Zig, but you can't do anything interesting like specialize templates or evaluate constexpr functions.
    10. lost_tourist ◴[] No.36156867[source]
    Why would I use zig c compiler in place of gcc or clang? Mainly for zig interactivity or does it have some advantage other than that over the aforementioned compilers?
    replies(1): >>36156986 #
    11. TUSF ◴[] No.36156986{3}[source]
    Zig's C/C++ compiler is just clang, but with header files for most major platforms included, and sane defaults, so there's no hassle getting it it to cross-compile. Some companies have been using Zig solely for an easier to use clang.
    replies(1): >>36157078 #
    12. lost_tourist ◴[] No.36157078{4}[source]
    Ah I see, thanks for your answer!
    13. beefcafe ◴[] No.36158596[source]
    This is a killer feature for CTFs/OSCP/etc and why I started using it.
    14. bitshiffed ◴[] No.36158610[source]
    I love this part about zig too. It definitely makes interop with, or gradual migration from, C, much easier.

    It's also the source of my major problem with zig. It doesn't have its own ABI [1].

    So, if for example, you want to write a library in zig, to be used by others from zig, they must build your library with their project. That may not be an issue for smaller things; but for a large library I'd really like consumers to be able to pull in a binary with just a definition (header) file. Since zig uses the C ABI, that would currently mean translating everything to and from C at the binary interface, and losing all ziggyness in the process.

    [1] https://github.com/ziglang/zig/issues/3786

    15. TDiblik ◴[] No.36184449[source]
    Hi, why is this feature interesting/important? (Coming from [uneducated] webdeveloper with interest for systems/embedded programming)