- Deploying to Linux with CMake
- Packaging the installation folder
- Summary
- Setting Up a CMake Project
- 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
Deploying to Linux with CMake
A while ago, I blogged about our CMake deployment API that allows you to enhance the cmake —install step with instructions that deploy Qt libraries, plugins and assets. At the time of writing, Linux was not supported yet. But not anymore: Qt 6.5 comes with deployment support for Linux!
Quick recap how it looks like to deploy a simple Qt application. Consider the following project.
cmake_minimum_required(VERSION 3.22)
project(MyApp)
find_package(Qt6 REQUIRED COMPONENTS Widgets)
qt_standard_project_setup()
qt_add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE Qt::Widgets)
Now, we install the application and enhance the installation step. Our goal is to have a (mostly) self-contained directory after installation.
# Install the executable into "$/bin".
install(TARGETS MyApp
BUNDLE DESTINATION .
RUNTIME DESTINATION $
)
# Generate the deployment script for the target MyApp.
qt_generate_deploy_app_script(
TARGET MyApp
FILENAME_VARIABLE deploy_script
NO_UNSUPPORTED_PLATFORM_ERROR
)
# Call the deployment script on "cmake --install".
install(SCRIPT $)
We build and install the project:
$ qt-cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/tmp/my-application ..
[output omitted]
$ cmake --build .
[output omitted]
$ cmake --install .
-- Install configuration: "Release"
-- Installing: /tmp/my-application/bin/myapp
-- Set runtime path of "/tmp/my-application/bin/myapp" to "$ORIGIN:$ORIGIN/../lib"
-- Writing /tmp/my-application/bin/qt.conf
-- Running generic Qt deploy tool on /home/someone/playground/myapp/build/myapp
-- Installing: /tmp/my-application/lib/libQt6Core.so.6
-- Installing: /tmp/my-application/lib/libQt6Core.so.6.6.0
-- Installing: /tmp/my-application/lib/libQt6DBus.so.6
-- Installing: /tmp/my-application/lib/libQt6DBus.so.6.6.0
-- Installing: /tmp/my-application/lib/libQt6EglFSDeviceIntegration.so.6
-- Installing: /tmp/my-application/lib/libQt6EglFSDeviceIntegration.so.6.6.0
-- Installing: /tmp/my-application/lib/libQt6EglFsKmsSupport.so.6
-- Installing: /tmp/my-application/lib/libQt6EglFsKmsSupport.so.6.6.0
-- Installing: /tmp/my-application/lib/libQt6Gui.so.6
-- Installing: /tmp/my-application/lib/libQt6Gui.so.6.6.0
-- Installing: /tmp/my-application/lib/libQt6OpenGL.so.6
-- Installing: /tmp/my-application/lib/libQt6OpenGL.so.6.6.0
-- Installing: /tmp/my-application/lib/libQt6Widgets.so.6
-- Installing: /tmp/my-application/lib/libQt6Widgets.so.6.6.0
-- Installing: /tmp/my-application/lib/libQt6XcbQpa.so.6
-- Installing: /tmp/my-application/lib/libQt6XcbQpa.so.6.6.0
-- Installing: /tmp/my-application/plugins/egldeviceintegrations/libqeglfs-emu-integrat.
-- Installing: /tmp/my-application/plugins/egldeviceintegrations/libqeglfs-kms-egldevic.
-- Installing: /tmp/my-application/plugins/egldeviceintegrations/libqeglfs-x11-integrat.
-- Installing: /tmp/my-application/plugins/imageformats/libqgif.so
-- Installing: /tmp/my-application/plugins/imageformats/libqico.so
-- Installing: /tmp/my-application/plugins/imageformats/libqjpeg.so
-- Installing: /tmp/my-application/plugins/xcbglintegrations/libqxcb-egl-integration.so
-- Installing: /tmp/my-application/plugins/xcbglintegrations/libqxcb-glx-integration.so
-- Installing: /tmp/my-application/plugins/platforms/libqxcb.so
After the myapp executable is installed into /tmp/my-application/bin/ , the deployment script handles the following tasks:
- Create a qt.conf file next to the executable that contains information about the directory layout. This is needed at runtime to find the plugins and assets. See the qt.conf documentation for details.
- Inspect the executable and used Qt plugins with CMake’s built-in file(GET_RUNTIME_DEPENDENCIES) to determine which Qt libraries to deploy.
- Install the necessary Qt plugins and Qt libraries.
The installation directory can now be copied to a different machine, and the application should still work.
Packaging the installation folder
After installation, we have a nice self-contained directory that can be packaged and shipped. For the sake of example, we’ll create a .deb package using cpack .
Add the following at the end of the project file:
set(CPACK_PACKAGE_NAME my-app)
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "My amazing application")
set(CPACK_PACKAGE_VENDOR "My Company")
set(CPACK_PACKAGE_INSTALL_DIRECTORY $)
set(CPACK_VERBATIM_VARIABLES ON)
set(CPACK_PACKAGING_INSTALL_PREFIX "/opt/myapp")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Manfred Maintainer ")
set(CPACK_DEBIAN_PACKAGE_DEPENDS libc6 libstdc++6 libgcc-s1)
include(CPack)
Re-configure the project and run cpack :
$ cpack -G DEB
CPack: Create package using DEB
CPack: Install projects
CPack: - Install project: MyApp []
CPack: Create package
CPack: - package: /home/someone/projects/myapp/build/my_app-1.0-Linux.deb generated.
That’s already it. The package has been wrapped and is ready to be shipped.
You can inspect the content of the package with
dpkg -c my_app-1.0-Linux.deb
and install with
sudo dpkg -i my_app-1.0-Linux.deb .
Summary
We have seen how to deploy a simple Qt application on Linux using Qt 6.5’s CMake deployment API. There’s no dedicated linuxdeployqt tool. The current solution wholly relies on CMake’s built-in functionality.
These are the platforms that are covered by Qt’s CMake deployment API:
For Android and iOS there’s still the need to call androiddeployqt / macdeployqt manually — or you let Qt Creator handle these details.
The relocatable installation folder can be conveniently wrapped into a package using tools like cpack .
Please report any issues you find while playing with this at our bug tracker.
Setting Up a CMake Project
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.
Qt Creator automatically runs CMake to regenerate makefiles when you edit a CMakeLists.txt configuration file in a project. If the project uses several configuration files, you can disable the automatic generation of makefiles by selecting Tools > Options > Build & Run > CMake. Makefiles are automatically regenerated when you build the project.
Adding CMake Tools
Qt Creator supports CMake version 3.0, or later. For best results you should use CMake version 3.3.2 or later. Earlier versions provide less information to the code model, which will then fail to resolve includes and defines.
To specify paths to CMake executables and to add them to kits:
- Select Tools >Options >Build & Run >CMake >Add.
- In the Name field, specify a name for the tool.
- In the Path field, specify the path to the CMake executable.
- Deselect the Autorun CMake check box to prevent Qt Creator from running CMake when you make changes to CMakeLists.txt files.
- Select Apply to save your changes.
- 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 configuration variables that are used: 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 macOS).
- 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 macOS).
- 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
- Code 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 macOS).
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 macOS).
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:
deployment/prefix> relative/source/file1>:relative/destination/dir1> ... relative/source/filen>:relative/destination/dirn>
- 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:
file(WRITE "$/QtCreatorDeployment.txt" "\n") macro(add_deployment_file SRC DEST) file(RELATIVE_PATH path $ $) file(APPEND "$/QtCreatorDeployment.txt" "$/$:$\n") endmacro() macro(add_deployment_directory SRC DEST) file(GLOB_RECURSE files RELATIVE "$" "$/*") foreach(filename $) get_filename_component(path $ PATH) add_deployment_file("$" "$/$") endforeach(filename) endmacro()
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.
© 2016 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.