Home > Blockchain >  cmake disabling MSVC increamental linking
cmake disabling MSVC increamental linking

Time:08-20

Background

I have some project which after a while (many builds when developing) consumes so many computer resources during linking process. So much that my machine becomes unresponsive (even mouse do not move).

My project has many static libraries (4) targets and many executable (2 are production excusable and 4 for testing purposes).

Quick googling indicated that problem is incremental linking. I've found this as solution for cmake. And applied fix from answer:

cmake -A x64 -Thost=x64 -DCMAKE_BUILD_TYPE=Debug -G "Visual Studio 16 2019" .. ^
    -DCMAKE_EXE_LINKER_FLAGS_DEBUG="/DEBUG /INCREMENTAL:NO" ^
    -DCMAKE_EXE_LINKER_FLAGS_RELEASE="/DEBUG /INCREMENTAL:NO" ^
    -DCMAKE_STATIC_LINKER_FLAGS_DEBUG="/DEBUG /INCREMENTAL:NO" ^
    -DCMAKE_STATIC_LINKER_FLAGS_RELEASE="/DEBUG /INCREMENTAL:NO" ^
    -DCMAKE_SHARED_LINKER_FLAGS_DEBUG="/DEBUG /INCREMENTAL:NO" ^
    -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="/DEBUG /INCREMENTAL:NO" ^
    -DCMAKE_MODULE_LINKER_FLAGS_DEBUG="/DEBUG /INCREMENTAL:NO" ^
    -DCMAKE_MODULE_LINKER_FLAGS_RELEASE="/DEBUG /INCREMENTAL:NO"

Problem

So far my machine do not show any signs of hangs, but in build logs I see this:

  Generating Code...
LINK : warning LNK4044: unrecognized option '/DEBUG'; ignored [F:\repos\Src\build64\Project\Project.vcxproj]
LINK : warning LNK4044: unrecognized option '/INCREMENTAL:NO'; ignored [F:\repos\Src\build64\Project\Project.vcxproj]

Based on MSVC linker documentation this should be fine!

Question

How it is possible I have this logs? Did I disabled incremental linking or not (just data do not grow enough to cause a problem)? If I didn't how I can fix this problem?

CodePudding user response:

For the static linker flags you do not need to add those options. In case of MSVC lib.exe is used as static linker and that one does not understand these options.

For most projects of mine I use inside the CMakeLists.txt the add_link_options command to let CMake properly add the link options to the linker commands.

add_link_options(
    /LARGEADDRESSAWARE
    /DEBUG
    $<$<NOT:$<CONFIG:DEBUG>>:/INCREMENTAL:NO> # Disable incremental linking.
    $<$<NOT:$<CONFIG:DEBUG>>:/OPT:REF> # Remove unreferenced functions and data.
    $<$<NOT:$<CONFIG:DEBUG>>:/OPT:ICF> # Identical COMDAT folding.
    $<$<CONFIG:DEBUG>:/INCREMENTAL> # Do incremental linking.
    $<$<CONFIG:DEBUG>:/OPT:NOREF> # No unreferenced data elimination.
    $<$<CONFIG:DEBUG>:/OPT:NOICF> # No Identical COMDAT folding.
)
  • Related