Test for supported Qt version with CMake - qt

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

There's no direct way to do this with the Qt-provided cmake packages, but it's easy enough to query the Qt version using qmake. Assuming you have something similar to the following already in the cmake files to locate the Qt install
set(QTDIR $ENV{QTDIR} CACHE STRING "Qt install path")
list(APPEND CMAKE_PREFIX_PATH ${QTDIR})
you can check the version and fail the configure step if it's too low like so
set(QT_MINIMUM_VERSION 5.3.0)
# Test for supported Qt version
find_program(QMAKE_EXECUTABLE NAMES qmake HINTS ${QTDIR} ENV QTDIR PATH_SUFFIXES bin)
execute_process(COMMAND ${QMAKE_EXECUTABLE} -query QT_VERSION OUTPUT_VARIABLE QT_VERSION)
if(QT_VERSION LESS QT_MINIMUM_VERSION)
MESSAGE(FATAL_ERROR "Minimum supported Qt version: ${QT_MINIMUM_VERSION}.
Installed version: ${QT_VERSION}")
endif()

Related

Building Qt project with cmake

Just installed QT and trying to build a QT project from inside the QTcreator using Cmake instead of Qmake.
I get the following error:
Starting to parse CMake project, using: "DCMAKE_BUILD_TYPE:STRING=Debug",
"-DCMAKE_CXX_COMPILER:STRING=",
"-DCMAKE_C_COMPILER:STRING=",
"DCMAKE_PREFIX_PATH:STRING=C:/Qt/5.13.1/msvc2015_64",
"DQT_QMAKE_EXECUTABLE:STRING=C:/Qt/5.13.1/msvc2015_64/bin/qmake.exe".
The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:3 (project):
The CMAKE_CXX_COMPILER:
cl
is not a full path and was not found in the PATH.
To use the JOM generator with Visual C++, cmake must be run from a shell
that can use the compiler cl from the command line. This environment is
unable to invoke the cl compiler. To fix this problem, run cmake from the
Visual Studio Command Prompt (vcvarsall.bat).
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
Configuring incomplete, errors occurred!
See also "C:/Users/tany/AppData/Local/Temp/QtCreator-pKrFgC/qtc-cmake-DqmsxZqd/CMakeFiles/CMakeOutput.log".
See also "C:/Users/tany/AppData/Local/Temp/QtCreator-pKrFgC/qtc-cmake-DqmsxZqd/CMakeFiles/CMakeError.log".
CMake Deprecation Warning:
The 'cmake-server(7)' is deprecated. Please port clients to use the
'cmake-file-api(7)' instead.
CMake Project parsing failed.
Now if I use the Cmake-GUI instead, I can configure and generate the project files successfully, after supplying the environment variable CMAKE_PREFIX_PATH.
But I can't get this to work from inside the QTcreator.
Anyone has any experience with using Cmake with QTcreator then please help.
Thank you!
The error message says that your C++ compiler Visual Studio 2015 (64 bits) could not be located. Maybe when you run cmake-gui it finds another C++ compiler like mingw?
In Qtcreator's settings dialog you have a tab showing the detected compilers, and another one with the available kits.
It seems that QtCreator is using a deprecated version of cmake. Update QtCreator to a new (unreleased, as writing this comment, version 4.10.0 is broken) or downgrade cmake to choco.exe install cmake --version 3.11 --force (a newer version might work).
Please also make sure to install jom (as this is the default for QtCreator/CMake on windows).
choco.exe install jom (how can I tell it to use make?)
The deprecation warning is harmless. CMake deprecated the cmake-server and provides a new API for the same purpose now. Qt Creator will support that from 4.11.
The error tells you, that CMake is unable find the cl compiler. cl.exe comes with Visual Studio. Make sure you mark C++ there during the installation process. It is not installed by default anymore.
Alternatively install the MinGW tool chain from the Qt installer. In Qt context it is easier to use than the Visual Studio tool chain.

CMake: find Qt packages when two Qt versions are installed

On my computer (Ubuntu-Gnome) are two Qt-versions installed. One with the package manager (5.x) and one manually to /opt/Qt (5.9).
For one project I need to use the /opt/Qt-installation with CMake. But find_package(Qt5Core 5.9 COMPONENTS CORE REQUIRED) does not find the correct installation:
Could not find a configuration file for package "Qt5Core" that is
compatible with requested version "5.9".
The following configuration files were considered but not accepted:
/usr/lib/x86_64-linux-gnu/cmake/Qt5Core/Qt5CoreConfig.cmake, version: 5.7.1
I tried to set CMAKE_PREFIX_PATH and CMAKE_MODULE_PATH to add a search path to CMake with all variants of paths but it does not work at all.
How can I correctly set the search path to the second installation at /opt/Qt/?
Updates in order to #Florian input
This works:
find_package(
Qt5Core 5.9
COMPONENTS
Core
REQUIRED
)
together with
cmake -DQt5_DIR:PATH=/opt/Qt/5.9.2/gcc_64/lib/cmake/Qt5Core
but in this case I found only QT5Core. With that it seems to work for all components as well:
find_package(
Qt5 5.9
COMPONENTS
Core
REQUIRED
)
together with
cmake -DQt5_DIR:PATH=/opt/Qt/5.9.2/gcc_64/lib/cmake/Qt5
2nd Edit
To avoid to always put the the full path in the call I add this to the my CMakeLists.txt:
set(QT_INSTALL_PATH /opt/Qt)
file( GLOB_RECURSE sres ${QT_INSTALL_PATH}/*/Qt5Config.cmake )
get_filename_component( Qt5_DIR ${sres} DIRECTORY )
Afterwards this works fine:
find_package(
Qt5 5.9
COMPONENTS
Core
Network
REQUIRED
)
Use cmake -DQt5_DIR:PATH=/opt/Qt5/5.9.2/gcc_64/lib/cmake/Qt5
Documentation

Install amarok, Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x

I try to install Amarok 2.8 music player from source in CentOS7.0.
After reading the README, I knew that Qt4.7(or later) is required. Then I installed the QT5.3(the path is ~/QT).
Followed the Amarok installation instructions,https://community.kde.org/Amarok/Development/Compiling
,
cmake .. -DCMAKE_INSTALL_PREFIX=`kde4-config --prefix`
However,
CMake Error at /usr/share/cmake/Modules/FindQt4.cmake:1368 (message):
Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
I think there is way to tell the cmake where to find the QT.
Hope someone can solve this. Thanks a lot.
in CentOS, you can try:
yum install qt-devel
CMake usually uses qmake to find and determine the Qt version, so make sure that is in your path.
However, you are answering your own question already:
After reading the README, I knew that Qt4.7(or later) is required. Then I installed the QT5.3(the path is ~/QT).
The Amarok CMake script reminds you of this by telling you:
this code requires Qt 4.x
(emphasis mine)

Cmake can't find Qt OpenGL library

I'm trying to compile a project using cmake. The instructions given to me was that Qt 4.8 is needed. Downloaded it from qt-project.org/downloads. Compiled and installed Qt 4.8:
mazdak#lnxamindai> qmake -query
QT_INSTALL_PREFIX:/usr/local/Trolltech/Qt-4.8.5
QT_INSTALL_DATA:/usr/local/Trolltech/Qt-4.8.5
QT_INSTALL_DOCS:/usr/local/Trolltech/Qt-4.8.5/doc
QT_INSTALL_HEADERS:/usr/local/Trolltech/Qt-4.8.5/include
QT_INSTALL_LIBS:/usr/local/Trolltech/Qt-4.8.5/lib
QT_INSTALL_BINS:/usr/local/Trolltech/Qt-4.8.5/bin
QT_INSTALL_PLUGINS:/usr/local/Trolltech/Qt-4.8.5/plugins
QT_INSTALL_IMPORTS:/usr/local/Trolltech/Qt-4.8.5/imports
QT_INSTALL_TRANSLATIONS:/usr/local/Trolltech/Qt-4.8.5/translations
QT_INSTALL_CONFIGURATION:/etc/xdg
QT_INSTALL_EXAMPLES:/usr/local/Trolltech/Qt-4.8.5/examples
QT_INSTALL_DEMOS:/usr/local/Trolltech/Qt-4.8.5/demos
QMAKE_MKSPECS:/usr/local/Trolltech/Qt-4.8.5/mkspecs
QMAKE_VERSION:2.01a
QT_VERSION:4.8.5
However, when compiling the project I received the following error:
root#lnxamindai> cmake .
-- Setting flags for GNU GCC
-- REQUIRED_VARS (missing: QTMOBILITY_INCLUDE_DIR QTMOBILITY_MULTIMEDIAKIT_INCLUDE_DIR QTMOBILITY_MULTIMEDIAKIT_LIBRARY VERSION_VAR QTMOBILITY_VERSION)
Qt QTOPENGL library not found.
-- Buiding ManyEarsLib Library...
-- Buiding RTAudio Library...
-- Found jack: /usr/lib/libjack.so
ALSA lib : -lasound -lpthread
Qt QTOPENGL library not found.
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
OPENGL_INCLUDE_DIR (ADVANCED)
used as include directory in directory /home/mazdak/dev/ManyEars/manyears/QtGUI
QTMOBILITY_MULTIMEDIAKIT_LIBRARY (ADVANCED)
linked by target "ManyEars" in directory /home/mazdak/dev/ManyEars/manyears/QtGUI
-- Configuring incomplete, errors occurred!
CMakeLists.txt:
(...)
IF (NOT MANYEARS_GUI_DISABLED)
SET(QT_USE_QTNETWORK TRUE)
SET(QT_USE_QTSVG TRUE)
SET(QT_USE_QTXML TRUE)
SET(QT_USE_QTSCRIPT TRUE)
SET(QT_USE_QTOPENGL TRUE)
SET(QT_USE_QTMULTIMEDIA TRUE)
find_package(QtMobility COMPONENTS MultimediaKit)
find_package(Qt4 4.8.0 QUIET)
if (QTMOBILITY_FOUND)
MESSAGE("QT_MOBILITY_INCLUDE_DIR : ${QTMOBILITY_INCLUDE_DIRS} QT_MOBILITY_LIB: ${QTMOBILITY_LIBRARIES}")
ENDIF (QTMOBILITY_FOUND)
if(QT4_FOUND AND (QT_QTMULTIMEDIA_FOUND OR QTMOBILITY_FOUND))
include(${QT_USE_FILE})
else(QT4_FOUND AND (QT_QTMULTIMEDIA_FOUND OR QTMOBILITY_FOUND))
MESSAGE("WARNING : ManyEars GUI will not be compiled because Qt4 not found or obsolete. You need Qt 4.8 or higher.Try using the latest QtSDK from http://qt-project.org")
MESSAGE("DEBUG: QT4_FOUND: ${QT4_FOUND} QT_MULTIMEDIA_FOUND: ${QT_QTMULTIMEDIA_FOUND} QT_MOBILITY_FOUND: ${QTMOBILITY_FOUND}")
SET(MANYEARS_GUI_DISABLED TRUE)
endif(QT4_FOUND AND (QT_QTMULTIMEDIA_FOUND OR QTMOBILITY_FOUND))
ELSE (NOT MANYEARS_GUI_DISABLED)
MESSAGE("ManyEars GUI disabled.")
ENDIF (NOT MANYEARS_GUI_DISABLED)
#Files excluded from package
set(CPACK_SOURCE_IGNORE_FILES
"build"
"bin"
${CPACK_SOURCE_IGNORE_FILES}
".svn"
)
#include files
(...)
IF (UNIX)
#########################################
# Packaging stuff for sources (All UNIX)
#########################################
SET(CPACK_SOURCE_GENERATOR "TGZ")
SET(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/COPYING.TXT")
SET(CPACK_PACKAGE_NAME "ManyEars")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "ManyEars")
SET(CPACK_PACKAGE_VENDOR "Francois Grondin, Dominic Letourneau")
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/README.TXT")
SET(CPACK_PACKAGE_CONTACT "blablablabla")
(...)
ENDIF(UNIX)
(...)
INCLUDE(CPack)
This is confusing me, shouldn't the OpenGL library be installed?
This is confusing me, shouldn't the OpenGL library be installed?
It's not complaining about OpenGL. It's complainting about Qt's OpenGL module QtOpenGL which is a different thing. Most likely your custom build of Qt doesn't include the QtOpenGL module. You've to go back building Qt and take extra care that you enable build of the QtOpenGL module.
After some research I found out that I needed to install some development packages if I was compiling from the source code.
apt-get install libfontconfig1-dev libfreetype6-dev libx11-dev libxext-dev libxfixes-dev libxi-dev libxrender-dev libxcb1-dev libx11-xcb-dev libxcb-glx0-dev libxcb-keysyms1-dev libxcb-image0-dev libxcb-shm0-dev libxcb-icccm4-dev libxcb-sync0-dev libxcb-xfixes0-dev libxcb-shape0-dev libxcb-randr0-dev libxcb-render-util0-dev
When configuring the Qt source code I had forgot to specify that I wanted to install the OpenGL module and the Multimedia Kit. I just naturally ran "configure" without any options and hoped for the best.
./configure --help
Specifies that I needed -multimedia and -opengl.
When I executed configure with the correct options the issue was resolved.
./configure -multimedia -opengl
Note that the multimedia kit needs gstreamer installed.

compiling musescore for uCLinux with QT and cmake

I've try few things but I still have this error when trying to cmake for blackfin:
CMake Error at CMakeLists.txt:116 (message):
Fatal error: QT (version >= 4.5.0) required. Cmake tries to detect QT4 by
searching for 'qmake' in your PATH.If you have QT4 installed, make sure
qmake is found in your PATH. If you compiled QT4 yourself make sure your
new qmake ist found _first_ in your PATH.
I'm trying to compile the source of musescore 0.9.5 to keep it compatible with the existing version of QT in uClinux options
What I did is to compile the uClinux kernel with QT lib so I can have the suitable embedded QT.
then I modified the Makefile of musescore to build with uclinux compilers. I've try different things so I show here the latest trial:
bfin:
mkdir build;
mkdir install; \
cd build;
cmake -DCMAKE_TOOLCHAIN_FILE=bfin.cmake -DCMAKE_VERBOSE_MAKEFILE=TRUE -\
DQT_QMAKE_EXECUTABLE=/home/william/Development_new/Music_Recognition/code/uCLin\
ux/rev2011R1/uclinux-dist/lib/qt-embedded/build-qt-embedded-linux-opensource-sr\
c-4.5.1/bin -libdir=/home/william/Development_new/Music_Recognition/cod\
e/uCLinux/rev2011R1/uclinux-dist/lib/qt-embedded/build-qt-embedded-linux-openso\
urce-src-4.5.1/lib \
-DCMAKE_INSTALL_PREFIX="${PREFIX}" \
mscore;
in the CMakeLists.txt I did add:
link_directories(/home/william/Development_new/Music_Recognition/code/uCLinux/r\
ev2011R1/uclinux-dist/lib/qt-embedded/build-qt-embedded-linux-opensource-src-4.\
5.1/bin)
SET(CMAKE_MODULE_PATH /home/william/Development_new/Music_Recognition/code/uCLi\
nux/rev2011R1/uclinux-dist/lib/qt-embedded/build-qt-embedded-linux-opensource-s\
rc-4.5.1/bin)
SET(CMAKE_PREFIX_PATH /home/william/Development_new/Music_Recognition/code/uCLi\
nux/rev2011R1/uclinux-dist/lib/qt-embedded/build-qt-embedded-linux-opensource-s\
rc-4.5.1/bin)
SET(QT_QMAKE_EXECUTABLE /home/william/Development_new/Music_Recognition/code/uC\
Linux/rev2011R1/uclinux-dist/lib/qt-embedded/build-qt-embedded-linux-opensource\
-src-4.5.1/bin)
my bfin.cmake look like that:
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)
#
SET (BFIN "/opt/uClinux/bfin-uclinux")
# specify the cross compiler
SET(CMAKE_C_COMPILER ${BFIN}/bin/bfin-uclinux-gcc)
SET(CMAKE_CXX_COMPILER ${BFIN}/bin/bfin-uclinux-g++)
SET(CMAKE_LINKER ${BFIN}/bin/bfin-uclinux-ld)
#/opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib
LINK_DIRECTORIES("${BFIN}/usr/lib")
SET(CMAKE_FIND_ROOT_PATH
/opt/uClinux/bfin-uclinux/bfin-uclinux
/opt/uClinux/bfin-linux-uclibc
/opt/uClinux/bfin-linux-uclibc/bfin-linux-uclibc/runtime)
# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Thanks in advance for the help,
William
You need to put qmake in your $PATH. If you prefer to use QT_QMAKE_EXECUTABLE it has to point to the actual qmake binary and not the directory.
That being said, MuseScore 0.9.5 is a very very old version of MuseScore, the current version is 1.1 and 1.2 is coming soon. Check http://musescore.org for more information about MuseScore and don't hesitate to contact the developers on #musescore on freenode.net or on the developer mailing list at http://musescore.org/mailing-list

Resources