Skip to content

Commit cd03a0a

Browse files
committed
compiler: Don't link ucrtbased.dll when targeting *-windows-msvc in Debug mode.
Linking it by default means that we produce binaries that, effectively, only run on systems which have the Windows SDK installed because ucrtbased.dll is not redistributable, and the Windows SDK is what actually installs ucrtbased.dll into %SYSTEM32%. The resulting binaries also can't run under Wine because Wine does not provide ucrtbased.dll. It is also inconsistent with our behavior for *-windows-gnu where we always link ucrtbase.dll. See #23983, #24019, and #24053 for more details. So just use ucrtbase.dll regardless of mode. With this change, we can also drop the implicit definition of the _DEBUG macro in zig cc, which has in some cases been problematic for users. Users who want to opt into the old behavior can do so, both for *-windows-msvc and *-windows-gnu, by explicitly passing -lucrtbased and -D_DEBUG. We might consider adding a more ergonomic flag like -fdebug-crt to the zig build-* family of commands in the future. Closes #24052.
1 parent 826e1c3 commit cd03a0a

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/Compilation.zig

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5904,8 +5904,7 @@ fn updateWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, win32
59045904
// them being defined matches the behavior of how MSVC calls rc.exe which is the more
59055905
// relevant behavior in this case.
59065906
switch (rc_src.owner.optimize_mode) {
5907-
.Debug => try argv.append("-D_DEBUG"),
5908-
.ReleaseSafe => {},
5907+
.Debug, .ReleaseSafe => {},
59095908
.ReleaseFast, .ReleaseSmall => try argv.append("-DNDEBUG"),
59105909
}
59115910
try argv.appendSlice(rc_src.extra_flags);
@@ -6260,10 +6259,7 @@ pub fn addCCArgs(
62606259
// LLVM IR files don't support these flags.
62616260
if (ext != .ll and ext != .bc) {
62626261
switch (mod.optimize_mode) {
6263-
.Debug => {
6264-
// windows c runtime requires -D_DEBUG if using debug libraries
6265-
try argv.append("-D_DEBUG");
6266-
},
6262+
.Debug => {},
62676263
.ReleaseSafe => {
62686264
try argv.append("-D_FORTIFY_SOURCE=2");
62696265
},

src/link/Coff.zig

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,21 +2113,17 @@ fn linkWithLLD(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node:
21132113

21142114
try argv.append(try comp.crtFileAsString(arena, "libmingw32.lib"));
21152115
} else {
2116+
try argv.append(switch (comp.config.link_mode) {
2117+
.static => "libcmt.lib",
2118+
.dynamic => "msvcrt.lib",
2119+
});
2120+
21162121
const lib_str = switch (comp.config.link_mode) {
2117-
.dynamic => "",
21182122
.static => "lib",
2123+
.dynamic => "",
21192124
};
2120-
const d_str = switch (optimize_mode) {
2121-
.Debug => "d",
2122-
else => "",
2123-
};
2124-
switch (comp.config.link_mode) {
2125-
.static => try argv.append(try allocPrint(arena, "libcmt{s}.lib", .{d_str})),
2126-
.dynamic => try argv.append(try allocPrint(arena, "msvcrt{s}.lib", .{d_str})),
2127-
}
2128-
2129-
try argv.append(try allocPrint(arena, "{s}vcruntime{s}.lib", .{ lib_str, d_str }));
2130-
try argv.append(try allocPrint(arena, "{s}ucrt{s}.lib", .{ lib_str, d_str }));
2125+
try argv.append(try allocPrint(arena, "{s}vcruntime.lib", .{lib_str}));
2126+
try argv.append(try allocPrint(arena, "{s}ucrt.lib", .{lib_str}));
21312127

21322128
//Visual C++ 2015 Conformance Changes
21332129
//https://msdn.microsoft.com/en-us/library/bb531344.aspx

0 commit comments

Comments
 (0)