From bf45efb1ead40d758aeaf7842f44f04e1eab7eea Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 12 Oct 2024 22:17:30 +0300 Subject: [PATCH] use /link before each library linker option on MSVC (#24291) fixes #24087, refs https://forum.nim-lang.org/t/341, refs #14222, refs #14221 The Nim compiler calls `cl` for linking as well as compilation. This means that options to the linker have to be passed after a `/link` argument. But the Nim compiler doesn't include this option normally, because users may still want to pass non-linker options to `cl` at link time. To deal with this, a workaround is used: every single library link option adds `/link` before it. The linker simply ignores extraneous `/link` arguments and gives a warning instead, since it's an unrecognized option to the linker. This is really hacky but otherwise we need to separate linker arguments into arguments passed either to the compiler or to the linker at link time, and this behavior wouldn't be meaningful outside of MSVC. I can't really test this manually but I did test that the linker ignores `/link`. I also can't really do more than this, I don't really use MSVC so I wouldn't know how to navigate it, or how people use it. Ideally someone who knows more about/uses MSVC can give their input or take over. (cherry picked from commit 449106a5a46d11c306b0b85790e31350fe01be53) --- compiler/extccomp.nim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/extccomp.nim b/compiler/extccomp.nim index c68da65a97..82cf5afb91 100644 --- a/compiler/extccomp.nim +++ b/compiler/extccomp.nim @@ -162,7 +162,11 @@ compiler vcc: linkerExe: "cl", linkTmpl: "$builddll$vccplatform /Fe$exefile $objfiles $buildgui /nologo $options", includeCmd: " /I", - linkDirCmd: " /LIBPATH:", + # HACK: we call `cl` so we have to pass `/link` for linker options, + # but users may still want to pass arguments to `cl` (see #14221) + # to deal with this, we add `/link` before each `/LIBPATH`, + # the linker ignores extra `/link`s since it's an unrecognized argument + linkDirCmd: " /link /LIBPATH:", linkLibCmd: " $1.lib", debug: " /RTC1 /Z7 ", pic: "",