Ninja: MSVC 'def' file gets repeated on the link line
If a MSVC '.def' file is listed as a source file of a target on Windows and the ninja generator is used, CMake generates a link link with the def file listed twice.
See simple test project: cmake-def-repeat-bug.zip, where for a MODULE target, CMake generates a link like with both /DEF:C:\Users\nick\code\cmake-def-repeat-bug\MyLib.def
and /DEF:MyLib.def
.
In a MSVC 19.3 x64 command prompt:
> cmake.exe --version
cmake version 3.23.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
> cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31107 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
usage: cl [ option... ] filename... [ /link linkoption... ]
> cd path\to\cmake-def-repeat-bug
> cmake.exe . -GNinja
-- The C compiler identification is MSVC 19.31.31107.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/nick/code/cmake-def-repeat-bug
> ninja.exe -v
[1/2] C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1431~1.311\bin\Hostx64\x64\cl.exe /nologo -DMyLib_EXPORTS /DWIN32 /D_WINDOWS /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\MyLib.dir\MyLib.c.obj /FdCMakeFiles\MyLib.dir\ /FS -cC:\Users\nick\code\cmake-def-repeat-bug\MyLib.c
[2/2] cmd.exe /C "cd . && C:\Users\nick\scoop\apps\cmake\3.23.2\bin\cmake.exe -E vs_link_dll --intdir=CMakeFiles\MyLib.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests -- C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1431~1.311\bin\Hostx64\x64\link.exe /nologo CMakeFiles\MyLib.dir\MyLib.c.obj /out:MyLib.dll /implib:MyLib.lib /pdb:MyLib.pdb /dll /version:0.0 /machine:x64 /debug /INCREMENTAL /DEF:C:\Users\nick\code\cmake-def-repeat-bug\MyLib.def /WX /DEF:MyLib.def kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
Edited by Brad King