使用CMake测试支持的Qt版本

Nic*_*aus 3 qt cmake

我正在使用CMake构建一个Qt项目,它使用了一些较新的Qt功能,并且至少需要5.3版才能正确构建.我想对那些尝试构建项目的人感到高兴,并且在CMake配置时失败,并且逻辑错误告诉他们他们的CMake版本不够新,而不是一些深奥的构建错误.

我知道我将通过简单地使用模块find_package语法(即find_package(Qt5Widgets REQURIED))来获得至少5.0版本,但是如何确保我使用正确的次要版本并不是那么明显.最简单的方法是什么?

Pho*_*rga 6

我知道这是一个有点旧帖子,但您可以使用查看版本Qt5Widgets_VERSION.这是一些示例CMake代码:

find_package(Qt5Widgets REQUIRED)

if (Qt5Widgets_FOUND)
    if (Qt5Widgets_VERSION VERSION_LESS 5.7.0)
        message(FATAL_ERROR "Minimum supported Qt5 version is 5.70!")
    endif()
else()
    message(SEND_ERROR "The Qt5Widgets library could not be found!")
endif(Qt5Widgets_FOUND)
Run Code Online (Sandbox Code Playgroud)


小智 5

如今,可以将版本传递给find_package,如下所示:

find_package(Qt5Core 5.10 REQUIRED)
Run Code Online (Sandbox Code Playgroud)

find_package如果找不到 Qt5Core 的兼容版本,调用将失败:

CMake Warning at CMakeLists.txt:15 (find_package):
Could not find a configuration file for package "Qt5Core" that is
compatible with requested version "5.10".

The following configuration files were considered but not accepted:

C:/some/where/lib/cmake/Qt5Core/Qt5CoreConfig.cmake, version: 5.9.4
Run Code Online (Sandbox Code Playgroud)

这在https://cmake.org/cmake/help/latest/command/find_package.html#version-selection中有详细记录