diff --git a/build.zig b/build.zig index 15762f0ae881..b7e991b01068 100644 --- a/build.zig +++ b/build.zig @@ -782,7 +782,17 @@ fn addCmakeCfgOptionsToExe( mod.linkSystemLibrary("unwind", .{}); }, .ios, .macos, .watchos, .tvos, .visionos => { - mod.link_libcpp = true; + if (static or !std.zig.system.darwin.isSdkInstalled(b.allocator)) { + mod.link_libcpp = true; + } else { + // Avoid using `mod.linkSystemLibrary()`, which: + // - is semantically equivalent to `-lc++` + // - and enables `mod.link_libcpp` + // Instead, add the full object pathname. + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.host.result) orelse return error.SdkDetectFailed; + const libcxx = b.pathJoin(&.{ sdk, "usr/lib/libc++.tbd" }); + exe.root_module.addObjectFile(.{ .cwd_relative = libcxx }); + } }, .windows => { if (target.abi != .msvc) mod.link_libcpp = true;