Skip to content

Conversation

hamptokr
Copy link
Contributor

This PR adds support for individually controlling dependencies' link mode static|dynamic via build.zig as requested in #7

Examples

    const libssh2_dependency = b.dependency("libssh2", .{
        .target = target,
        .optimize = optimize,
        .@"crypto-backend" = .mbedtls,
        .@"mbedtls-linkage" = .static,
    });
    exe.linkLibrary(libssh2_dependency.artifact("ssh2"));

Will statically link mbedtls, ldd output:

# ldd zig-out/bin/example
linux-vdso.so.1 (0x00007f3fe1e81000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007f3fe1d42000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f3fe1a00000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f3fe1e83000)

Whereas dynamic linking:

    const libssh2_dependency = b.dependency("libssh2", .{
        .target = target,
        .optimize = optimize,
        .@"crypto-backend" = .mbedtls,
        .@"mbedtls-linkage" = .dynamic,
    });
    exe.linkLibrary(libssh2_dependency.artifact("ssh2"));

Will result in:

# ldd zig-out/bin/example
linux-vdso.so.1 (0x00007fdf6a024000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007fdf69ee5000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007fdf69c00000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007fdf6a026000)
libmbedcrypto.so.16 => /usr/lib/libmbedcrypto.so.16 (0x00007fdf69e54000)

On my machine statically linking (debug build) resulted in an 11mb binary. Dynamically linking reduced it to 8.3mb.

@hamptokr hamptokr merged commit d67e17d into main Sep 30, 2025
4 checks passed
@hamptokr hamptokr deleted the kah/controlled-dep-linkage branch September 30, 2025 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant