making one CMake project buildable with either Qt5 or Qt6
-
I would like to make my project which uses CMake able to be built with either Qt5 or Qt6 until we start using new features only available in Qt6. Currently we have code like this in CMakeLists.txt:
target_link_libraries(mixxx-lib PUBLIC Qt5::Concurrent Qt5::Core Qt5::Gui Qt5::Network Qt5::OpenGL Qt5::Qml Qt5::Sql Qt5::Svg Qt5::Test Qt5::Widgets Qt5::Xml)
I could find-and-replace
Qt5
withQt6
and keep that in a separate Git branch, but I want to keep the Qt6 branch with as little differences from the main development branch as possible to reduce maintenance burden. What is the simplest way I could do this with CMake? Defining a variable and using it liketarget_link_libraries(mixxx-lib PUBLIC ${QT_MAJOR}::Concurrent ${QT_MAJOR}::Core ${QT_MAJOR}::Gui ${QT_MAJOR}::Network ${QT_MAJOR}::OpenGL ${QT_MAJOR}::Qml ${QT_MAJOR}::Sql ${QT_MAJOR}::Svg ${QT_MAJOR}::Test ${QT_MAJOR}::Widgets ${QT_MAJOR}::Xml)
does not work.
I suppose one approach could be moving all the Qt code from the main CMakeLists.txt to another file then conditionally including one based on an option provided by the user. But I hope there is an easier to maintain way. -
Hi and welcome to devnet,
It's described in the CMake chapter in Qt's documentation .
-
Actually, I do know how I over looked that. I looked at the Qt6 Porting Guide. That links at the bottom to Qt6 Build System which discusses building Qt but not building applications. I recommend linking to CMake Qt5 and Qt6 compatibility from both the Qt6 Porting Guide and Qt6 Build System pages.
-
Good point
The doc update is in progress.