Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Using CMake with Qt 5

The basics of a powerful build system alternative to QMake

find_package(Qt4 COMPONENTS QTCORE QTGUI)
find_package(Qt5Widgets)
  find_package(Qt5Declarative)
find_package(Qt5 COMPONENTS Widgets Declarative)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
    ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
set(CMAKE_CXX_FLAGS "-fPIC")
cmake_minimum_required(2.8.7)
  project(hello-world)

  # Tell CMake to run moc when necessary:
  set(CMAKE_AUTOMOC ON)
  # As moc files are generated in the binary dir, tell CMake
  # to always look for includes there:
  set(CMAKE_INCLUDE_CURRENT_DIR ON)

  # Widgets finds its own dependencies (QtGui and QtCore).
  find_package(Qt5Widgets REQUIRED)

  # 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}")

  add_executable(hello_world main.cpp mainwindow.cpp)

  # The Qt5Widgets_LIBRARIES variable also includes QtGui and QtCore
  target_link_libraries(hello_world ${Qt5Widgets_LIBRARIES})
cmake_minimum_required(2.8.8)
  project(hello-world)

  # Tell CMake to run moc when necessary:
  set(CMAKE_AUTOMOC ON)
  # As moc files are generated in the binary dir, tell CMake
  # to always look for includes there:
  set(CMAKE_INCLUDE_CURRENT_DIR ON)

  # Widgets finds its own dependencies.
  find_package(Qt5Widgets REQUIRED)

  add_executable(hello_world main.cpp mainwindow.cpp)

  qt5_use_modules(hello_world Widgets)
qt5_use_modules(hello_world Widgets Declarative)
TARGET = hello_world
  QT += widgets declarative
add_executable(hello_world main.cpp mainwindow.cpp)
  add_library(hello_library lib.cpp)
  add_executable(hello_coretest test.cpp)

  find_package(Qt5Widgets)

  qt5_use_package(hello_world Widgets)
  qt5_use_package(hello_library Core)
  qt5_use_package(hello_coretest Test)
find_package(Qt4)

About KDAB


28 Comments

21 - May - 2012

Laszlo Papp

21 - May - 2012

Pau Garcia i Quiles

21 - May - 2012

steveire

21 - May - 2012

Pau Garcia i Quiles

21 - May - 2012

steveire

28 - Nov - 2012

Laszlo Papp

29 - Nov - 2012

Laszlo Papp

29 - Nov - 2012

Laszlo Papp

18 - Mar - 2013

Jussi

18 - Mar - 2013

steveire

11 - May - 2013

Romas

12 - May - 2013

steveire

24 - May - 2013

Romas

19 - May - 2013

Kevina

20 - May - 2013

steveire

11 - Aug - 2013

Chris

12 - Aug - 2013

steveire

4 - Dec - 2013

Stephan

13 - Dec - 2013

steveire

15 - Jan - 2014

Paul

15 - Jan - 2014

steveire

17 - Jun - 2014

Doug Barbieri

18 - Jun - 2014

steveire

13 - Nov - 2014

Aman

13 - Nov - 2014

Aman

3 - Feb - 2015

newgen

23 - Apr - 2015

3Dickulus

19 - Dec - 2017

Gaël