How to link QtMain in CMake with Qt5?

后端 未结 3 1545
無奈伤痛
無奈伤痛 2020-12-30 02:05

I upgraded my project code from Qt4 to Qt5. It uses CMake. The conversion got well except for one line of Cmake commands related to Qt. I can’t find in current documentation

相关标签:
3条回答
  • 2020-12-30 02:12

    EDIT : Thanks to Archi comment (see below), simply add

    target_link_libraries(<your_app> Qt5::WinMain)
    

    or

    target_link_libraries(<your_app> ${Qt5Core_QTMAIN_LIBRARIES})
    

    in your application's CMakeLists.txt. Both syntaxes worked for me.

    0 讨论(0)
  • 2020-12-30 02:18

    As of CMake 2.8.11 and Qt 5.1, linking to Qt5::WinMain is automatic/implicit if you specify WIN32 in your add_executable call, or otherwise set the WIN32_EXECUTABLE target property.

    The presentation at

    https://devdays.kdab.com/wp-content/uploads/2012/cmake.pdf

    with video at

    http://www.youtube.com/watch?feature=player_detailpage&v=GJ0kMsLbk6Q#t=751

    describes the features which made it into CMake 2.8.11.

    For more about CMake with Qt see

    http://www.kdab.com/modern-cmake-with-qt-and-boost/

    0 讨论(0)
  • 2020-12-30 02:35

    From the Qt docs you linked to, it seems you can find Qt5Core instead of Qt5Widgets. That will create an imported target named Qt5::WinMain. From the Qt docs:

    Imported targets are created for each Qt module. That means that the Qt5<Module>_LIBRARIES contains a name of an imported target, rather than a path to a library.
    ...
    Each module in Qt 5 has a library target with the naming convention Qt5::<Module>

    find_package( Qt5Widgets REQUIRED )
    find_package( Qt5Core REQUIRED )
    ...
    add_executable( aosdesigner WIN32 ${AOSDESIGNER_ALL_FILES} )
    target_link_libraries( aosdesigner
        ${Boost_LIBRARIES}
        utilcpp
        aoslcpp
        Qt5::WinMain  # <-- New target available via find_package ( Qt5Core )
    )
    
    qt5_use_modules( aosdesigner Widgets )

    I'd also recommend that you remove your two link_libraries calls since it's a deprecated command and I'd specify CMake version 2.8.9 rather than just 2.8 as the minimum required at the top of your CMakeLists.txt, since that's required for qt5_use_modules.

    0 讨论(0)
提交回复
热议问题