- Qt Documentation
- Contents
- Adding CMake Tools
- Editing CMake Configuration Files
- Adding External Libraries to CMake Projects
- Related Topics
- Qt Documentation
- Contents
- Getting Started
- Imported targets
- Variable Reference
- Module variables
- Installation variables
- Command Reference
- Qt5::Core
- Qt5::Widgets
- Qt5::DBus
- Qt5::LinguistTools
- Qt Documentation Snapshots
- Contents
- Adding CMake Tools
- Creating CMake Projects
- Opening CMake Projects
- Editing CMake Configuration Files
- Building CMake Projects
- Running CMake Projects
- Deploying CMake Projects to Embedded Linux Devices
- Adding External Libraries to CMake Projects
Qt Documentation
Contents
CMake automates the configuration of build systems. It controls the software compilation process by using simple configuration files, called CMakeLists.txt files. CMake generates native build configurations and workspaces that you can use in the compiler environment of your choice.
You can use CMake from Qt Creator to build applications for the desktop and Android devices. You can also build single files to test your changes.
Qt Creator automatically detects the CMake executable specified in the PATH . You can add paths to other CMake executables and use them in different build and run kits.
CMake documentation is installed in Qt help file format (.qch) when you install CMake. It is automatically registered by Qt Creator, and you can view it in the Help mode.
Qt Creator automatically runs CMake to refresh project information when you edit a CMakeLists.txt configuration file in a project. Project information is also automatically refreshed when you build the project.
The File System section in the sidebar Projects view displays information from the file system. Qt Creator cannot determine whether the files are part of the project. For example, header files that Qt Creator finds in the project directories but that are not mentioned in the CMakeLists.txt files are listed here.
Adding CMake Tools
Qt Creator requires CMake’s file-based API. Please make sure to use CMake version 3.14, or later.
To view and specify settings for CMake:
To add a path to a CMake executable that Qt Creator does not detect automatically, and to specify settings for it, select Add. To make changes to automatically detected installations, select Clone.
Qt Creator uses the default CMake if it does not have enough information to choose the CMake to use. To set the selected CMake executable as the default, select Make Default.
To remove the selected CMake executable from the list, select Remove.
Select the Kits tab to add the CMake tool to a build and run kit. The kit also specifies the CMake generator that is used for producing project files for Qt Creator and the initial configuration parameters:
For more information, see Adding Kits.
Editing CMake Configuration Files
To open a CMakeLists.txt file for editing, right-click it in the Projects view and select Open With > CMake Editor.
You can also use the cmo filter in the locator to open the CMakeLists.txt file for the current run configuration in the editor. This is the same build target as when you select Build > Build for Run Configuration.
The following features are supported:
- Pressing F2 when the cursor is on a filename to open the file
- Keyword completion
- Code completion
- Path completion
- Auto-indentation
- Matching parentheses and quotes
Adding External Libraries to CMake Projects
Through external libraries, Qt Creator can support code completion and syntax highlighting as if they were part of the current project or the Qt library.
Qt Creator detects the external libraries using the find_package() macro. Some libraries come with the CMake installation. You can find those in the Modules directory of your CMake installation. For more information, see cmake-packages(7).
Syntax completion and highlighting work once your project successfully builds and links against the external library.
Related Topics
В© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Источник
Qt Documentation
Contents
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 and Visual Studio project files.
CMake is a 3rd party tool with its own documentation. The rest of this topic details the specifics of how to use Qt 5 with CMake . The minimum version required to use Qt5 is CMake 3.1.0.
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 is to use the target_link_libraries command. This command automatically adds appropriate include directories, compile definitions, the position-independent-code flag, and links to the qtmain.lib library on Windows.
To build a helloworld GUI executable, typical usage would be:
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 Qt5Config.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 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. Imported target names should be preferred instead of using a variable like Qt5 _LIBRARIES in CMake commands such as target_link_libraries . The actual path to the library can be obtained using the LOCATION property:
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:: 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.
Plugins are also available as IMPORTED targets in CMake. The Qt Network, Qt SQL, Qt GUI, and Qt Widgets modules have plugins associated. They provide a list of plugins in the Qt5 _PLUGINS variable.
Variable Reference
Module variables
The result of a find_package call is that imported targets will be created for use with target_link_libraries , some variables will be populated with information required to configure the build, and macros will be made available for use. The name of the imported target for each module matches the name of the module with a prefix of ‘Qt5::’, for example Qt5::Widgets. All of the package-specific variables have a consistent name with a prefix of the name of the package. For example, find_package(Qt5 COMPONENTS Widgets) will make the following variables available if successfully found:
- Qt5Widgets_VERSION String describing the version of the module.
- Qt5Widgets_LIBRARIES List of libraries for use with the target_link_libraries command.
- Qt5Widgets_INCLUDE_DIRS List of directories for use with the include_directories command.
- Qt5Widgets_DEFINITIONS List of definitions for use with add_definitions.
- 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.
Command Reference
Qt5::Core
Compiles big binary resources into object code
Creates an RCC file from a list of Qt resource files
Compiles binary resources into source code
Calls moc on an input file
Creates .moc files from sources
Qt5::Widgets
Creates sources for .ui files
Qt5::DBus
Generates an adaptor class for a D-Bus interface
Generates C++ sources implementing an interface for a D-Bus interface description file
Generates C++ sources implementing interfaces for D-Bus interface description files
Generates a D-Bus interface from a header file
Qt5::LinguistTools
Compiles Qt Linguist .ts files into .qm files
Sets up the Qt Linguist translation toolchain
В© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Источник
Qt Documentation Snapshots
Contents
CMake is an alternative to qmake for automating the generation of build configurations. It controls the software compilation process by using simple configuration files, called CMakeLists.txt files. CMake generates native build configurations and workspaces that you can use in the compiler environment of your choice.
Qt Creator automatically detects the CMake executable specified in the PATH . You can add paths to other CMake executables and use them in different build and run kits.
Adding CMake Tools
Qt Creator supports CMake version 3.0, or later.
To specify paths to CMake executables and to add them to kits:
For more information, see Adding Kits.
Creating CMake Projects
To create a CMake project:
- Select File >New File or Project >Non-Qt Project >Plain C Application or Plain C++ Application >Choose.
- In the Name field, enter a name for the project.
- In the Create in field, enter the path for the project files, and then select Next (or Continue on OS X).
- In the Build system field, select CMake, and then select Next.
- Select CMake kits for the platforms that you want to build the application for, and then select Next.
- Review the project settings, and click Finish (or Done on OS X).
- Select Run CMake to generate a .cbp file.
Some projects require command line arguments to the initial CMake call. CMake will remember the arguments during subsequent calls.
Qt Creator generates a main.cpp and CMakeLists.txt file that you can modify in the Edit mode.
Opening CMake Projects
To open an existing CMake project:
- Select File >Open File or Project.
- Select the CMakeLists.txt file from your CMake project.
- Select a kit that is configured to use CMake for building the project.
- In Projects, right-click the project name to open the context menu, and then select Run CMake to have the project contents listed in the view.
Editing CMake Configuration Files
To open a CMakeLists.txt file for editing, right-click it in the Projects view and select Open with > CMake Editor.
The following features are supported:
- Pressing F2 when the cursor is on a filename to open the file
- Keyword completion
- Auto-indentation
- Matching parentheses and quotes
Building CMake Projects
To build CMake projects, select Build Project or press Ctrl+B (or Cmd+B on OS X).
Qt Creator builds CMake projects by running make , mingw32-make , nmake , or ninja depending on the selected kit.
By default, Qt Creator uses the Default build configuration. You can select another build configuration in Projects > Build Settings > Edit build configuration. In addition to debug and release build configurations, you can create a release build that contains debug information or a release build with the smallest possible size.
In the Build directory field, you can specify the directory in which the project is built (shadow build).
To view all settings, select the Advanced check box.
To modify the value of a build setting, select it, and then select Edit. The new value is displayed in italics until you save the changes by selecting Apply Configuration Changes. Any configuration change might trigger a follow-up configuration change, so keep saving until no more values are displayed in italics.
You can add arguments and targets for the build command in Build Steps.
You can add arguments and targets for the clean command in Clean Steps.
The build errors and warnings are parsed and displayed in the Issues output pane.
Running CMake Projects
Qt Creator automatically adds Run Configurations for all targets specified in the CMake project file.
To run CMake projects, select Run or press Ctrl+R (or Cmd+R on OS X).
Deploying CMake Projects to Embedded Linux Devices
Qt Creator cannot extract files to be installed from a CMake project, and therefore, only executable targets are automatically added to deployment files. You must specify all other files in the QtCreatorDeployment.txt file that you create and place in either the root directory of the CMake project or the build directory of the active build configuration. Currently, Qt Creator first checks the root directory and only if no QtCreatorDeployment.txt exists it checks the active build directory.
Use the following syntax in the file:
- is the (absolute) path prefix to where files are copied on the remote machine.
- is the file path relative to the CMake project root. No directories or wildcards are allowed in this value.
- is the destination directory path relative to deployment/prefix .
To automate the creation of QtCreatorDeployment.txt file:
- Define the following macros in the top level CMakeLists.txt file:
- Use add_deployment_file( ) to add files and add_deployment_directory( ) to add directories (including subdirectories) to the QtCreatorDeployment.txt file.
- Re-run cmake after you add or remove files using the macros.
Adding External Libraries to CMake Projects
Through external libraries, Qt Creator can support code completion and syntax highlighting as if they were part of the current project or the Qt library.
Qt Creator detects the external libraries using the FIND_PACKAGE() macro. Some libraries come with the CMake installation. You can find those in the Modules directory of your CMake installation.
Note: If you provide your own libraries, you also need to provide your own FindFoo.cmake file. For more information, see CMake FAQ.
Syntax completion and highlighting work once your project successfully builds and links against the external library.
© 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Источник