From cee59f14f9b624746cb9f7adb0bd209ff895356c Mon Sep 17 00:00:00 2001 From: JacobCrabill Date: Sat, 27 Sep 2025 16:14:03 -0700 Subject: [PATCH] Compile: Don't add libs as sources to static libs When compiling static libraries, don't tell the linker to include other libraries (static OR dynamic) as sources to include in the archive. Should resolve https://github.com/ziglang/zig/issues/19341 --- lib/std/Build/Step/Compile.zig | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 0f47a0b64781..d59e1ccf936d 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -1222,11 +1222,19 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { break :prefix "-l"; }; switch (system_lib.use_pkg_config) { - .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })), + .no => { + if (compile.linkage != .static) { + // Avoid putting another compiled library inside a static library. + try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })); + } + }, .yes, .force => { if (compile.runPkgConfig(system_lib.name)) |result| { try zig_args.appendSlice(result.cflags); - try zig_args.appendSlice(result.libs); + if (compile.linkage != .static) { + // Avoid putting another compiled library inside a static library. + try zig_args.appendSlice(result.libs); + } try seen_system_libs.put(arena, system_lib.name, result.cflags); } else |err| switch (err) { error.PkgConfigInvalidOutput, @@ -1268,10 +1276,10 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { }, .lib => l: { const other_produces_implib = other.producesImplib(); - const other_is_static = other_produces_implib or other.isStaticLibrary(); + const other_is_compiled_lib = other_produces_implib or other.isStaticLibrary() or other.isDynamicLibrary(); - if (compile.isStaticLibrary() and other_is_static) { - // Avoid putting a static library inside a static library. + if (compile.isStaticLibrary() and other_is_compiled_lib) { + // Avoid putting another compiled library inside a static library. break :l; }