CMake: Finding Qt 5 the “Right Way”

I work on build systems a fair bit, and this is something I thought others might benefit from. 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. 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. This led to many of us, myself included, finding Qt 5 modules individually:

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. 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. There is a better way, and it makes many of these things simpler again:

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 are many other great features in CMake that improve Qt integration, but I want to keep this post short…

10 Responses to CMake: Finding Qt 5 the “Right Way”

  1. Pingback: Alexis Girault

  2. Pingback: Sankhesh Jhaveri

  3. Pingback: TManhente

    • Pingback: Marcus D. Hanwell

      • Pingback: Marcus D. Hanwell

  4. Pingback: Andre Fitch

    • Pingback: Marcus D. Hanwell

  5. Pingback: Keith Kyzivat (Keithel)

Questions or comments are always welcome!