Reducing Binary Size of Qt Applications with Qt 6.8 - Part 2: Coffee Machine example

Are you looking to optimize your Qt applications for better performance and efficiency? Look no further: Qt Configure Options in Qt 6.8 can help you with this.

With the ability to selectively enable or disable features and modules in the Qt framework, developers can deliver applications in smaller packages, reducing RAM and ROM footprints, and achieving faster startup times.

In this blog post, we will walk through the process of cross-compiling Qt Configure Options for the Raspberry Pi 4 target device. By following the outlined steps you can tailor your applications to specific needs and experience the benefits of Qt Configure Options firsthand.

 

Qt Configure Options and cross-compilation for Raspberry Pi 4

Let's walk through an example of cross-compiling Qt Configure Options for Raspberry Pi 4 target device.

1. First, download the Qt cross-compilation SDK, which provides the necessary toolchain for the target device. You can find more information on Boot2Qt Raspberry Pi pages. Also, make sure to download the corresponding version of the host SDK along with its source code using the same method.

2. Download the qtlite_coffeemachine.txt file, which includes a customized feature configuration, and the toolchain_rpi4.cmake file, which outlines the build target compilation and linker parameters.

3. Configure Qt with the necessary modules and features. In this example we use a feature set which allows the Coffee Machine example to compile and run. 

cd $HOME/Qt/6.8.0/Src
./configure -qt-host-path $HOME/Qt/6.8.0/gcc_64 -extprefix $HOME/Qt/my_rpi4_sdk -release -optimize-size -static -ltcg -reduce-exports -gc-binaries $(cat $HOME/qtlite_coffeemachine.txt) -- -DCMAKE_TOOLCHAIN_FILE=$HOME/toolchain_rpi4.cmake

4. Finally, build and install SDK to to $HOME/Qt/my_rpi4_sdk

cmake --build . --parallel
cmake --install .

For more detailed instructions, refer to the Raspberry Pi 4 wiki page and other provided links and documentation.

 

Building the Coffee Machine Example

Import the customized SDK into Qt Creator and open the Coffee Machine example from the welcome page. Then, incorporate the following CMake commands into the main CMakeLists.txt to further optimize the binary size.

set_target_properties(
        coffeemachine
    PROPERTIES
        QT_QMLCACHEGEN_ARGUMENTS "--only-bytecode"
)

The final step involves removing all textual symbols:

aarch64-linux-gnu-strip -s coffeemachine

Ultimately, users will notice a reduction in RAM and ROM usage, as well as quicker startup times.

 

Observing results

For comparison, we utilized the vanilla 6.8.0 Beta4 release. Dynamic results were obtained using unmodified binaries, while static libraries were generated with the following configure command:

./configure -qt-host-path $HOME/Qt/6.8.0/gcc_64 -extprefix $HOME/Qt/6.8.0_static -release -static -skip qtapplicationmanager -qpa eglfs -- -DCMAKE_TOOLCHAIN_FILE=$HOME/toolchain_rpi4.cmake

Optimized results were achieved through modifications in both the Qt framework and the application build process as described in previous chapter. Additionally, all debug information was stripped from the optimized application.

Memory Footprint in ROM

Memory Footprint in RAM

Application startup time

Fine Tuning

Developers can further reduce the binary size by eliminating features such as QML Effects and QML Controls Fusion-style. Removing Effects allows for the utilization of the linuxfb backend instead of eglfs, resulting in an additional 30% reduction in binary size. 

Conclusion

Qt 6.8's Configure Options empower developers to fine-tune their applications for performance and efficiency, precisely catering to their specific requirements. By toggling features and components within the Qt framework, developers can craft leaner applications that slash ROM usage by up to 77%, trim RAM consumption by 32%, and launch significantly faster.

This post is about leveraging the Qt Configure Options , which can provide tangible reduction of the binary size and related resource consumption. Optimizing the graphical assets as well as the application code itself will also provide improvements in the resource consumption, but for the measurements shown in this blog post we did not adjust those. When seeking to optimize an application, configuring your Qt binaries is one of the things you can do. 

Cross-compiling Qt with configure options for Raspberry Pi 4 is made easier with the necessary toolchain and configuration files. To further explore the benefits of Qt Configure Options and how to implement it in your projects, take a deeper dive into the provided links and documentation.

 


Blog Topics:

Comments