Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 with Qt6 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 like

    target_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.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It's described in the CMake chapter in Qt's documentation .



  • Thank you. I don't know how I overlooked that in the 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.


  • Lifetime Qt Champion

    Good point

    The doc update is in progress.


Log in to reply