Klaim
Klaim

Reputation: 69682

How to link QtMain in CMake with Qt5?

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

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.

Upvotes: 20

Views: 19432

Answers (3)

steveire
steveire

Reputation: 11074

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/

Upvotes: 7

Fraser
Fraser

Reputation: 78320

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.

Upvotes: 14

Antwane
Antwane

Reputation: 22598

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.

Upvotes: 5

Related Questions