树莓派CM4(五):搭建QT开发环境
1. 软件版本
Ubuntu20.04
QT5.12.12
2. 参考链接
https://www.tal.org/tutorials/building-qt-512-lts-raspberry-pi-raspberry-pi-os
https://wiki.qt.io/Cross-Compile_Qt_6_for_Raspberry_Pi#Final_Configuration_on_Raspberry_Pi
https://www.tal.org/tutorials/building-qt-62-raspberry-pi-raspberry-pi-os
https://doc-snapshots.qt.io/qt6-dev/configure-linux-device.html
3. Ubuntu20.04安装Qt5.12.12
-
下载链接
-
将下载的
qt-opensource-linux-x64-5.12.12.run
拷贝到Ubuntu20.04
中 -
执行
qt-opensource-linux-x64-5.12.12.run
$ chmod a+x qt-opensource-linux-x64-5.12.12.run $ sudo ./qt-opensource-linux-x64-5.12.12.run
-
默认安装在
/opt
目录下,可自行更改,其它保持默认即可
-
选择组件,干脆全选得了
-
查看安装目录
-
在
/etc/profile
添加下面这行,修改PATH
环境变量#QT 5.12.12 export PATH=/opt/Qt5.12.12/Tools/QtCreator/bin:$PATH
-
使之立即生效
$ source /etc/profile
-
在终端启动
qtcreator
$ qtcreator &
4. 树莓派环境配置
-
官网下载镜像,我选择的是最新版无桌面环境的
Lite
镜像,下载链接如下:https://www.raspberrypi.com/software/operating-systems/#raspberry-pi-os-64-bit
-
镜像烧录,参考之前的文章
-
在
/boot/config.txt
中添加如下内容# 启用调试串口 enable_uart=1 # 启用DSI输出 dtoverlay=vc4-kms-dsi-7inch
- 进入树莓派系统,安装一些依赖的包,在此提供一个脚本来进行安装
- 有一项依赖
libxcb-xineramalibfontconfig1-dev
安装出错,但是看起来好像没什么影响 - 正常情况下,这个脚本不会报任何错误
- 有一项依赖
#!/bin/bash # #安装失败的包 # libxcb-xineramalibfontconfig1-dev # SOFT_LIST='build-essential libx11-dev libxcb1-dev libxcb-keysyms1-dev libxcb-image0-dev libxcb-shm0-dev libxcb-icccm4-dev libxcb-sync0-dev libxcb-xfixes0-dev libxcb-shape0-dev libxcb-randr0-dev libxcb-render-util0-dev libxcb-glx0-dev libxcb-present-dev libdbus-1-dev libfreetype6-dev libicu-dev libinput-dev libxkbcommon-dev libsqlite3-dev libssl-dev libpng-dev libjpeg-dev libglib2.0-dev libudev-dev libts-dev libmtdev-dev libfontconfig1-dev libglib2.0-dev libgles2-mesa-dev libgbm-dev libdrm-dev unzip pkg-config gfortran qt5-qmake libqt5gui5 libqt5webengine-data libqt5webkit5 libxcb-xinerama0-dev libxcb-xinerama0 gdbserver libxcb-randr0-dev libxcb-xtest0-dev libxcb-shape0-dev libxcb-xkb-dev libv4l-dev libxvidcore-dev libwayland-dev libbluetooth-dev libwayland-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-plugins-bad libgstreamer-plugins-bad1.0-dev gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-alsa ttf-wqy-zenhei gdbserver libpq-dev libx11-dev libxcb1-dev libxext-dev libxi-dev libxcomposite-dev libxcursor-dev libxtst-dev libxrandr-dev libfontconfig1-dev libfreetype6-dev libx11-xcb-dev libxext-dev libxfixes-dev libxi-dev libxrender-dev libxcb-glx0-dev libxcb-keysyms1-dev libxcb-image0-dev libxcb-shm0-dev libxcb-sync-dev libxcb-shape0-dev libxcb-randr0-dev libxkbcommon-x11-dev libzstd-dev libsctp-dev perl libudev-dev libxcb-xinerama0 flex bison gperf libxslt-dev ruby libssl-dev libxcursor-dev libxcomposite-dev libxdamage-dev libxrandr-dev libfontconfig1-dev libcap-dev libxtst-dev libpulse-dev libudev-dev libpci-dev libnss3-dev libasound2-dev libxss-dev libbz2-dev libcups2-dev libatkmm-1.6-dev ttf-mscorefonts-installer ' for i in ${SOFT_LIST} do echo -e "[INFO] ${i} installing..." apt-get install -y $i if [ $? -ne 0 ]; then echo -e "[ERROR] ${i} install failed!" exit 1 fi done
- 进入树莓派系统,安装一些依赖的包,在此提供一个脚本来进行安装
5. Ubuntu20.04环境配置
5.1 构建sysroot
- 在
Ubuntu
中创建sysroot
目录$ mkdir -p raspi/sysroot $ cd raspi
- 同步树莓派中的库及头文件到
sysroot
$ rsync -avz pi@192.168.16.9:/lib sysroot $ rsync -avz pi@192.168.16.9:/usr/include sysroot/usr $ rsync -avz pi@192.168.16.9:/usr/lib sysroot/usr
- 添加
opengl
相关库# 下载树莓派固件 $ curl -L https://github.com/Hexxeh/rpi-firmware/archive/master.tar.gz -o master.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 121M 0 121M 0 0 8011k 0 --:--:-- 0:00:15 --:--:-- 8348k # 解压 $ tar -xvf master.tar.gz # 复制rpi-firmware-master/vc/softfp/opt到sysroot $ cp -r rpi-firmware-master/vc/hardfp/opt ./sysroot # 修改sysroot中的软连接 $ cd sysroot/ sysroot$ rm usr/lib/aarch64-linux-gnu/libEGL.so.1.1.0 sysroot$ ln -s opt/vc/lib/libEGL.so usr/lib/aarch64-linux-gnu/libEGL.so.1.1.0 sysroot$ ln -s opt/vc/lib/libGLESv2.so usr/lib/aarch64-linux-gnu/libGLESv2.so.2.0.0 sysroot$ rm opt/vc/lib/libEGL.so sysroot$ ln -s opt/vc/lib/libbrcmEGL.so opt/vc/lib/libEGL.so sysroot$ rm opt/vc/lib/libGLESv2.so sysroot$ ln -s opt/vc/lib/libbrcmGLESv2.so.2 opt/vc/lib/libGLESv2.so sysroot$ ln -s opt/vc/lib/libEGL.so opt/vc/lib/libEGL.so.1 sysroot$ ln -s opt/vc/lib/libGLESv2.so opt/vc/lib/libGLESv2.so.2
- 修复软连接
$ wget https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py $ chmod a+x sysroot-relativelinks.py $ ./sysroot-relativelinks.py ./sysroot
5.2 安装树莓派CM4交叉编译链
-
参考链接:
-
实测发现,交叉编译工具链版本也会导致编译失败,因此先查看一下树莓派中的gcc版本为
12.2.0
-
到
linaro
官网下载对应版本的交叉编译工具链https://snapshots.linaro.org/gnu-toolchain/12.2-2023.04-1/aarch64-linux-gnu/
-
安装交叉编译工具链到Ubuntu20.04,过程略
6. QT交叉编译及安装
-
参考链接:
https://doc.embedfire.com/linux/rk356x/Qt/zh/latest/lubancat_qt/install/install_arm_2.html
-
安装依赖
sudo apt-get install -y libx11-* libx11* libxcb-* libxcb* libxkbcommon-x11-dev libxcb-xinerama0-dev libxcb-sync-dev
-
下载QT源码:
QT5.12.9
-
解压
$ tar -xvf qt-everywhere-src-5.12.9.tar.xz
-
将
qtbase/mkspecs/devices/linux-rasp-pi3-v3d-g++
复制一份,重命名为linux-rasp-pi4-v3d-g++
$ cp -rf qtbase/mkspecs/devices/linux-rasp-pi3-v3d-g++ qtbase/mkspecs/devices/linux-rasp-pi4-v3d-g++
-
修改
qtbase/mkspecs/devices/linux-rasp-pi4-v3d-g++/qmake.conf
文件- 删除
-mfpu=crypto-neon-fp-armv8
- 将
include(../common/linux_arm_device_post.conf)
修改为include(../common/linux_device_post.conf)
- 删除
-
编写自动配置脚本
autoconfigure.sh
,内容如下:# 目标平台,这个平台可以在qtbase/mkspecs目录下查找 HOST=linux-aarch64-gnu # sysroot路径 SYSROOT_PATH=/home/jun/work/share/qt-workspace/raspi/sysroot # 指定目标平台设备 DEVICE=linux-rasp-pi4-v3d-g++ # 交叉编译工具链 CROSS_CHAIN_PREFIX=/home/jun/work/share/gcc-linaro-12.2.1-2023.04-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu- #CROSS_CHAIN_PREFIX=/usr/local/arm/raspi/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- # 运行目录 PREFIX=/usr/local/qt5pi # 库及工具的安装目录 EXTPREFIX=/home/jun/work/share/qt-workspace/qt5pi #-hostprefix /home/jun/work/share/qt-workspace/qt5pi-host ./configure \ -release \ -device ${DEVICE} \ -device-option CROSS_COMPILE=${CROSS_CHAIN_PREFIX} \ -sysroot ${SYSROOT_PATH} \ -prefix ${PREFIX} \ -extprefix ${EXTPREFIX} \ -opengl es2 \ -opengles3 \ -no-gtk \ --disable-d3d12 \ -force-pkg-config \ -opensource \ -confirm-license \ -reduce-exports \ -nomake examples \ -no-compile-examples \ -nomake tests \ -skip qtwayland \ -skip qtwebengine \ -qt-pcre \ -no-pch \ -ssl \ -evdev \ -make libs \ -system-freetype \ -fontconfig \ -glib \ -no-feature-eglfs_brcm \ -recheck-all \ -v
● 执行自动配置脚本autoconfigure.sh
qt-everywhere-src-5.12.9$ chmod a+x autoconfigure.sh
qt-everywhere-src-5.12.9$ ./autoconfigure.sh
● 编译
$ make -j$(nproc)
● 编译时报错
-
解决方法:
在qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h
开头包含头文件<limits>
-
make install
安装$ make install -j8
-
查看编译输出目录
-
将该目录打包,发送到树莓派CM4
$ cd .. $ tar -cvzf qt5pi.tar.gz qt5pi/ $ scp qt5pi.tar.gz pi@192.168.16.9:/tmp
7. 设置树莓派CM4 QT运行环境
- 将上一步中得到的
qt5pi.tar.gz
解压到/usr/local
目录# tar -xvf qt5pi.tar.gz -C /usr/local
- 编辑
/etc/profile
设置环境变量,在其中添加如下内容export PATH=$PATH:/opt export QT_QPA_EGLFS_WIDTH=800 export QT_QPA_EGLFS_HEIGHT=480 export QTDIR=/usr/local/qt5pi export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PA export QT_QPA_FONTDIR=$QTDIR/lib/fonts export QT_QPA_PLATFORM_PLUGIN_PATH=$QTDIR/plugins/ export QT_QPA_PLATFORM=eglf export QT_QPA_EGLFS_HIDECURSOR=1 export QT_QPA_EGLFS_ALWAYS_SET_MODE="1" export QWS_MOUSE_PROTO=/dev/input/event0 export QML_IMPORT_PATH=$QTDIR/qml export QML2_IMPORT_PATH=$QTDIR/qml export QT_QPA_PLATFORM=linuxfb:/dev/fb0
- 使之立即生效
# source /etc/profile
8. QT Creator开发环境设置
8.1 添加kits
-
Tools-> Options -> Kits
-
Manual-> Add
- 设置新添加的
Kits
名称,选择Device type为 Generic Linux Device
- 设置新添加的
-
设置
Qt Version
,就是指定交叉编译使用的qmake
所在路径,前文已经说明了在QT交叉编译时,执行make install
将编译产物安装到了/home/jun/work/share/qt-workspace/qt5pi
目录,qmake
就在该目录下
-
添加C编译器,即指定交叉编译工具链:
Kits-> Compilers-> Manual-> Add-> GCC-> C
- 自定义
C Compilers
名称为RaspberryCM4-GCC
Compiler path
设置为交叉编译工具链aarch64-none-linux-gnu-gcc
的绝对路径Apply
应用
- 自定义
-
添加C++编译器: Kits-> Compilers-> Manual-> Add-> GCC-> C++
- 设置Name为RaspberryCM4-G++
- 设置Compiler path为aarch64-none-linux-gnu-g++的绝对路径
- Apply应用
-
返回
kits
界面,设置C Compiler
为上一步添加的RaspberryCM4-GCC
-
Kits-> RaspberryCM4-> Compiler-> C-> RaspberryCM4-GCC
-
设置
C++ Compiler
为上一步添加的RaspberryCM4-G++
Kits-> RaspberryCM4-> Compiler-> C++-> RaspberryCM4-G++
-
别忘了
Apply
应用
8.2 为项目选择Kits
Project -> RaspberryCM4 -> Build
8.3 远程部署调试
-
Tools-> Options-> Devices-> Add-> Generic Linux Device
-
Connection
设置
-
Key Deployment
设置,这一步先不设置,后续使用SSH连接,直接next
-
Finish
-
回到
Device
,选择Authentication type
为Default
表示使用SSH连接- 点击
Test
测试连通性,测试通过后Apply
- 点击
9. QT上以root
权限run/debug
-
工具 -> 选项 -> 环境 -> Terminal -> 选择使用gnome-terminal
,后面加上参数-x sudo
,然后Apply
-
在运行设置中选择
Run in termainal
,项目-> Run-> Run in terminal