树莓派CM4(五):搭建QT开发环境

1. 软件版本

  • Ubuntu20.04
  • QT5.12.12

2. 参考链接

https://zhuanlan.zhihu.com/p/138021025?utm_id=0

https://zhuanlan.zhihu.com/p/137745265

https://bugreports.qt.io/browse/QTBUG-62216

https://www.tal.org/tutorials/building-qt-512-lts-raspberry-pi-raspberry-pi-os

https://wiki.qt.io/RaspberryPi2EGLFS

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

  • 下载链接

    https://download.qt.io/archive/qt/5.12/5.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交叉编译链

6. QT交叉编译及安装

  • 参考链接:

    https://zhuanlan.zhihu.com/p/461514256

    https://doc.embedfire.com/linux/rk356x/Qt/zh/latest/lubancat_qt/install/install_arm_2.html

    https://www.jianshu.com/p/659822ebc183

    https://zhuanlan.zhihu.com/p/138021025

  • 安装依赖

    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 typeDefault表示使用SSH连接

    • 点击Test测试连通性,测试通过后Apply

9. QT上以root权限run/debug

  • 工具 -> 选项 -> 环境 -> Terminal -> 选择使用gnome-terminal,后面加上参数-x sudo,然后Apply

  • 在运行设置中选择Run in termainal, 项目-> Run-> Run in terminal

posted @ 2024-10-29 11:34  zhijun  阅读(153)  评论(0编辑  收藏  举报