50

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 main function.

My CMakeLists.txt is exactly:

cmake_minimum_required(VERSION 2.8.9)

project(testproject)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets)

# Tell CMake to create the helloworld executable
add_executable(helloworld hello.cpp)

# Use the Widgets module from Qt 5.
qt5_use_modules(helloworld Widgets)

When in MSysGit bash I enter

$ cmake -G"Visual Studio 11"

I get this output:

$ cmake -G"Visual Studio 11"
-- The C compiler identification is MSVC 17.0.60204.1
-- The CXX compiler identification is MSVC 17.0.60204.1
-- Check for working C compiler using: Visual Studio 11
-- Check for working C compiler using: Visual Studio 11 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 11
-- Check for working CXX compiler using: Visual Studio 11 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Warning at CMakeLists.txt:11 (find_package):
  By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project
  has asked CMake to find a package configuration file provided by
  "Qt5Widgets", but CMake did not find one.

  Could not find a package configuration file provided by "Qt5Widgets" with
  any of the following names:

    Qt5WidgetsConfig.cmake
    qt5widgets-config.cmake

  Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or set
  "Qt5Widgets_DIR" to a directory containing one of the above files.  If
  "Qt5Widgets" provides a separate development package or SDK, be sure it has
  been installed.


CMake Error at CMakeLists.txt:17 (qt5_use_modules):
  Unknown CMake command "qt5_use_modules".


-- Configuring incomplete, errors occurred!

Do you have any ideas?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
dzada
  • 5,344
  • 5
  • 29
  • 37

6 Answers6

44

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
dzada
  • 5,344
  • 5
  • 29
  • 37
  • I had the same problem. I was using cmake 2.8.9, and trying to do the same thing with CMAKE_MODULE_PATH. It was ignoring me. Using CMAKE_PREFIX_PATH, it worked. Probably, something changed on this version of cmake... Thanks! – Jorge Arévalo Apr 04 '14 at 14:57
  • 16
    Don't require your users to change the CMakeLists like that. Set the environment variable instead, as the Qt documentation says to do. – steveire Sep 22 '14 at 08:13
  • 3
    @steveire I agree but could you point to the QT documentation where this is explained? – thomas Nov 18 '14 at 08:35
  • 2
    Adding WHERE? which file? which line? – Petr Mar 19 '15 at 16:31
  • I second that. Adding where? The answer is hard to follow because of this missing information. – Suma Jul 30 '15 at 10:55
  • I added it to my CMakeLists.txt. Fourth line, but I don't think that matters. ;-) – itmuckel Apr 20 '16 at 17:10
  • 1
    I believe what @steveire means is to `set CMAKE_PREFIX_PATH=...` (windows) or `export CMAKE_PREFIX_PATH=...` (UNIX-like) before running cmake/cmake-gui. On Windows, you can make a batch file which sets this path and then launches cmake-gui. – lethal-guitar Aug 07 '16 at 12:58
  • Although environment variables work, the usual way is to use the CMake command line: `cmake -DCMAKE_PREFIX_PATH=...`. It can also be done through the CMake GUI. – Thomas Mar 19 '20 at 10:01
21

You should set the CMAKE_PREFIX_PATH environment variable instead or use the cmake-gui to set the path to the Qt 5 packages.

steveire
  • 10,694
  • 1
  • 37
  • 48
  • 1
    I would also advise people to look at dzada's answer for an example of the path to find the Qt prefix needed by CMake. – Amir Eldor Feb 20 '16 at 12:20
8

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

takahawk
  • 113
  • 2
  • 5
2

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)
tresf
  • 7,103
  • 6
  • 40
  • 101
0

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 ()
Dmitry Mikushin
  • 1,478
  • 15
  • 16
-1

One way is to open the CMakeLists.txt in Qt Creator. Qt Creator supports CMake natively and it always knows where Qt is.

user2061057
  • 962
  • 1
  • 9
  • 20