How to Cross-Compile and Port Qt on the OKMX6ULx Linux System?

Qt is a cross-platform C++ graphical user interface development framework that supports desktop, embedded, and mobile platforms. It is renowned for its efficient signal-and-slot mechanism, hardware-accelerated rendering, and a rich set of modular toolchains. Qt is widely used in fields such as industrial control, in-vehicle systems, and smart homes.

1. Source Code Download

Qt 5.12 can be downloaded from the official Qt website or GitHub, among other sources.

2. Source Code Configuration and Modification

Unzip the source code
forlinx@ubuntu:~/111/qt5.12$ unzip qtsrc512-master.zip 
forlinx@ubuntu:~/111/qt5.12$ cd qtsrc512-master/
forlinx@ubuntu:~/111/qt5.12$ chmod 777 -R qtsrc512-master    Grant maximum permissions to the source code (otherwise, some files may lack execution permissions during compilation, leading to errors).

Modify the qmake configuration file to cross-compile to the QT of the arm architecture

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$vi qtbase/mkspecs/linux-arm-gnueabi-
g++/qmake.conf

Modifications are:

#
# qmake configuration for building with arm-linux-gnueabi-g++
#
MAKEFILE_GENERATOR      = UNIX
CONFIG                 += incremental
QMAKE_INCREMENTAL_STYLE = sublib
include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)
# modifications to g++.conf
QMAKE_CC   = arm-poky-linux-gnueabi-gcc -march=armv7ve -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-2.0.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi
QMAKE_CXX  =arm-poky-linux-gnueabi-g++ -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7--sysroot=/opt/fsl-imx-x11/4.1.15-2.0.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi
QMAKE_LINK =arm-poky-linux-gnueabi-g++ -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7--sysroot=/opt/fsl-imx-x11/4.1.15-2.0.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi
QMAKE_LINK_SHLIB =arm-poky-linux-gnueabi-g++ -march=armv7ve -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-2.0.0/sysroots
/cortexa7hf-neon-poky-linux-gnueabi
QMAKE_CFLAGS   = -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard
QMAKE_CXXFLAGS  = -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard
QMAKE_LDFLAGS    = -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed  -Wl,-z,relro,-z,now
# modifications to linux.conf
QMAKE_AR                = arm-poky-linux-gnueabi-ar cqs
QMAKE_OBJCOPY           = arm-poky-linux-gnueabi-objcopy
QMAKE_NM                = arm-poky-linux-gnueabi-nm -P
QMAKE_STRIP             = arm-poky-linux-gnueabi-strip
load(qt_config)

arm-poky-linux-gnueabi-gcc:Specify the compiler.

-march=armv7ve:Specify the target architecture as ARMv7 and enable virtualization extensions.

-mfpu=neon:Enable the NEON SIMD instruction set.

-mfloat-abi=hard:Enable hardware floating-point operations

-mcpu=cortex-a7:Optimize code for the Cortex-A7 processor.

--sysroot:Specify the root directory of the target system, including header and library files.

-O2:Enable medium optimization to improve the execution efficiency of the program.

-O1:Optimize the link process and improve the link efficiency.

--hash-style=gnu:Use a more efficient symbol hash table to improve loading speed.

--as-needed:Reduce unnecessary links to shared libraries and reduce the size of binary files.

-z relro:Enable read-only relocations to improve security and prevent certain attacks.

-z now:Instantly resolve all dynamic symbols, improving security and startup performance.

3. Cross Compilation

The configure file used to generate the Makefile requires many parameters, so it needs to write a script to save these parameters.

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ vi shell.sh
forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ chmod 777 shell.sh (After creating a new script, press: wq to save and exit, and give a permission, otherwise it cannot be executed)

The following is the contents of the shell. sh script (the path after -prefix needs to be changed to your own)

./configure -prefix /home/forlinx/111/qt5.12/qtsrc512-master/__install \
-opensource \
-confirm-license \
-release \
-strip \
-shared \
-xplatform linux-arm-gnueabi-g++ \
-optimized-qmake \
-c++std c++11 \
-I /home/forlinx/111/qt5.12/libxkbcommon-master/include \
--rpath=no \
-pch \
-skip qt3d \
-skip qtactiveqt \
-skip qtandroidextras \
-skip qtcanvas3d \
-skip qtconnectivity \
-skip qtdatavis3d \
-skip qtdoc \
-skip qtgamepad \
-skip qtlocation \
-skip qtmacextras \
-skip qtnetworkauth \
-skip qtpurchasing \
-skip qtremoteobjects \
-skip qtscript \
-skip qtscxml \
-skip qtsensors \
-skip qtspeech \
-skip qtsvg \
-skip qttools \
-skip qttranslations \
-skip qtwayland \
-skip qtwebengine \
-skip qtwebview \
-skip qtwinextras \
-skip qtxmlpatterns \
-make libs \
-make examples \
-nomake tools -nomake tests \
-gui \
-widgets \
-dbus-runtime \
--glib=no \
--iconv=no \
--pcre=qt \
--zlib=qt \
-no-openssl \
--xcb=qt \
--freetype=qt \
--harfbuzz=qt \
-no-opengl \
--libpng=qt \
--libjpeg=qt \
--sqlite=qt \
-plugin-sql-sqlite \
-recheck-all

Set Environment Variables:

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ ./shell.sh

Executing the script may report the following error:

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ ./shell.sh 
+ cd qtbase
+ /home/forlinx/111/qt5.12/qtsrc512-master/qtbase/configure -top-level -prefix /home/forlinx/111/qtsrc512-master/512qt -opensource -confirm-license -release -strip -shared -xplatform linux-arm-gnueabi-g++ -optimized-qmake -c++std c++11 --rpath=no -pch -skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtconnectivity -skip qtdatavis3d -skip qtdoc -skip qtgamepad -skip qtlocation -skip qtmacextras -skip qtnetworkauth -skip qtpurchasing -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtwayland -skip qtwebengine -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -make libs -make examples -nomake tools -nomake tests -gui -widgets -dbus-runtime --glib=no --iconv=no --pcre=qt --zlib=qt -no-openssl --xcb=qt --freetype=qt --harfbuzz=qt -no-opengl --libpng=qt --libjpeg=qt --sqlite=qt -plugin-sql-sqlite -recheck-all
Please make sure to unset the QMAKESPEC, XQMAKESPEC, QMAKEPATH,
and QMAKEFEATURES environment variables prior to building Qt.

This is because there is already a QT environment in Ubuntu. If you need to solve the problem, you can refer to the prompt to execute:

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ unset QMAKESPEC XQMAKESPEC QMAKEPATH QMAKEFEATURES

And then proceed to execute the shell. sh script

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ ./shell.sh
forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ make
forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ make install

After compilation, _ _ install is the required qt5.12 file

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master$ cd __install/
forlinx@ubuntu:~/111/qt5.12/qtsrc512-master/__install$ ls
bin  doc  examples  include  lib  mkspecs  plugins  qml

The lib path is the required lib library and the examples are some demo projects.

4. Function Test

The entire _ _ install folder is copied to the board for testing.

Method 1: Copy with network

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master/__install$ scp -r * [email protected]:/			//Network copy requires the computer and the board to be connected by a network cable.

Method 2: Pack the folder and copy it to the board

forlinx@ubuntu:~/111/qt5.12/qtsrc512-master/__install$ tar -cjvf qt5.12.tar.bz2 *

Unzip on the board after packing

root@fl-imx6ull:~# tar -xvf qt5.12.tar.bz2 -C /

Execute the environment variable in the board, where export QT _ ROOT =/is the path related to the qt5.12 file (fill in the unzipped path)

export QT_ROOT=/					//
export QT_QPA_FONTDIR=/usr/share/fonts
export QT_QPA_PLATFORM_PLUGIN_PATH=$QT_ROOT/plugins
export QT_QPA_PLATFORM=linuxfb:tty=/dev/fb0		//Use the Linux FB display box
export QT_PLUGIN_PATH=$QT_ROOT/plugins
export LD_LIBRARY_PATH=$QT_ROOT/lib:$QT_ROOT/plugins/platforms
export QML2_IMPORT_PATH=$QT_ROOT/qml

Run the demo

root@fl-imx6ull:~# /examples/charts/audio/audio

The above test uses the Linuxfb display framework. If you need the X11 display framework, you can use the environment variables displayed by the framework:

export QT_QPA_PLATFORM=xcb:tty=/dev/fb0				//Use X11 display frame

Note:

1. All the above operations are tests conducted in the Qt file system. If you want to use the X11 display in the console file system, you need to port X11 or copy the X11-related files from the Qt file system. If you want to use touch functionality in the console file system, you need to port tslib or add the path of tslib library files and header files to the configure parameters during the Qt cross - porting process;

2. You can delete the non - essential files to save the flash space on the board. For example, the demo projects under the ''examples'' directory can be deleted;

3. If you encounter display issues when running an application, you need to add the environment variable: export DISPLAY=:0.0




Dear friends, we have created an exclusive embedded technical exchange group on Facebook, where our experts share the latest technological trends and practical skills. Join us and grow together!