Important: Please read the 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

    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

    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