I\'m trying to make a very basic Qt5 application using CMake on Windows.
I used the documentation of Qt5 to use CMake, and my main.cpp
file just contains a
Here's a technique that takes advantage of cmake's ability to read the registry to coerce a registry value into locating the matching msvc's Qt5Config.cmake
.
It attempts to use the highest available Qt5 version by doing a reverse sort on the various "5.x" folder names inside (e.g. C:\Qt\
).
This could be placed inside a module as well, e.g. QtLocator.cmake
.
SET(QT_MISSING True)
# msvc only; mingw will need different logic
IF(MSVC)
# look for user-registry pointing to qtcreator
GET_FILENAME_COMPONENT(QT_BIN [HKEY_CURRENT_USER\\Software\\Classes\\Applications\\QtProject.QtCreator.cpp\\shell\\Open\\Command] PATH)
# get root path so we can search for 5.3, 5.4, 5.5, etc
STRING(REPLACE "/Tools" ";" QT_BIN "${QT_BIN}")
LIST(GET QT_BIN 0 QT_BIN)
FILE(GLOB QT_VERSIONS "${QT_BIN}/5.*")
LIST(SORT QT_VERSIONS)
# assume the latest version will be last alphabetically
LIST(REVERSE QT_VERSIONS)
LIST(GET QT_VERSIONS 0 QT_VERSION)
# fix any double slashes which seem to be common
STRING(REPLACE "//" "/" QT_VERSION "${QT_VERSION}")
# do some math trickery to guess folder
# - qt uses (e.g.) "msvc2012"
# - cmake uses (e.g.) "1800"
# - see also https://cmake.org/cmake/help/v3.0/variable/MSVC_VERSION.html
MATH(EXPR QT_MSVC "2000 + (${MSVC_VERSION} - 600) / 100")
# check for 64-bit os
# may need to be removed for older compilers as it wasn't always offered
IF(CMAKE_SYSTEM_PROCESSOR MATCHES 64)
SET(QT_MSVC "${QT_MSVC}_64")
ENDIF()
SET(QT_PATH "${QT_VERSION}/msvc${QT_MSVC}")
SET(QT_MISSING False)
ENDIF()
# use Qt_DIR approach so you can find Qt after cmake has been invoked
IF(NOT QT_MISSING)
MESSAGE("-- Qt found: ${QT_PATH}")
SET(Qt5_DIR "${QT_PATH}/lib/cmake/Qt5/")
SET(Qt5Test_DIR "${QT_PATH}/lib/cmake/Qt5Test")
ENDIF()
And then..
# finally, use Qt5 + COMPONENTS technique, compatible with Qt_DIR
FIND_PACKAGE(Qt5 COMPONENTS Core Gui Widgets Xml REQUIRED)
You should set the CMAKE_PREFIX_PATH environment variable instead or use the cmake-gui to set the path to the Qt 5 packages.
The @tresf's solution perfectly covers the whole idea. It's only one thing to add: Microsoft's versioning seems to be turning into geometric progression. The series is too short yet to confirm, so as of 2019' the following formula may be used:
# do some math trickery to guess folder
# - qt uses (e.g.) "msvc2012"
# - cmake uses (e.g.) "1800"
# - see also https://cmake.org/cmake/help/v3.0/variable/MSVC_VERSION.html
# - see also https://dev.to/yumetodo/list-of-mscver-and-mscfullver-8nd
if ((MSVC_VERSION GREATER_EQUAL "1920") AND (IS_DIRECTORY "${QT_VERSION}/msvc2019"))
set(QT_MSVC "2019")
elseif ((MSVC_VERSION GREATER_EQUAL "1910") AND (IS_DIRECTORY "${QT_VERSION}/msvc2017"))
set(QT_MSVC "2017")
elseif (MSVC_VERSION GREATER_EQUAL "1900")
set(QT_MSVC "2015")
else ()
MATH(EXPR QT_MSVC "2000 + (${MSVC_VERSION} - 500) / 100")
endif ()
After the lines
cmake_minimum_required(VERSION 2.8.9)
project(testproject)
add
set (CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.0.1\\5.0.1\\msvc2010\\")
This solves the problem.
You need just add Qt path to Windows %PATH% variable. As suggested in official documentation: http://doc.qt.io/qt-4.8/install-win.html#step-3-set-the-environment-variables
One way is to open the CMakeLists.txt in Qt Creator. Qt Creator supports CMake natively and it always knows where Qt is.