Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How to use find_package with Qt6 and modern CMake (Windows)
Forum Updated to NodeBB v4.3 + New Features

How to use find_package with Qt6 and modern CMake (Windows)

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
5 Posts 4 Posters 21.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Elador
    wrote on last edited by
    #1

    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, with find_package's CONFIG mode?

    Thanks a lot.

    A.A.SEZENA 1 Reply Last reply
    1
    • E Elador

      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, with find_package's CONFIG mode?

      Thanks a lot.

      A.A.SEZENA Offline
      A.A.SEZENA Offline
      A.A.SEZEN
      wrote on last edited by
      #2

      @Elador said in How to use find_package with Qt6 and modern CMake (Windows):

      find_package(Qt6 REQUIRED COMPONENTS Core Widgets)

      Qt6 Doc qtcore

      Are you sure you made the changes like "target_link_libraries" described in the document?

      E 1 Reply Last reply
      0
      • A.A.SEZENA A.A.SEZEN

        @Elador said in How to use find_package with Qt6 and modern CMake (Windows):

        find_package(Qt6 REQUIRED COMPONENTS Core Widgets)

        Qt6 Doc qtcore

        Are you sure you made the changes like "target_link_libraries" described in the document?

        E Offline
        E Offline
        Elador
        wrote on last edited by
        #3

        @A-A-SEZEN Yes, target_link_libraries is not the issue. As you can see in my CMake output log, the call to find_package fails when I try and use Qt6_DIR. target_link_libraries happens much later.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on last edited by mchinand
          #4

          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).

          1 Reply Last reply
          0
          • kkoehneK Offline
            kkoehneK Offline
            kkoehne
            Moderators
            wrote on last edited by
            #5

            @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 set Qt6_DIR, but also Qt6Core_DIR, Qt6Gui_DIR, Qt6Widgets_DIR ...

            Director R&D, The Qt Company

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved