How to use find_package with Qt6 and modern CMake (Windows)
-
Hi,
I'm using Qt 6.3 and CMake 3.22. I've installed Qt from the official Qt installer (
MaintenanceTool.exe
).When I do
find_package(Qt6 REQUIRED COMPONENTS Core Widgets)
, I get the following error (sort of expectedly):1> [CMake] CMake Error at D:/vcpkg/scripts/buildsystems/vcpkg.cmake:816 (_find_package): 1> [CMake] By not providing "FindQt6.cmake" in CMAKE_MODULE_PATH this project has 1> [CMake] asked CMake to find a package configuration file provided by "Qt6", but 1> [CMake] CMake did not find one. 1> [CMake] 1> [CMake] Could not find a package configuration file provided by "Qt6" with any of 1> [CMake] the following names: 1> [CMake] 1> [CMake] Qt6Config.cmake 1> [CMake] qt6-config.cmake 1> [CMake] 1> [CMake] Add the installation prefix of "Qt6" to CMAKE_PREFIX_PATH or set "Qt6_DIR" 1> [CMake] to a directory containing one of the above files. If "Qt6" provides a 1> [CMake] separate development package or SDK, be sure it has been installed.
Now I don't want to mess with my
CMAKE_PREFIX_PATH
, so I follow the suggestion and do:set(Qt6_DIR "D:/Qt/6.3.0/msvc2019_64/lib/cmake/Qt6")
. I wasn't sure which path to exactly set it to, but from what I could gather, this should be the correct one. But it spits out the following lengthy error. Note that I am using vcpkg for other dependencies, but I don't have any Qt version installed in vcpkg.1> [CMake] -- Could NOT find Qt6CoreTools (missing: Qt6CoreTools_DIR) 1> [CMake] CMake Warning at D:/vcpkg/scripts/buildsystems/vcpkg.cmake:816 (_find_package): 1> [CMake] Found package configuration file: 1> [CMake] 1> [CMake] D:/Qt/6.3.0/msvc2019_64/lib/cmake/Qt6Core/Qt6CoreConfig.cmake 1> [CMake] 1> [CMake] but it set Qt6Core_FOUND to FALSE so package "Qt6Core" is considered to be 1> [CMake] NOT FOUND. Reason given by package: 1> [CMake] 1> [CMake] Target "Qt6::Core" was not found. 1> [CMake] 1> [CMake] Call Stack (most recent call first): 1> [CMake] D:/Qt/6.3.0/msvc2019_64/lib/cmake/Qt6/Qt6Config.cmake:213 (find_package) 1> [CMake] D:/vcpkg/scripts/buildsystems/vcpkg.cmake:816 (_find_package) 1> [CMake] CMakeLists.txt:16 (find_package) 1> [CMake] 1> [CMake] 1> [CMake] -- Could NOT find Qt6WidgetsTools (missing: Qt6WidgetsTools_DIR) 1> [CMake] CMake Warning at D:/vcpkg/scripts/buildsystems/vcpkg.cmake:816 (_find_package): 1> [CMake] Found package configuration file: 1> [CMake] 1> [CMake] D:/Qt/6.3.0/msvc2019_64/lib/cmake/Qt6Widgets/Qt6WidgetsConfig.cmake 1> [CMake] 1> [CMake] but it set Qt6Widgets_FOUND to FALSE so package "Qt6Widgets" is considered 1> [CMake] to be NOT FOUND. Reason given by package: 1> [CMake] 1> [CMake] Target "Qt6::Widgets" was not found. 1> [CMake] 1> [CMake] Call Stack (most recent call first): 1> [CMake] D:/Qt/6.3.0/msvc2019_64/lib/cmake/Qt6/Qt6Config.cmake:213 (find_package) 1> [CMake] D:/vcpkg/scripts/buildsystems/vcpkg.cmake:816 (_find_package) 1> [CMake] CMakeLists.txt:16 (find_package) 1> [CMake] 1> [CMake] 1> [CMake] CMake Error at D:/vcpkg/scripts/buildsystems/vcpkg.cmake:816 (_find_package): 1> [CMake] Found package configuration file: 1> [CMake] 1> [CMake] D:/Qt/6.3.0/msvc2019_64/lib/cmake/Qt6/Qt6Config.cmake 1> [CMake] 1> [CMake] but it set Qt6_FOUND to FALSE so package "Qt6" is considered to be NOT 1> [CMake] FOUND. Reason given by package: 1> [CMake] 1> [CMake] Failed to find Qt component "Core". 1> [CMake] 1> [CMake] Expected Config file at 1> [CMake] "D:/Qt/6.3.0/msvc2019_64/lib/cmake/Qt6Core/Qt6CoreConfig.cmake" exists 1> [CMake] 1> [CMake] Failed to find Qt component "Widgets". 1> [CMake] 1> [CMake] Expected Config file at 1> [CMake] "D:/Qt/6.3.0/msvc2019_64/lib/cmake/Qt6Widgets/Qt6WidgetsConfig.cmake" 1> [CMake] exists 1> [CMake] 1> [CMake] 1> [CMake] 1> [CMake] Call Stack (most recent call first): 1> [CMake] CMakeLists.txt:16 (find_package) 1> [CMake] -- Configuring incomplete, errors occurred!
If I do:
set(CMAKE_PREFIX_PATH "D:/Qt/6.3.0/msvc2019_64/")
, it seems to work though.Why does it not work with setting
Qt6_DIR
?
And am I doing this correctly in general, or is there another / a better way to make CMake able to find Qt6 - preferably, withfind_package
'sCONFIG
mode?Thanks a lot.
-
@Elador said in How to use find_package with Qt6 and modern CMake (Windows):
find_package(Qt6 REQUIRED COMPONENTS Core Widgets)
Are you sure you made the changes like "target_link_libraries" described in the document?
-
I haven't tried this with Qt6, but for Qt5 what seemed to always work for me was to add Qt's bin directory to my PATH before running cmake (and then don't have to set Qt6_DIR). After adding it to your path, check that you can run qmake without needing to specify its full path. Also, make sure you completely delete your build folder so there are no cached CMake-generated files (this could be why it's not working for you even after setting Qt6_DIR).
-
@Elador said in How to use find_package with Qt6 and modern CMake (Windows):
Why does it not work with setting Qt6_DIR?
This is a very late reply. Anyhow, for others who run into this, this is a known issue with Qt 6: https://bugreports.qt.io/browse/QTBUG-97615 .
The fix for now is to either
CMAKE_PREFIX_PATH
(either as environment variable, or as CMake cache variable). Or not only setQt6_DIR
, but alsoQt6Core_DIR
,Qt6Gui_DIR
,Qt6Widgets_DIR
...