A minimal Qt5 application
We’ll call our application qt-sensor-demo
, so create a directory with this name, outside of Buildroot. It’s important to not mix up your application code with your build system: you could very well decide to use another build system one day, while keeping your application code. To keep things simple, create this qt-sensor-demo
side-by-side with Buildroot, as this will be important for a future step in this blog post.
In this directory, create a main.cpp
file with the following code:
#include <QApplication> #include <QPushButton> int main(int argc, char* argv[]) { QApplication app(argc, argv); QPushButton hello("Hello world!"); hello.resize(100,30); hello.show(); return app.exec(); }
Tt should be fairly straight-forward to understand that this program creates a QApplication object, a push button with the Hello world! label, sets the button size to 100 by 30 pixels, shows the button, and enters the application event loop. It is obviously a very basic application, because it doesn’t do anything useful, but that’s good enough as a starting point.
Now, we need to build this application. Building Qt applications by hand is definitely not reasonable, as Qt may need to run several tools on the source code before it gets built, and requires a number of compiler and linker flags. So, we won’t write a Makefile by hand, but instead use a build tool that generates the Makefile for us. We have a number of options here:
- qmake, the build tool provided by Qt itself
- cmake, with its Qt5 integration
- autotools, with the ax_have_qt macro from autoconf-archive
- meson with its Qt5 module
In this blog post, we’ll simply stick to qmake, which is good enough for a number of Qt-based applications. qmake takes as input one or several .pro
files describing the project, and uses that to generate Makefiles (on Linux systems).
In our case, the qt-sensor-demo.pro
file will be as simple as:
QT += widgets SOURCES = main.cpp
Building our application
We have two ways to build our application:
- Manually outside of Buildroot. In this case, we’ll use the Buildroot-provided compiler and tools, but we will trigger the build of our application separately from the Buildroot build.
- Using Buildroot. In this case, our application would have a corresponding Buildroot package, that would automate building the application as part of the complete system build process.
Ultimately, we definitely want to have a Buildroot package for our application, to make sure the entire build is fully automated. However, during the active development of the application, it may be useful to build it manually outside of Buildroot, so we are going to see both solutions, which are not mutually exclusive: you can have a Buildroot package for your application, and still build it manually when you’re doing active development/debugging.
Building manually outside of Buildroot
To build manually, we simply need to first invoke Buildroot’s provided qmake
:
/path/to/buildroot/output/host/bin/qmake
This will generate a Makefile
, that we can use to build our application:
make
At this point, you should have:
$ ls main.cpp main.o Makefile qt-sensor-demo qt-sensor-demo.pro
The qt-sensor-demo
executable is compiled for ARM, and linked against the various libraries built by Buildroot.
Now, we need this executable on our STM32MP15 target. For now, we’ll simply add it to the SD card image:
cp qt-sensor-demo /path/to/buildroot/output/target/usr/bin/ cd /path/to/buildroot/ make
This will copy the executable to the output/target
folder, which contains the root filesystem produced by Buildroot. Then invoking Buildroot’s make
will ensure that the root filesystem and SD card images get re-generated. Of course, beware that if you run a Buildroot make clean
, all the contents of output/
, including output/target/
get removed. So this technique is only suitable for temporary changes. This is fine since anyway as discussed above, ultimately we’ll have a proper Buildroot package to build our qt-sensor-demo application.
Reflash your SD card with the new image, and on the target, run the demo:
# qt-sensor-demo -platform linuxfb