【问题标题】:How to find the Qt5 CMake module on Windows如何在 Windows 上找到 Qt5 CMake 模块
【发布时间】:2013-03-16 09:48:11
【问题描述】:

我正在尝试在 Windows 上使用 CMake 制作一个非常基本的 Qt5 应用程序。 我使用了文档of Qt5 to use 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!

你有什么想法吗?

【问题讨论】:

    标签: c++ windows visual-c++ cmake qt5


    【解决方案1】:

    行后

    cmake_minimum_required(VERSION 2.8.9)
    
    project(testproject)
    

    添加

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

    这样就解决了问题。

    【讨论】:

    • 我遇到了同样的问题。我正在使用 cmake 2.8.9,并尝试使用 CMAKE_MODULE_PATH 做同样的事情。它无视我。使用 CMAKE_PREFIX_PATH,它起作用了。可能,这个版本的 cmake 发生了一些变化......谢谢!
    • 不要要求您的用户像那样更改 CMakeLists。按照 Qt 文档的说明设置环境变量。
    • @steveire 我同意,但你能指出解释这一点的 QT 文档吗?
    • 在哪里添加?哪个文件?哪一行?
    • 我相信@steveire 的意思是set CMAKE_PREFIX_PATH=... (windows) 或export CMAKE_PREFIX_PATH=... (UNIX-like) 在运行cmake/cmake-gui 之前。在 Windows 上,您可以创建一个批处理文件来设置此路径,然后启动 cmake-gui。
    【解决方案2】:

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

    【讨论】:

    • 我还建议人们查看 dzada 的答案,以查找 CMake 所需的 Qt 前缀的路径示例。
    【解决方案3】:

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

    【讨论】:

    • 人们应该听这个家伙的。
    【解决方案4】:

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

    它尝试通过对内部的各种“5.x”文件夹名称(例如C:\Qt\)进行反向排序来使用最高可用的 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)
    

    【讨论】:

      【解决方案5】:

      @tresf's solution 完美地涵盖了整个想法。只需要补充一件事:微软的版本控制似乎正在变成几何级数。该系列太短尚未确认,因此从 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 ()
      

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 这根本不是真的。
        • 您能解释一下原因吗?即使 Qt 甚至不在 PATH 中,我也可以通过 Qt Creator 进行配置。相反,CMake GUI 对 Qt 毫无头绪。
        猜你喜欢
        • 2020-04-10
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        • 2021-01-16
        • 1970-01-01
        相关资源
        最近更新 更多