CMake cannot find packages within Qt6 installation

Viewed 3189

I am in the process of trying to build a project through CMake and I set environmental variable Qt6_DIR to the directory containing Qt6Config.cmake. However the build doesn't seem to be able to find any of the other dependencies it needs within the Qt6 installation & I'm not sure what needs to be done next. I see references to other possible environmental variables like "Qt6CoreTools_DIR" and I'm wondering if I really need to set each of these independently or if there's a toolchain containing this information that needs to be properly linked to.

-- GUI Frontend: Qt6
CMake Warning at C:/Program Files/CMake/share/cmake-3.20/Modules/CMakeFindDependencyMacro.cmake:47 (find_package):
  By not providing "FindQt6CoreTools.cmake" in CMAKE_MODULE_PATH this project
  has asked CMake to find a package configuration file provided by
  "Qt6CoreTools", but CMake did not find one.

  Could not find a package configuration file provided by "Qt6CoreTools"
  (requested version 6.2.3) with any of the following names:

    Qt6CoreToolsConfig.cmake
    qt6coretools-config.cmake

  Add the installation prefix of "Qt6CoreTools" to CMAKE_PREFIX_PATH or set
  "Qt6CoreTools_DIR" to a directory containing one of the above files.  If
  "Qt6CoreTools" provides a separate development package or SDK, be sure it
  has been installed.
Call Stack (most recent call first):
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6WidgetsTools/Qt6WidgetsToolsDependencies.cmake:9 (find_dependency)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6WidgetsTools/Qt6WidgetsToolsConfig.cmake:34 (include)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6Widgets/Qt6WidgetsDependencies.cmake:71 (find_package)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6Widgets/Qt6WidgetsConfig.cmake:40 (include)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake:177 (find_package)
  src/CMakeLists.txt:22 (find_package)


CMake Warning at C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6Widgets/Qt6WidgetsDependencies.cmake:71 (find_package):
  Found package configuration file:

    C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6WidgetsTools/Qt6WidgetsToolsConfig.cmake

  but it set Qt6WidgetsTools_FOUND to FALSE so package "Qt6WidgetsTools" is
  considered to be NOT FOUND.  Reason given by package:

  Qt6WidgetsTools could not be found because dependency Qt6CoreTools could
  not be found.

Call Stack (most recent call first):
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6Widgets/Qt6WidgetsConfig.cmake:40 (include)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake:177 (find_package)
  src/CMakeLists.txt:22 (find_package)


CMake Warning at C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake:177 (find_package):
  Found package configuration file:

    C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6Widgets/Qt6WidgetsConfig.cmake

  but it set Qt6Widgets_FOUND to FALSE so package "Qt6Widgets" is considered
  to be NOT FOUND.  Reason given by package:

  Target "Qt6::Widgets" was not found.

Call Stack (most recent call first):
  src/CMakeLists.txt:22 (find_package)


-- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR)
-- Could NOT find Qt6CoreTools (missing: Qt6CoreTools_DIR)
CMake Warning at C:/Program Files/CMake/share/cmake-3.20/Modules/CMakeFindDependencyMacro.cmake:47 (find_package):
  Found package configuration file:

    C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6Core/Qt6CoreConfig.cmake

  but it set Qt6Core_FOUND to FALSE so package "Qt6Core" is considered to be
  NOT FOUND.  Reason given by package:

  Target "Qt6::Core" was not found.

Call Stack (most recent call first):
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/QtPublicDependencyHelpers.cmake:14 (find_dependency)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGL/Qt6OpenGLDependencies.cmake:91 (_qt_internal_find_dependencies)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGL/Qt6OpenGLConfig.cmake:40 (include)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake:177 (find_package)
  src/CMakeLists.txt:22 (find_package)


CMake Warning at C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake:177 (find_package):
  Found package configuration file:

    C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGL/Qt6OpenGLConfig.cmake

  but it set Qt6OpenGL_FOUND to FALSE so package "Qt6OpenGL" is considered to
  be NOT FOUND.  Reason given by package:

  Qt6OpenGL could not be found because dependency Qt6Core could not be found.

Call Stack (most recent call first):
  src/CMakeLists.txt:22 (find_package)


-- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR)
-- Could NOT find Qt6CoreTools (missing: Qt6CoreTools_DIR)
CMake Warning at C:/Program Files/CMake/share/cmake-3.20/Modules/CMakeFindDependencyMacro.cmake:47 (find_package):
  Found package configuration file:

    C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6Core/Qt6CoreConfig.cmake

  but it set Qt6Core_FOUND to FALSE so package "Qt6Core" is considered to be
  NOT FOUND.  Reason given by package:

  Target "Qt6::Core" was not found.

Call Stack (most recent call first):
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/QtPublicDependencyHelpers.cmake:14 (find_dependency)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGL/Qt6OpenGLDependencies.cmake:91 (_qt_internal_find_dependencies)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGL/Qt6OpenGLConfig.cmake:40 (include)
  C:/Program Files/CMake/share/cmake-3.20/Modules/CMakeFindDependencyMacro.cmake:47 (find_package)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/QtPublicDependencyHelpers.cmake:14 (find_dependency)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGLWidgets/Qt6OpenGLWidgetsDependencies.cmake:91 (_qt_internal_find_dependencies)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGLWidgets/Qt6OpenGLWidgetsConfig.cmake:40 (include)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake:177 (find_package)
  src/CMakeLists.txt:22 (find_package)


CMake Warning at C:/Program Files/CMake/share/cmake-3.20/Modules/CMakeFindDependencyMacro.cmake:47 (find_package):
  Found package configuration file:

    C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGL/Qt6OpenGLConfig.cmake

  but it set Qt6OpenGL_FOUND to FALSE so package "Qt6OpenGL" is considered to
  be NOT FOUND.  Reason given by package:

  Qt6OpenGL could not be found because dependency Qt6Core could not be found.

Call Stack (most recent call first):
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/QtPublicDependencyHelpers.cmake:14 (find_dependency)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGLWidgets/Qt6OpenGLWidgetsDependencies.cmake:91 (_qt_internal_find_dependencies)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGLWidgets/Qt6OpenGLWidgetsConfig.cmake:40 (include)
  C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake:177 (find_package)
  src/CMakeLists.txt:22 (find_package)


CMake Warning at C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake:177 (find_package):
  Found package configuration file:

    C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGLWidgets/Qt6OpenGLWidgetsConfig.cmake

  but it set Qt6OpenGLWidgets_FOUND to FALSE so package "Qt6OpenGLWidgets" is
  considered to be NOT FOUND.  Reason given by package:

  Qt6OpenGLWidgets could not be found because dependency Qt6OpenGL could not
  be found.

Call Stack (most recent call first):
  src/CMakeLists.txt:22 (find_package)


CMake Error at src/CMakeLists.txt:22 (find_package):
  Found package configuration file:

    C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6/Qt6Config.cmake

  but it set Qt6_FOUND to FALSE so package "Qt6" is considered to be NOT
  FOUND.  Reason given by package:

  Failed to find Qt component "Widgets".

  Expected Config file at
  "C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6Widgets/Qt6WidgetsConfig.cmake" exists

  Failed to find Qt component "OpenGL".

  Expected Config file at
  "C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGL/Qt6OpenGLConfig.cmake" exists

  Failed to find Qt component "OpenGLWidgets".

  Expected Config file at
  "C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6OpenGLWidgets/Qt6OpenGLWidgetsConfig.cmake"
  exists
4 Answers

Add this to your root cmake.

list(APPEND CMAKE_PREFIX_PATH "~/Qt/6.3.0/gcc_64")

Change the path as needed.

According to QTBUG-97615 the proper support of configuring the build with single Qt6_DIR isn't supported (yet?), and the 'official' way to configure your application build is to use CMAKE_PREFIX_PATH (as suggested in other answers).
However if your tools don't support such appoach easily (like VisualStudio/VSCode), there is a workaround for such case (I've discovered it while digging the messy cmake-scripts of Qt6).
You can use QT_ADDITIONAL_PACKAGES_PREFIX_PATH to specify the Qt root (along with Qt6_DIR to hook it initially).
And the command line will look as follows (just to illustrate the approach):
cmake -DQt6_DIR:PATH=C:/Qt6/6.2.3/mingw_64/lib/cmake/Qt6 -DQT_ADDITIONAL_PACKAGES_PREFIX_PATH=C:/Qt6/6.2.3/mingw_64 ..

Both these variables are easily configurable through UI in VisualStudio/VSCode.
Happy building! :)

From Qt Documentation:

There are different ways you can tell CMake about Qt, but the most common and recommended approach is to set the CMake cache variable CMAKE_PREFIX_PATH to include the Qt 6 installation prefix.

So you shoud add C:/Qt6/6.2.3/mingw_64/ to the CMAKE_PREFIX_PATH semicolon-separated list.

You could check whether CMAKE_PREFIX_PATH is set or modified by one of the cmake scripts supplied by the project.

I had the same problem with a different project on Linux OS and what seemed to work in the end was setting a project specific variable named QT_PREFIX to /opt/Qt/6.2.4/gcc_64/.

By looking at the project's file CMakeLists_Dependencies.cmake I noticed that CMAKE_PREFIX_PATH was being set by the script like this: set(CMAKE_PREFIX_PATH "${QT_PREFIX}/lib/cmake"). My guess is, that this statement shadowed my definition of CMAKE_PREFIX_PATH from the command line.

I checked it by inserting the statement message(CMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}") right below set(CMAKE_PREFIX_PATH "${QT_PREFIX}/lib/cmake").

When not setting QT_PREFIX, the output was CMAKE_PREFIX_PATH="/lib/cmake", no matter whether I included -DCMAKE_PREFIX_PATH=… on the command line or not. (/lib/cmake/ was not a valid path in my case.)

If QT_DIR was set as described above, the output was CMAKE_PREFIX_PATH="/opt/Qt/6.2.4/gcc_64//lib/cmake", which enabled cmake to find the directory, even despite the double slash.

Related