[CMake] How to turn off incremental linking

I recently had to build a static library using CMake. In the current CMake version, apparently something has changed in the linker settings.

The build always ended with an error:

LINK : fatal error LNK1123: failure during conversion to COFF: file invalid
or corrupt [D:\0.GIT\libarchive-3.1.2\vc10.32\CMakeFiles\CMakeTmp\cmTC_bbd0c.vcxproj]

Looking at the linker settings, I found that CMake seems to enable incremental linking by default and this is the root cause for the trouble; at least with Visual Studio 2010

Link:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:QUEUE
/OUT:”D:\0.GIT\libarchive-3.1.2\vc10.32\CMakeFiles\CMakeTmp\Debug\cmTC_bbd0c.exe”
/INCREMENTAL
/NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib
/MANIFEST /ManifestFile:”cmTC_bbd0c.dir\Debug\cmTC_bbd0c.exe.intermediate.manifest”
/MANIFESTUAC:”level=’asInvoker’ uiAccess=’false'”
/DEBUG /PDB:”D:/0.GIT/libarchive-3.1.2/vc10.32/CMakeFiles/CMakeTmp/Debug/cmTC_bbd0c.pdb”
/SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT
/IMPLIB:”D:/0.GIT/libarchive-3.1.2/vc10.32/CMakeFiles/CMakeTmp/Debug/cmTC_bbd0c.lib”
/MACHINE:X86 cmTC_bbd0c.dir\Debug\cmTC_bbd0c.exe.embed.manifest.res
cmTC_bbd0c.dir\Debug\testCCompiler.obj /machine:X86 /debug

I searched the web for a solution, but could not find anything that really works. So here is what I did to fix the issue.

In in [CMakePrgmDir]\share\cmake-3.3\Modules\Platform\windows.cmake

modify the line 242 ( might be different in other versions )


# add /debug and /INCREMENTAL:YES to DEBUG and RELWITHDEBINFO also add pdbtype
# on versions that support it
set( MSVC_INCREMENTAL_YES_FLAG "")
if(NOT WINDOWS_PHONE AND NOT WINDOWS_STORE)
if(NOT MSVC_INCREMENTAL_DEFAULT)
set( MSVC_INCREMENTAL_YES_FLAG "/INCREMENTAL:YES")
else()
#set( MSVC_INCREMENTAL_YES_FLAG "/INCREMENTAL" ) ' comment out here
set( MSVC_INCREMENTAL_YES_FLAG "/INCREMENTAL:NO" )
endif()
endif()

With this modification in place, I was now able to let CMake create the project configuration files.