How to set qt5 path with cmake find_package on Windows?

Stepan Yakovenko picture Stepan Yakovenko · Jun 8, 2018 · Viewed 8.7k times · Source

I am adding QT like this:

find_package(Qt5 COMPONENTS Core Quick REQUIRED)
...
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick ${OpenCV_LIBS})

but cmake finds some python artifacts instead of expected C:\QT...

enter image description here

How can I change this? Tried to find some examples, but didn't find any clear instruction, which subfolder of C:\QT\ I should specify (any where).

Answer

Teivaz picture Teivaz · Jun 12, 2018

find_package search order is following:

  1. Search in cache variables: CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH
  2. Search in environment variables: <package>_DIR, CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH.
  3. Search in the HINTS option.
  4. Search the PATH environment variable.
  5. And in some more "desperate" places. More about that here.

With that in mind there are several ways to provide a proper version to QT:

  1. Have an environment variable pointing to the proper version of QT (e.g. QTDIR). And use it in the CMake files:
    • like set(CMAKE_PREFIX_PATH "$ENV{QTDIR}")
    • or find_package(Qt5 HINTS "$ENV{QTDIR}" COMPONENTS Core Quick REQUIRED)
  2. Have an environment variable explicitly named Qt5_DIR pointing to the proper version of QT. Then no additional changes to CMake files are required.
  3. Make sure that required version of Qt is the first one to be found in the PATH environment variable, for example, for windows C:\Qt\Qt5.10.1\5.10.1\msvc2017_64