Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link QtMain in CMake with Qt5?

Tags:

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.

like image 794
Klaim Avatar asked Jan 01 '13 23:01

Klaim


People also ask

How does CMake find qt5?

In order for find_package to be successful, Qt 5 must be found below the CMAKE_PREFIX_PATH, or the Qt5_DIR must be set in the CMake cache to the location of the Qt5WidgetsConfig. cmake file. The easiest way to use CMake is to set the CMAKE_PREFIX_PATH environment variable to the install prefix of Qt 5.

Does Qt use CMake?

CMake is a buildsystem generator developed in the open, and widely used for Qt based development.

What is QT CMake?

CMake is a tool to simplify the build process for development projects across different platforms. CMake automatically generates build systems, such as Makefiles and Ninja files. CMake is a third-party tool with its own documentation. This manual focuses on how to use CMake to build Qt applications and libraries.


2 Answers

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.

like image 168
Fraser Avatar answered Sep 18 '22 03:09

Fraser


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/

like image 34
steveire Avatar answered Sep 22 '22 03:09

steveire