![]() ![]() All of the things mentioned in both the cmake docs and in the Qt docs were not true for this install. So, I encountered something weird - on Windows, with Qt 5.11.1 and 5.11.2 at least, I was trying to figure out how cmake found the Qt cmake modules under /lib/cmake. There are many other great features in CMake that improve Qt integration, but I want to keep this post short… This is not only more compact, which is always nice, but it means that you can simply pass in ‘Qt5_DIR’ to your project, and it will use that when searching for the components. There is a better way, and it makes many of these things simpler again: find_package(Qt5 COMPONENTS Core Widgets REQUIRED) It also led to either using ‘CMAKE_PREFIX_PATH’ to specify the prefix Qt 5 was installed in, or passing in ‘Qt5Core_DIR’, ‘Qt5Widgets_DIR’, etc for the directory containing each and every Qt 5 config module – normally all within a common prefix. This didn’t feel right to me, but I hadn’t yet seen what I think is the preferred way (and the way I would recommend) to find Qt 5. This led to many of us, myself included, finding Qt 5 modules individually: find_package(Qt5Core REQUIRED) Qt 5 improved its integration with CMake quite significantly – moving from using the qmake command to find Qt 4 components in a traditional CMake find module to providing its own CMake config files. This meant that instead of having to guess where Qt and its libraries/headers were installed we could use the information generated by Qt’s own build system. I went through quite a bit of code in our projects that did not do things the “right way”, and it wasn’t clear what that was to me at first. # used in the COVERAGE CMake configuration.I work on build systems a fair bit, and this is something I thought others might benefit from. # set up a mapping so that the Release configuration for the Qt imported target is Set(CMAKE_CXX_FLAGS_COVERAGE "$ -fprofile-arcs -ftest-coverage") find_package(Qt5 COMPONENTS Core REQUIRED) If your project has custom CMake build configurations, you have to map your custom configuration to either the debug or the release Qt configuration. If Qt was configured with the -debug-and-release switch, then imported targets are created with both RELEASE and DEBUG configurations.If Qt was configured with the -release switch, an imported target with the RELEASE configuration is created.If Qt was configured with the -debug switch, an imported target with the DEBUG configuration is created.Imported targets are created with the same configurations as when Qt was configured. This eases writing CMake code that can work with both Qt 5 and Qt 6. Note: Since Qt 5.15, the CMake targets are also available as Qt::Core, Qt::Gui, and so on. Pass the name of the library target to target_link_libraries to use the respective library. The target names start with Qt5::, followed by the module name. ![]() Imported library targetsĮach Qt module that is loaded defines a CMake library target. For more details, see CMake AUTOMOC documentation. The CMAKE_AUTOMOC setting runs moc automatically when required. Set the Qt5_DIR in the CMake cache to the location of the Qt5Config.cmake file.Set your CMAKE_PREFIX_PATH environment variable to the Qt 5 installation prefix.Target_link_libraries(helloworld Qt5::Widgets)įor find_package to be successful, CMake must find the Qt installation in one of the following ways: To build a helloworld GUI executable, you need the following: cmake_minimum_required(VERSION 3.1.0)įind_package(Qt5 COMPONENTS Widgets REQUIRED) This command automatically adds the appropriate include directories, compile definitions, the position-independent-code flag, and links to the qtmain.lib library on Windows, for example. Then, you can use these libraries and header files with the target_link_libraries command to build Qt-based libraries and applications. Start with find_package to locate the libraries and header files shipped with Qt. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |