- Qt for Linux/X11 — Building from Source
- Step 1: Unpacking the Archive
- Step 2: Build the Qt Library
- Step 3: Set the Environment Variables
- Adding Compilers
- Redetecting Compilers
- Specifying Compiler Settings
- Adding Nim Compilers
- Adding Custom Compilers
- Troubleshooting MinGW Compilation Errors
- Configure an Embedded Linux Device
- Toolchain Files versus Device Makespecs
- Host Tools
- Configuring Qt
- An Example Toolchain File
- Building Applications for the Target Device
- Defaults for Platform Plugins and EGLFS
Qt for Linux/X11 — Building from Source
You can download the Qt sources from the Downloads page. For more information, visit the Getting Started with Qt page.
Qt for X11 has some requirements that are given in more detail in the Qt for X11 Requirements document.
Distribution packagers can find information how to package Qt in the packaging recommendations.
Step 1: Unpacking the Archive
Unpack the archive if you have not done so already. For example, if you have the qt-everywhere-opensource-src-%VERSION%.tar.gz package, type the following commands at a command line prompt:
This creates the directory /tmp/qt-everywhere-opensource-src-%VERSION% containing the files from the archive. We only support the GNU version of the tar archiving utility. Note that on some systems it is called gtar.
Step 2: Build the Qt Library
To configure the Qt library for your machine type, run the ./configure script in the package directory.
By default, Qt is configured for installation in the /usr/local/Qt-%VERSION% directory, but this can be changed by using the -prefix option.
The Configure Options page contains more information about the configure options.
The Configure Options page contains more information about the configure options.
To create the library and compile all the examples, tools, and tutorials, type:
If -prefix is outside the build directory, you need to install the library, examples, tools, and tutorials in the appropriate place. To do this (as root if necessary), type:
Note that on some systems the make utility is named differently, like gmake. The configure script tells you which make utility to use.
Note: Later, if you need to reconfigure and rebuild Qt from the same location, ensure that all traces of the previous configuration are removed. To do so, from the build directory, type make confclean before running configure again.
Step 3: Set the Environment Variables
To use Qt, some environment variables need to be extended.
This is done as follows:
In .profile (if your shell is bash, ksh, zsh or sh), add the following lines:
In .login (if your shell is csh or tcsh), add the following line:
If you use a different shell, modify your environment variables accordingly.
For compilers that do not support rpath you must also extend the LD_LIBRARY_PATH environment variable to include /usr/local/Qt-%VERSION%/lib . On Linux with GCC this step is not needed.
В© 2022 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.
Adding Compilers
Qt is supported on a variety of 32-bit and 64-bit platforms, and can usually be built on each platform with GCC, a vendor-supplied compiler, or a third party compiler. In Qt Creator, a kit specifies the compiler and other necessary tools for building an application for and running it on a particular platform.
Qt Creator automatically detects the compilers that are registered by your system or by the Qt Installer and lists them in Edit > Preferences > Kits > Compilers:
You can add the following compilers to build applications by using other compilers or by using additional versions of the automatically detected compilers:
- Clang is a C, C++, Objective C, and Objective C++ front-end for the LLVM compiler for Windows, Linux, and macOS.
- clang-cl is an alternative command-line interface to Clang that is compatible with the Visual C++ compiler, cl.exe .
- GNU Compiler Collection (GCC) is a compiler for Linux and macOS.
- ICC (Intel C++ Compiler) is a group of C and C++ compilers. Only the GCC-compatible variant, available for Linux and macOS, is currently supported by Qt Creator.
- MinGW (Minimalist GNU for Windows) is a native software port of GCC and GNU Binutils for use in the development of native Microsoft Windows applications on Windows. MinGW is distributed together with Qt Creator and Qt for Windows.
- MSVC (Microsoft Visual C++ Compiler) is a C++ compiler that is installed with Microsoft Visual Studio.
- Nim is the Nim Compiler for Windows, Linux, and macOS.
- QCC is the interface for compiling C++ applications for QNX.
In addition, the Qt Creator Bare Metal Device plugin provides support for the following compilers:
- IAREW is a group of C and C++ bare-metal compilers from the various IAR Embedded Workbench development environments.
Note: Currently supported architectures are 8051 , AVR , ARM , STM8 , and MSP430 .
Note: Currently supported architectures are 8051 and ARM .
Note: Currently supported architectures are 8051 and STM8 .
The emscripten compiler is tool chain for compiling to WebAssembly.
Redetecting Compilers
When Qt Creator finds an x86_64 GCC compiler, it sets up an instance for the native x86_64 target. If you plan to create also 32-bit x86 binaries without using a dedicated cross-compiler, select Auto-detection Settings > Detect x86_64 GCC compilers as x86_64 and x86. Then select Re-detect to refresh the list of automatically detected compilers.
To remove manually added compilers, select Remove or Remove All.
Specifying Compiler Settings
To build an application using GCC, MinGW, Clang, or QCC, specify the path to the directory where the compiler is located and select the application binary interface (ABI) version from the list of available versions. You can also create a custom ABI definition. For QCC, also specify the path to the QNX Software Development Platform (SDP) in the SPD path field.
To enable Microsoft Visual C++ Compilers (MSVC) and clang-cl to find system headers, libraries, and the linker, Qt Creator executes them inside a command prompt where the environment has been set up using vcvarsall.bat . For these compilers, you also specify the path to the script that sets up the command prompt in the Initialization field.
You specify the compiler to use for each kit in Edit > Preferences > Kits.
To add a C or C++ compiler, select Edit > Preferences > Kits > Compilers > Add. Select a compiler in the list, and then select C or C++.
To clone the selected compiler, select Clone.
The settings to specify depend on the compiler:
- In the Name field, enter a name for the compiler to identify it in Qt Creator.
Adding Nim Compilers
To build an application using the Nim Compiler, select Edit > Preferences > Kits > Compilers > Add > Nim, and specify the path to the directory where the compiler is located.
Adding Custom Compilers
To add a compiler that is not listed above or a remote compiler, use the Custom option and specify the paths to the directories where the compiler and make tool are located and options for the compiler.
To add other compilers:
- Select Edit >Preferences >Kits >Compilers >Add >Custom >C or C++.
- In the Name field, enter a name for the compiler.
- In the Compiler path field, enter the path to the directory where the compiler is located.
- In the Make path field, enter the path to the directory where the make tool is located.
- In the ABI field, specify the ABI version.
- In the Predefined macros field, specify the macros that the compiler enables by default. Specify each macro on a separate line, in the following format: MACRO[=value].
- In the Header paths field, specify the paths to directories that the compiler checks for headers. Specify each path on a separate line.
- In the C++11 flags field, specify the flags that turn on C++11 support in the compiler.
- In the Qt mkspecs field, specify the path to the directory where mkspecs are located. Usually, the path is specified relative to the Qt mkspecs directory.
- In the Error parser field, select the error parser to use. You can add custom output parsers to the list. For more information, see Using Custom Output Parsers.
Troubleshooting MinGW Compilation Errors
If error messages displayed in Compile Output contain paths where slashes are missing (for example, C:QtSDK ), check your PATH variable. At the command line, enter the following commands:
If these commands show paths, they have been added to the global PATH variable during the installation of a tool chain based on Cygwin or MinGW, even though this is against Windows conventions.
To keep working with the third-party tool chain, create a new shell link that adds the required paths (as Visual Studio and Qt do). The shell link must point to cmd.exe, as illustrated by the following example:
C:\Windows\System32\cmd.exe /K C:\path_to\myenv.bat
where the /K parameter carries out the command specified in the bat file.
Create the myenv.bat file at path_to, which should be in a convenient location. In the file, specify the paths to the tool chains. For example,
where path1 and path2 are paths to the tool chains.
Finally, remove the paths from the global PATH, reboot the computer, and run the where commands again to verify that the global PATH is now clean.
You can use the shell link to run the tools in the third-party tool chains.
В© 2022 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.
Configure an Embedded Linux Device
Cross-compiling Qt for a given device requires a toolchain and a sysroot. The toolchain is expected to contain a version of gcc, or another compiler, and associated tools built for cross-compilation. This means these tools run on the host system (typically x64), while producing binaries for the target architecture (for example, 32 or 64 bit ARM). The sysroot contains the headers and libraries for the target system, allowing compiling and linking libraries and applications on the host.
This overview page describes to the generic approach, where no distribution building systems, such as Yocto or Buildroot, are used. It is always possible to cross-compile and deploy Qt onto a device as long as a suitable toolchain and sysroot are available.
Warning: This page can only provide a generic, high-level overview. There are a vast number of details that can vary depending on the build environment, the target device, and the toolchain. When in doubt, refer to your system integrator. For pre-built reference images and SDKs, refer to the Boot to Qt offering.
When running Qt-based applications without a windowing system, such as X11 or Wayland, some devices require vendor-specific adaptation code for EGL and OpenGL ES support. This is provided in form of backends for the EGLFS platform plugin. This is not relevant for non-accelerated platforms, such as those that use the LinuxFB platform plugin, which is meant for software-based rendering only. As of Qt 6, many embedded systems use drm to set a video mode, manage display connectors and graphical surfaces. For example, an NXP i.MX8-based device or a Raspberry Pi 4 will use this approach, and therefore the most commonly used backend for EGLFS is eglfs_kms, which enables EGL and OpenGL ES based rendering with drm , using gbm for surface and buffer management. Older devices, such as the NXP i.MX6, will continue to use the legacy, GPU vendor-specific approach to connect EGL window surfaces to the framebuffer, using dedicated eglfs backends, such as eglfs_viv .
Note: Be aware that Qt is just one component in the software stack for an embedded device. Especially when accelerated graphics are involved, Qt expects a functional graphics stack, with an appropriate configuration for the userspace and kernel components, such as the display driver. These components are outside of Qt’s domain, and it is the system integrator’s responsibility to ensure the base system is fully functional and optimal, including accelerated graphics.
For further information on graphics and input configuration for Embedded Linux systems, refer to Qt for Embedded Linux.
Toolchain Files versus Device Makespecs
In Qt 5, you would typically use a device spec under the qtbase/mkspecs/devices directory. These contain the appropriate compiler and linker flags for a certain device, also making sure the correct EGL and OpenGL ES libraries are picked up, in case they are in a non-standard location in the sysroot.
For example, you could have configured a Qt 5 build for a Raspberry Pi 2 with a configure command like the following:
With Qt 6 and CMake, this approach is no longer sufficient on its own. Rather, a CMake toolchain file must be provided before configuring can happen. It is in this file where customization with regards to compiler and linker flags, and toolchain and sysroot specific quirks, happens.
In the below sections we will present a toolchain file that can be used in many cases, with minimal customization. It is based on the approach presented in this blog post.
Note: The toolchain file presented below is an example, that will often need further customization for a given device. Users and system integrators are also free to create their own toolchain files in any way they see fit.
While CMake is the only supported build system for building Qt itself, applications may still be built using qmake in Qt 6.0. In order to get a qmake setup that is functional with cross-compilation, one will need to specify some of the legacy arguments to CMake or to configure.
Host Tools
Cross-compiling Qt requires a host build of Qt being available. During the build, tools such as moc , rcc , qmlcachegen , qsb , and others, are invoked from there. For example, if one cross-compiles for ARM on an x64 machine, a local x64 build of the same Qt version must be made available first. The path to this Qt build will be passed to configure or cmake.
Configuring Qt
Let’s assume that the following are available:
- a toolchain and sysroot under $HOME/rpi-sdk ,
- a checkout of Qt, at minimum the qtbase module, under $HOME/qt-cross ,
- a host build of Qt in $HOME/qt-host .
In addition, the following must be decided before configuring:
- Where will the Qt build be installed on the local system once the build completes? In the example we will use $HOME/qt6-rpi .
- Where will the Qt build be deployed on the device? In the example we will use /usr/local/qt6 .
In the example we are going to use a Raspberry Pi 4 SDK (toolchain+sysroot) generated via Yocto, but the instructions here are completely generic, with no dependency on Yocto. The steps are the same with any other toolchain and sysroot, once the toolchain file is updated with the correct cross compiler and other paths.
After creating and switching to a build directory:
In practice this configure command is equivalent to the following direct CMake call:
Given the appropriate toolchain file, this is sufficient to generate a Qt build that then allows applications to be built using CMake. To enable applications to be built with qmake as well, the Qt 5 style device spec and device options must be specified, in addition to all arguments shown above:
By default, when cross-compiling, only the Qt libraries and tools that are supposed to run on the target device are built. Build-related tools like moc and uic are not built. Building such tools can be enabled by setting QT_FORCE_BUILD_TOOLS to ON .
Note: When QT_FORCE_BUILD_TOOLS is enabled, target binaries of tools like qmake will get installed to the staging location. Therefore, if qmake is used to build applications, call the host-qmake script instead.
Once configuration completes without errors, run cmake —build . —parallel to build. Once built, run cmake —install . to install the results to $HOME/qt6-rpi . From there the Qt build can be deployed to the device by using rsync, scp, or another method.
If building individual Qt modules, one can use the qt-configure-module script from the bin directory of the staging location ( $HOME/qt6-rpi in the example) to configure additional modules, such as qtdeclarative, qtquick3d, and so on. They can then be built using cmake —build . and installed to the staging location by running cmake —install .
Note: Before starting the build, always inspect the output of the configuration step carefully: does it have all the expected features enabled? Making a build and deploying it to the device is futile if essential features are not enabled at configuration time.
For example, when accelerated graphics via OpenGL is desired, pay extra attention to the following features:
With the Raspberry Pi 4 example, we expect that EGL, OpenGL ES and EGLFS GBM are all reported as yes , otherwise the EGLFS platform plugin and its eglfs_kms backend will not be functional on the device. For getting function mouse, keyboard, and touch input, either evdev or libinput must be enabled.
Similarly, if X11 is planned to be used as the (or one of the) windowing systems on the device, then ensure the xcb and X11 related features are marked as yes .
An Example Toolchain File
We will assume there is a sysroot and toolchain available under $HOME/rpi-sdk . TARGET_SYSROOT and CROSS_COMPILER must be adjusted to the toolchain and sysroot in use. The example here is only suitable for one specific, Yocto-generated SDK. The same is true for CMAKE_C_COMPILER and CMAKE_CXX_COMPILER .
We do not rely on any wrapper scripts that would provide environment variables such as PKG_CONFIG_*. Rather, the path to the .pc files is specified in the toolchain file. It is likely that another sysroot will need adjustments in PKG_CONFIG_LIBDIR . For example, with a sysroot generated from a Raspberry Pi OS (formerly Raspbian) image one would use /usr/lib/arm-gnueabihf/pkgconfig instead.
The compiler and linker flags are not necessary optimal in the example. Adjust them as necessary for the target device.
For further information on the CMake specifics in the example toolchain file, refer to this blog post and the CMake documentation.
Building Applications for the Target Device
Once the Qt build is done and installed to the staging location, examples or applications can be built.
With CMake, use the generated qt-cmake script in the bin directory of the staging location ( $HOME/qt6-rpi in the example) to configure, then run ninja . For example:
The resulting application binary can then be deployed to the device. Using the qt-cmake helper script is convenient, because the script ensures the toolchain file that was used for building Qt is loaded, so there is no need to repeatedly specify it for each application.
Unlike for Qt itself, building applications with qmake is still supported in Qt 6.0, as long as a suitable device spec is available, and the appropriate legacy arguments were passed to CMake or configure when configuring Qt. If this is all true, then running qmake and make will also generate an application binary for the target device.
Defaults for Platform Plugins and EGLFS
Once configured, a default platform plugin is chosen. This is used when launching an application without the -platform argument and without having the QT_QPA_PLATFORM environment variable set.
Similarly, the EGLFS platform plugin has multiple backends. The default is chosen based on availability and a pre-defined priority order. If drm and gbm are available, the default will be the eglfs_kms backend. This can always be overridden at runtime by setting the QT_QPA_EGLFS_INTEGRATION environment variable.
To change these defaults for the build, without having to force a specific value at run time, the following to CMake cache variables are available once CMake has been run once:
- QT_QPA_DEFAULT_PLATFORM ( STRING ) — The name of the default platform plugin.
- QT_QPA_DEFAULT_EGLFS_INTEGRATION ( STRING ) — The default EGLFS backend.
These variables can also be set inside the toolchain file.
For more information on configuring Qt, see Qt Configure Options.
В© 2022 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.