I'm using CMake to build a Qt project, and it uses some of the newer Qt features and requires at least version 5.3 to build properly. I'd like to be nice to the people trying to build the project and fail at CMake configure time with a logical error telling them their CMake version isn't recent enough, rather than with some esoteric build error.
I know that I'll be getting at least version 5.0 by simply using the module find_package
syntax (i.e. find_package(Qt5Widgets REQURIED)
), but it's not so obvious how to make sure I'm using the right minor version.
What's the easiest way to do this?
I know this is a somewhat old post, but you can check the version using Qt5Widgets_VERSION
. Here's some example CMake code:
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)
These days it is possible to pass a version to find_package
, like this:
find_package(Qt5Core 5.10 REQUIRED)
The find_package
call will fail if no compatible version of Qt5Core can be found:
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
This is in-depth documented at https://cmake.org/cmake/help/latest/command/find_package.html#version-selection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With