47

我正在尝试在 Windows 上使用 CMake 制作一个非常基本的 Qt5 应用程序。我使用Qt5 的文档来使用 CMake,我的main.cpp文件只包含一个main函数。

CMakeLists.txt的正是:

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)

在 MSysGit bash 中我输入

$ cmake -G"Visual Studio 11"

我得到这个输出:

$ 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!

你有什么想法?

4

6 回答 6

42

行后

cmake_minimum_required(VERSION 2.8.9)

project(testproject)

添加

set (CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.0.1\\5.0.1\\msvc2010\\")

这解决了问题。

于 2013-03-27T15:46:35.583 回答
20

您应该设置 CMAKE_PREFIX_PATH 环境变量,或者使用 cmake-gui 设置 Qt 5 包的路径。

于 2014-04-09T18:56:43.220 回答
7

您只需将 Qt 路径添加到 Windows %PATH% 变量。正如官方文档中所建议的那样:http: //doc.qt.io/qt-4.8/install-win.html#step-3-set-the-environment-variables

于 2016-04-13T10:48:34.250 回答
2

这是一种利用 cmake 读取注册表的能力来强制注册表值定位匹配 msvc 的技术Qt5Config.cmake

C:\Qt\它试图通过对内部的各种“5.x”文件夹名称(例如)进行反向排序来使用最高可用的 Qt5 版本。

这也可以放在模块内,例如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()

接着..

# finally, use Qt5 + COMPONENTS technique, compatible with Qt_DIR
FIND_PACKAGE(Qt5 COMPONENTS Core Gui Widgets Xml REQUIRED)
于 2017-12-08T22:39:52.943 回答
0

@tresf的解决方案完美地涵盖了整个想法。只需要补充一件事:微软的版本控制似乎正在变成几何级数。该系列太短尚无法确认,因此从 2019 年开始可以使用以下公式:

# 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 ()
于 2019-08-08T18:56:06.593 回答
-1

一种方法是在 Qt Creator 中打开 CMakeLists.txt。Qt Creator 原生支持 CMake,它总是知道 Qt 在哪里。

于 2014-09-19T15:21:52.847 回答