How to link QtMain in CMake with Qt5?

故事扮演 提交于 2019-12-18 12:13:10

问题


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, like

  • http://qt-project.org/doc/qt-5.0/qtdoc/cmake-manual.html
  • http://qt-project.org/doc/qt-5.0/qtdoc/qtmain.html

How to link with QtMain from CMake (with Qt5)?

It is the only missing bit to convert my project. Can someone point me to a doc explaining this or explain how to do it with Qt5? My Qt4 code worked correctly but I can't find the Cmake macro for Qt5.

EDIT> Here is the CMake file I have at the moment: https://bitbucket.org/klaim/aos_qt5/src/593c195c4c6889f6968d68fca018ef425783a063/tools/aosdesigner/CMakeLists.txt?at=wip_qt5

All qt5 necessary CMake macros have been set correctly I belive, the only thing that don't work is the linking to QtMain that do nothing, as expected since there should be a Qt5 specific way of doing it that I don't find in the Qt5 documentation.

You can browse the file history to see how it was working with Qt4.


回答1:


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.




回答2:


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/




回答3:


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.



来源:https://stackoverflow.com/questions/14115024/how-to-link-qtmain-in-cmake-with-qt5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!