Visual Studio CMake project and runtime qt dll not found


I’m trying to move my .sln Qt project to CMake using Visual Studio. Through the wizard I can get the three files CMakeLists.txt, CMakePresets.json and CMakeUserPresets.json allowing me to correctly compile my executable. However when I try to execute it thorugh Visual Studio and error informing me that runtime Qt dll libraries can not be found. Currently the only solution I found to allow execution from VS is to change the cmake files as follow:

CMakePresets.json

{
  "version": 3,
  "configurePresets": [
    {
      "name": "windows-path",
      "hidden": true,
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      },
      "environment": {
        "PATH": "$env{QTDIR}/bin;$penv{PATH}"
      }
    },
    {
      "hidden": true,
      "name": "Qt",
      "cacheVariables": {
        "CMAKE_PREFIX_PATH": "$env{QTDIR}"
      },
      "vendor": {
        "qt-project.org/Qt": {
          "checksum": "wVa86FgEkvdCTVp1/nxvrkaemJc="
        }
      }
    }
  ],
  "vendor": {
    "qt-project.org/Presets": {
      "checksum": "67SmY24ZeVbebyKD0fGfIzb/bGI="
    }
  }
}

CMakeUserPresets.json

{
  "version": 3,
  "configurePresets": [
    {
      "name": "Debug-x64",
      "displayName": "Debug (x64)",
      "binaryDir": "${sourceDir}/out/build",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug",
        "CMAKE_CXX_FLAGS": "-DQT_QML_DEBUG"
      },
      "environment": {
        "QML_DEBUG_ARGS": "-qmljsdebugger=file:{1562a4bd-a37e-46ae-a6b2-412e8b63f4ae},block"
      },
      "inherits": [
        "windows-path",
        "Qt-Default"
      ]
    },
    {
      "name": "Release-x64",
      "displayName": "Release (x64)",
      "binaryDir": "${sourceDir}/out/build",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Release"
      },
      "inherits": [
        "windows-path",
        "Qt-Default"
      ]
    },
    {
      "hidden": true,
      "name": "Qt-Default",
      "inherits": "msvc2019_64_6",
      "vendor": {
        "qt-project.org/Default": {
          "checksum": "uI+euPB4oZt5osJHE33ppAlKduk="
        }
      }
    },
    {
      "hidden": true,
      "name": "msvc2019_64_6",
      "inherits": "Qt",
      "environment": {
        "QTDIR": "C:/Qt/6.6.3/msvc2019_64"
      },
      "architecture": {
        "strategy": "external",
        "value": "x64"
      },
      "generator": "Ninja",
      "vendor": {
        "qt-project.org/Version": {
          "checksum": "b5VAFXnHVFa8S4l3Lxo0igVIEqU="
        }
      }
    }
  ],
  "vendor": {
    "qt-project.org/Presets": {
      "checksum": "TsEVjuQh+PC836fCIPbPoI7HdtU="
    }
  }
}

This solution work but is very annoying cause the file CMakeUserPresets.json shuold not be committed since it contains the local system paths and every time something change (Qt version, compilation PC ad so on) it’s required to re-add the “windows-path” label on inherits section. Is there a different solution to get the same without change the CMakeUserPresets.json file every time? I tired to change the CMakePresets.json as follow:

{
  "version": 3,
  "configurePresets": [
    {
      "name": "windows-path",
      "hidden": true,
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      },
      "environment": {
        "PATH": "$env{QTDIR}/bin;$penv{PATH}"
      }
    },
    {
      "hidden": true,
      "name": "Qt",
      "inherits": "windows-path",
      "cacheVariables": {
        "CMAKE_PREFIX_PATH": "$env{QTDIR}"
      },
      "vendor": {
        "qt-project.org/Qt": {
          "checksum": "wVa86FgEkvdCTVp1/nxvrkaemJc="
        }
      }
    }
  ],
  "vendor": {
    "qt-project.org/Presets": {
      "checksum": "Uhv9qJEvGySPn/GlY7zJ50jQal0="
    }
  }
}

but the Qt VS Tools plugin detect the change (the addtion of “inherits”: “windows-path”,) and reset the code to original one (which is another very annoying behavior)…
Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *