ManuelSchneid3r
ManuelSchneid3r

Reputation: 16121

CMake version branch for Qt

My application needs svg support, which was moved to a external module, that has to be linked explicitely. This happened in Qt 5.1. How can I adjust my CMakeLists.txt to distinguish between minor Qt verions?

Upvotes: 1

Views: 270

Answers (2)

Nicolas Holthaus
Nicolas Holthaus

Reputation: 8313

You can use qmake to query the Qt version, and make sure it matches a minimum that you specify.

set(QT_MINIMUM_VERSION 5.1.0)

set(QTDIR $ENV{QTDIR} CACHE STRING "Qt install path")
list(APPEND CMAKE_PREFIX_PATH ${QTDIR})

# Test for supported Qt version
find_program(QMAKE NAMES qmake HINTS ${QTDIR} PATH_SUFFIXES bin)
execute_process(COMMAND ${QMAKE} -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()

Upvotes: 2

nils
nils

Reputation: 2534

This example CMakeLists.txt should be a good start

cmake_minimum_required(VERSION 2.8.11)

project(testproject)

# Make sure that CMake can find your Qt installations.
list(APPEND CMAKE_PREFIX_PATH "/path/to/qt5.1")
list(APPEND CMAKE_PREFIX_PATH "/path/to/qt5.2")
list(APPEND CMAKE_PREFIX_PATH "/path/to/qt5.3")
list(APPEND CMAKE_PREFIX_PATH "/path/to/qt5.4")
# ...

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# You version switch.
set(MY_QT_VERSION 5.1)

# Find the QtWidgets library
find_package(Qt5Widgets ${MY_QT_VERSION} EXACT REQUIRED)
find_package(Qt5Svg ${MY_QT_VERSION} EXACT REQUIRED)

# Tell CMake to create the helloworld executable
add_executable(helloworld main.cpp)

# Use the Widgets module from Qt 5.
target_link_libraries(helloworld Qt5::Widgets Qt5::Svg)

More details can be found at qt.io or here.

Upvotes: 1

Related Questions