Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. making one CMake project buildable with either Qt5 or Qt6
QtWS25 Last Chance

making one CMake project buildable with either Qt5 or Qt6

Scheduled Pinned Locked Moved Unsolved Qt 6
5 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Be.ing
    wrote on 11 Dec 2020, 19:16 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Dec 2020, 19:20 last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Be.ing
        wrote on 11 Dec 2020, 19:41 last edited by
        #3

        Thank you. I don't know how I overlooked that in the documentation.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Be.ing
          wrote on 11 Dec 2020, 19:55 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 11 Dec 2020, 20:46 last edited by
            #5

            Good point

            The doc update is in progress.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            1/5

            11 Dec 2020, 19:16

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved