In my journey to learning C, I’ve come across header files, which are used to (I’m assuming) define a prototype for the source file, as well as structure modules. This feature, in my opinion, is pointlessly not just redundant, but possibly a source for pitfall. The same information can probably be extracted from the source code, if not for the restrictions of the language specification in C.

Say, if I have a GTK project, I will have to use the preprocessor directive, that will require the use of GTK headers that look something like #include <gtk/gtk.h>, and they’re usually in the system path. How do modern languages, like Rust, Zig or Go deal with this situation, where shared libraries are used?

  • Ephera
    link
    fedilink
    arrow-up
    1
    ·
    18 minutes ago

    To my knowledge, they generally don’t. Shared libraries are a massive pain from a programming perspective, so it’s largely been avoided (even though there are some advantages for Linux distro maintainers and sysadmins).

    Modern languages usually bring along their own package manager, which downloads the libraries you want to use from a central repository and then bundles them into your build artifact that you hand to users.
    In ‘semi-modern’ languages, like the JVM languages and I think also Python, you get the dependencies in a folder as part of a larger archive.
    With Go and Rust, they decided to include those libraries directly into the executable, so that many programs can just ship a single executable.

  • Perhyte@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    2 hours ago

    I’ve never used Rust or Zig, but for Go: (disclaimer: this is all from memory, so there may be inaccuracies or out-of-date information here)

    Go does not allow circular references between modules. That restriction allows the compiler, when compiling a module, to not only put the compiled machine code in the resulting object file for that module but also the information that in C would be required to be in a header file (i.e. type definitions, function signatures, and even complete functions if they’re considered candidates for inlining, etc.). When compiling a module that imports others, the compiler reads that stuff back out of those files. Essentially a compiled Go library has it’s auto-generated “header file” baked-in.

    In older versions this was actually human-readable: an early part of the object file would essentially look like trimmed-down Go when opened in a text editor. IIRC they’ve switched to a binary serialization format for this some years back, but AFAIK it still essentially works the same.

    I guess when comparing to C or C++, you could compare this to automatically generating pre-compiled headers for every module, except the headers themselves are also auto-generated (as you alluded to in your post).

    If by “shared library” you mean a dynamically linked one: IIRC Go does allow shared libraries to be used, but by default all Go code is linked statically (though libraries written in other languages may be dynamically linked by default, if you import a module that requires it).

    • Ephera
      link
      fedilink
      arrow-up
      1
      ·
      31 minutes ago

      If by “shared library” you mean a dynamically linked one: IIRC Go does allow shared libraries to be used, but by default all Go code is linked statically (though libraries written in other languages may be dynamically linked by default, if you import a module that requires it).

      This is the same for Rust.

      • JoYo
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        8 minutes ago

        go has an abi, if unstable between versions.

        https://pkg.go.dev/internal/abi

        rust foundation has stated that they will never have an abi.

        I think both and zig support C ABI. Because Rust has no internal abi, projects will always be structurally incompatible with dynamic loading. that leaves rust with FFI.