Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

CMake Manual

CMake is a tool that helps simplify the build process for development projects across different platforms. CMake automates the generation of buildsystems such as Makefiles.

CMake is a 3rd party tool with its own documentation. The rest of this manual details the specifics of how to use Qt 5 with CMake. The minimum version required to use Qt5 is CMake 2.8.3, but 2.8.8 is recommended.

Getting Started

The first requirement when using CMake is to use find_package to locate the libraries and header files shipped with Qt. These libraries and header files can then be used to build libraries and applications based on Qt.

The recommended way to use Qt libraries and headers with CMake 2.8.8 is to use the qt5_use_modules command.

To build a helloworld executable, typical usage would be:

 cmake_minimum_required(VERSION 2.8.8)

 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 main.cpp)

 # Use the Widgets module from Qt 5.
 qt5_use_modules(helloworld Widgets)

Note that it is necessary to use find_package to find a Qt module before using the macro. See the documentation for the CMake find_package Documentation command for the full options it supports.

In order for find_package to be successful, Qt 5 must be found below the CMAKE_PREFIX_PATH, or the Qt5<Module>_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.

The qt5_use_modules command encapsulates all of the variable usage required to use a Qt module. It automatically finds the modules given to it on the command line if they have not already been found.

 find_package(Qt5Core)

 # Find the Widgets Sql and Network modules, and
 # use them in helloworld.
 qt5_use_modules(helloworld Widgets Sql Network)

The CMAKE_AUTOMOC setting runs moc automatically when required. For more on this feature see the CMake AUTOMOC documentation

Imported targets

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. The actual path to the library can be obtained using the LOCATION property:

 find_package(Qt5Core)

 get_target_property(QtCore_location Qt5::Core LOCATION)

Note however that it is rare to require the full location to the library in CMake code. Most CMake APIs are aware of imported targets and can automatically use them instead of the full path.

Each module in Qt 5 has a library target with the naming convention Qt5::<Module> which can be used for this purpose.

Imported targets are created with the configurations Qt was configured with. That is, if Qt was configured with the -debug switch, an imported target with the configuration DEBUG will be created. If Qt was configured with the -release switch an imported target with the configuration RELEASE will be created. If Qt was configured with the -debug-and-release switch (the default on windows), then imported targets will be created with both RELEASE and DEBUG configurations.

If your project has custom CMake build configurations, it may be necessary to set a mapping from your custom configuration to either the debug or release Qt configuration.

 find_package(Qt5Core)

 set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-arcs -ftest-coverage")

 # set up a mapping so that the Release configuration for the Qt imported target is
 # used in the COVERAGE CMake configuration.
 set_target_properties(Qt5::Core PROPERTIES MAP_IMPORTED_CONFIG_COVERAGE "RELEASE")

Using Qt 5 with CMake older than 2.8.8

If using CMake older than 2.8.8, the qt5_use_modules macro is not available. Attempting to use it will result in an error.

To use Qt 5 with versions of CMake older than 2.8.8, it is necessary to use the target_link_libraries, include_directories, and add_definitions commands, and to manually specify moc requirements with either qt5_generate_moc or qt5_wrap_cpp:

 cmake_minimum_required(VERSION 2.8.3)

 project(testproject)

 # Find includes in corresponding build directories
 set(CMAKE_INCLUDE_CURRENT_DIR ON)

 # Find the QtWidgets library
 find_package(Qt5Widgets)

 # Add the include directories for the Qt 5 Widgets module to
 # the compile lines.
 include_directories(${Qt5Widgets_INCLUDE_DIRS})

 # Use the compile definitions defined in the Qt 5 Widgets module
 add_definitions(${Qt5Widgets_DEFINITIONS})

 # Add compiler flags for building executables (-fPIE)
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")

 qt5_generate_moc(main.cpp main.moc)

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

 #Link the helloworld executable to the Qt 5 widgets library.
 target_link_libraries(${Qt5Widgets_LIBRARIES})

It is also necessary when using an older CMake to add Qt5<Module>_EXECUTABLE_COMPILE_FLAGS to the CMAKE_CXX_FLAGS so that the -fPIE flags are added to the compile flags if necessary (as is the default with Qt 5).

Variable Reference

Module variables

The result of a find_package call is that some variables will be populated with information required to configure the build, and macros will be made available for use. All of the package-specific variables have a consistent name with a prefix of the name of the package. For example, find_package(Qt5Widgets) will make the following variables available if successfully found:

  • Qt5Widgets_VERSION_STRING
  • Qt5Widgets_LIBRARIES List of libraries for use with the target_link_libraries command, for example.
  • Qt5Widgets_INCLUDE_DIRS List of libraries for use with the include_directories command, for example.
  • Qt5Widgets_DEFINITIONS List of definitions for use with add_definitions, for example.
  • Qt5Widgets_COMPILE_DEFINITIONS List of definitions for use with the COMPILE_DEFINITIONS target property.
  • Qt5Widgets_FOUND Boolean describing whether the module was found successfully.
  • Qt5Widgets_EXECUTABLE_COMPILE_FLAGS String of flags to be used when building executables.

Equivalents of those variables will be available for all packages found with a find_package call. Note that the variables are case-sensitive.

Installation variables

Additionally, several other variables are available which do not relate to a particular package, but to the Qt installation itself.

  • QT_VISIBILITY_AVAILABLE Boolean describing whether Qt was built with hidden visibility.
  • QT_LIBINFIX String containing the infix used in library names.

Macro Reference

Qt5Core macros

Macros available when Qt5Core is found.

MacroDescription
QT5_WRAP_CPP(outfiles inputfile ... OPTIONS ...)Create moc code from a list of files containing Qt class with the Q_OBJECT declaration. Per-direcotry preprocessor definitions are also added. Options may be given to moc, such as those found when executing "moc -help".
QT5_ADD_RESOURCES(outfiles inputfile ... OPTIONS ...)Create code from a list of Qt resource files. Options may be given to rcc, such as those found when executing "rcc -help"
QT5_GENERATE_MOC(inputfile outputfile )Creates a rule to run moc on infile and create outfile. Use this if for some reason QT5_WRAP_CPP() isn't appropriate, e.g. because you need a custom filename for the moc file or something similar.
QT5_USE_MODULES(target [LINK_PUBLIC|LINK_PRIVATE] module ... )Indicates that the target uses the named Qt 5 modules. The target will be linked to the specified modules, use the include directories installed by those modules, use the COMPILE_DEFINITIONS set by those modules, and use the COMPILE_FLAGS set by the modules. The LINK_PRIVATE or LINK_PUBLIC specifiers can optionally be specified. If LINK_PRIVATE is specified then the modules are not made part of the link interface of the target. See the documentation for target_link_libraries for more information.

Note that this macro is only available if using CMake 2.8.8 or later.

Qt5Widgets macros

Macros available when Qt5Widgets is found.

MacroDescription
QT5_WRAP_UI(outfiles inputfile ... OPTIONS ...)Create code from a list of Qt designer ui files. Options may be given to uic, such as those found when executing "uic -help"

Qt5DBus macros

Macros available when Qt5DBus is found.

MacroDescription
QT5_ADD_DBUS_INTERFACE(outfiles interface basename)Create a the interface header and implementation files with the given basename from the given interface xml file and add it to the list of sources
QT5_ADD_DBUS_INTERFACES(outfiles inputfile ... )Create the interface header and implementation files for all listed interface xml files the name will be automatically determined from the name of the xml file
QT5_ADD_DBUS_ADAPTOR(outfiles xmlfile parentheader parentclassname [basename] [classname])Create a dbus adaptor (header and implementation file) from the xml file describing the interface, and add it to the list of sources. The adaptor forwards the calls to a parent class, defined in parentheader and named parentclassname. The name of the generated files will be <basename>adaptor.{cpp,h} where basename defaults to the basename of the xml file. If <classname> is provided, then it will be used as the classname of the adaptor itself.
QT5_GENERATE_DBUS_INTERFACE( header [interfacename] OPTIONS ...)Generate the xml interface file from the given header. If the optional argument interfacename is omitted, the name of the interface file is constructed from the basename of the header with the suffix .xml appended. Options may be given to qdbuscpp2xml, such as those found when executing "qdbuscpp2xml --help"

Qt5LinguistTools macros

Macros available when Qt5LinguistTools is found.

MacroDescription
QT5_CREATE_TRANSLATION( qm_files directories ... sources ... ts_files ... OPTIONS ...)Out: qm_files In: Directories sources ts_files Options: flags to pass to lupdate, such as -extensions to specify Extensions for a directory scan. Generates commands to create .ts (vie lupdate) and .qm (via lrelease) - files from directories and/or sources. The ts files are created and/or updated in the source tree (unless given with full paths). The qm files are generated in the build tree. Updating the translations can be done by adding the qm_files to the source list of your library/executable, so they are always updated, or by adding a custom target to control when they get updated/generated.
QT5_ADD_TRANSLATION( qm_files ts_files ... )Out: qm_files In: ts_files Generates commands to create .qm from .ts - files. The generated filenames can be found in qm_files. The ts_files must exists and are not updated in any way.
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web