Detecting Qt5 with CMake

15,098

Solution 1

I added the correct "CMAKE_PREFIX_PATH" as advised by usr1234567 (should it not be made by the installer?) added the include directories following https://www.kdab.com/using-cmake-with-qt-5/ (should it not be included in the manual http://doc.qt.io/qt-5/cmake-manual.html, and the KDevelop-generated CMakeLists.txt ?), furthermore the source file generating the GUI elements (after changing the #include directories from QtGui to QtWidgets, should it not be made by KDevelop?) I transformed the CMakeLists.txt to what shown. Maybe not optimal, but works. (it is my very first attempt with Qt, and I guess the mentioned difficulties can discourage people who prefer out-of-box solutions)

cmake_minimum_required(VERSION 2.8.11)

project(MyFirst)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets)

# Tell CMake to create the helloworld executable
add_executable(MyFirst main.cpp MyFirst.cpp)

# The Qt5Widgets_INCLUDES also includes the include directories for
  # dependencies QtCore and QtGui
  include_directories(${Qt5Widgets_INCLUDES})

  # We need add -DQT_WIDGETS_LIB when using QtWidgets in Qt 5.
  add_definitions(${Qt5Widgets_DEFINITIONS})

  # Executables fail to build with Qt 5 in the default configuration
  # without -fPIE. We add that here.
  set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")

# Use the Widgets module from Qt 5.
target_link_libraries(MyFirst Qt5::Widgets)

Solution 2

Have a look at the Qt 5 documentation containing a section on how to use CMake: http://doc.qt.io/qt-5/cmake-manual.html

It provides an example:

cmake_minimum_required(VERSION 2.8.11)

project(testproject)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets)

# Tell CMake to create the helloworld executable
add_executable(helloworld WIN32 main.cpp)

# Use the Widgets module from Qt 5.
target_link_libraries(helloworld Qt5::Widgets)

Mind the line containing find_package which differs from your line. It also contains advice, how you can help CMake finding your Qt installation:

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.

Further reading: CMake: Finding Qt5 the right way

Share:
15,098
katang
Author by

katang

Updated on June 04, 2022

Comments

  • katang
    katang almost 2 years

    I am trying to install and use Qt 5 on Ubuntu. Running CMake for my project which requires Qt 5 leads to:

    -- The C compiler identification is GNU 4.8.4
    -- The CXX compiler identification is GNU 4.8.4
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    CMake Warning at /usr/local/share/cmake-3.2/Modules/FindQt4.cmake:626 (message):
      /usr/bin/qmake reported QT_INSTALL_LIBS as "/usr/lib/x86_64-linux-gnu" but
      QtCore could not be found there.  Qt is NOT installed correctly for the
      target build environment.
    

    I attempted hints from

    https://askubuntu.com/questions/508503/whats-the-development-package-for-qt5-in-14-04

    https://www.kdab.com/using-cmake-with-qt-5/

    How to find and use Qt 5 with CMake?