11

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
Jigwally
  • 111
  • 1
  • 1
  • 4
  • If by the actual qt6 dir you mean C:/Qt6, I tried that and it tells me that Qt6_DIR needs to be a directory containing Qt6Config.cmake. – Jigwally Feb 11 '22 at 21:35
  • Refer to config mode search [procedure](https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-search-procedure). You need to either set cache variable `Qt_ROOT` or add qt dir to `CMAKE_PREFIX_PATH`. Former can be done in `CMakePresets.json` or in command line so that you don't touch `CMakeLists.txt` – Osyotr Feb 11 '22 at 22:58
  • Your problem is similar to [this one](https://stackoverflow.com/questions/68967408). Are you sure you have all the dependencies installed? – u-235 Feb 12 '22 at 00:47
  • Refer to [QTBUG-97615](https://bugreports.qt.io/browse/QTBUG-97615) - apparently they've dropped proper support of `Qt6_DIR` in Qt6 :( – AntonK Aug 19 '22 at 14:44
  • Can you tell us which project exactly you're trying to build? – Codebird Aug 22 '22 at 07:52

5 Answers5

7

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! :)

AntonK
  • 1,210
  • 1
  • 16
  • 22
  • 1
    I just confirmed on my side that a `find_library` kept failing while building using Qt 6.3.2 unless `QT_ADDITIONAL_PACKAGES_PREFIX_PATH` is set with the correct path. With 6.4.2, no need to specify `QT_ADDITIONAL_PACKAGES_PREFIX_PATH`, `find_library` uses the provided path... – david Feb 19 '23 at 17:50
5

Qt ≥ 6.4.2

run cmake with -DQt6_DIR=~/Qt/6.5.0/gcc_64/lib/cmake/Qt6


Qt < 6.4.2

Add this to your root CMakeLists.txt.

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

Change the path as needed.

Alternatively, you can set it as env var.

export PATH="~/Qt/6.3.0/gcc_64/:$PATH"

Search the standard system environment variables. This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is passed or by setting the CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH to FALSE. Path entries ending in /bin or /sbin are automatically converted to their parent directories:

  • PATH

Refs:

CMake find_package() search paths

Search paths specified in cmake-specific cache variables. These are intended to be used on the command line with a -DVAR=VALUE. The values are interpreted as semicolon-separated lists. This can be skipped if NO_CMAKE_PATH is passed or by setting the CMAKE_FIND_USE_CMAKE_PATH to FALSE:

  • CMAKE_PREFIX_PATH
  • CMAKE_FRAMEWORK_PATH
  • CMAKE_APPBUNDLE_PATH

CMAKE_PREFIX_PATH:

Path used for searching by FIND_XXX(), with appropriate suffixes added. Specifies a path which will be used by the FIND_XXX() commands. It contains the “base” directories, the FIND_XXX() commands append appropriate subdirectories to the base directories. So FIND_PROGRAM() adds /bin to each of the directories in the path, FIND_LIBRARY() appends /lib to each of the directories, and FIND_PATH() and FIND_FILE() append /include . By default it is empty, it is intended to be set by the project. See also CMAKE_SYSTEM_PREFIX_PATH, CMAKE_INCLUDE_PATH, CMAKE_LIBRARY_PATH, CMAKE_PROGRAM_PATH.

QT BUG [97615]

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
ניר
  • 1,204
  • 1
  • 8
  • 28
4

Since I see some people in the comments asking specifically for VS(-Code) settings/configuration, the following worked for me on Windows:

I set CMAKE_PREFIX_PATH to C:/Qt/6.4.2/msvc2019_64 (substitute C:/Qt/6.4.2 with your Qt installation path and version). Note: with Qt 5, you could use [..]/msvc2019_64/lib/cmake, this doesn't work for Qt 6 anymore. If you need multiple paths in CMAKE_PREFIX_PATH, join them with a semicolon (;).

An example .vscode/settings.json could look like this:

{
  "cmake.configureSettings": {
    "CMAKE_PREFIX_PATH": "C:/Qt/6.4.2/msvc2019_64"
  }
}
Nerix
  • 131
  • 1
  • 3
2

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.

  • well, the approach with `CMAKE_PREFIX_PATH` works if you invoke cmake via command line, but how do you pass it through VisualStudio / VSCode? Worth noting that with Qt5 it was possible to tell cmake, where the library was, with `Qt5_DIR`. Unfortunately same approach doesn't work with Qt6 any more – AntonK Aug 19 '22 at 13:53
0

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.

Codebird
  • 91
  • 8
  • `set(CMAKE_PREFIX_PATH "XYZ")` is a piece of bad code in any case, it should be re-written as `list(APPEND CMAKE_PREFIX_PATH "XYZ")` – AntonK Aug 19 '22 at 14:53
  • @AntonK: Can you elaborate why it should generally be considered 'bad code' or is your comment primarily opinion based? While I'd agree that it lead to some unexpected behaviour and inconclusive error messages in my case, the developers might have had a good reason to do it this way in their specific use case. Without knowing the constraints they were confronted with, we can't really judge if their code is 'bad' or 'good'. It might as well have been a deliberate decision of design, i.e. a matter of taste. So if your definition of 'bad code' goes beyond taste, please provide some evidence. :-) – Codebird Aug 22 '22 at 07:50
  • `CMAKE_PREFIX_PATH` is a semicolon-separated **list** of directories ... according to [the official documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html), and `set` just replaces the list with a single value. While such trick may work in some specific case, it will generally lead to hard-to-detect problems (e.g., when cross-compiling). – AntonK Aug 24 '22 at 11:15