在树莓派4上安装编译Qt5和PyQt5

需要使用qt5的eglfs(没有x11)的环境下运行Qt5和PyQt5,决定手动编译(直接apt-get应该也可以),走了不少弯路,也花了不少时间(编译时间漫长)。
期间也使用过yocto生成的镜像,也可以完美运行,但apt-get没有,对于开发环境还不适合,yocto适合最终环境,参考文档:https://jumpnowtek.com/rpi/Raspberry-Pi-Systems-with-Yocto.html
也走了不少弯路,因为第一次接触yocto。
一、安装依赖
1.安装依赖包:

sudo apt install libglu1-mesa libglu1-mesa-dev build-essential libgl1-mesa-dev libegl1-mesa-dev freeglut3 freeglut3-dev mesa-common-dev libglapi-mesa libosmesa6 mesa-utils libdrm-dev libgbm-dev libgbm1  libgegl-dev mesa-utils-extra gegl libglfw3-dev libgles2-mesa-dev  libglew-dev libgl1-mesa-glx libssl-dev libfontconfig1-dev libclang-dev libjpeg-dev libjpeg62-turbo-dev libxcb-xfixes0-dev

2.opengl:eglfs-kms环境测试:

sudo apt install mesa-utils 
kmscube -D /dev/dri/card1

如果看到旋转的立方体,基本就成功了。

OpenGL ES 2.x information:
  version: "OpenGL ES 3.1 Mesa 19.3.2"
  shading language version: "OpenGL ES GLSL ES 3.10"
  vendor: "Broadcom"
  renderer: "V3D 4.2"#可以看出是使用了v3d,也就是/dev/dri/card1,/dev/dri/card0为vc4

二、编译:
使用:linux-g++ 和qt-everywhere-src-5.12.4版本
参考并修改:https://www.tal.org/tutorials/building-qt-512-raspberry-pi
下载:qt-everywhere-src-5.12.4.tar.xz并解压–(参考文档中使用的是5.12.7)
下载:

git clone https://github.com/oniongarlic/qt-raspberrypi-configuration.git

执行:

cd qt-raspberrypi-configuration && make install DESTDIR=../qt-everywhere-src-5.12.4

我的树莓派4系统环境:2020-02-13-raspbian-buster-lite.img,将源码全部放在了u盘(ext4格式)中编译,挂载到/media/pi目录下。


PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig \
../qt-everywhere-src-5.12.4/configure -platform linux-rpi4-v3d-g++ \
-v \
-opengl es2 -eglfs -kms -gbm \
-no-gtk -no-pch -no-xcb -no-xcb-xlib \
-opensource -confirm-license -release \
-reduce-exports \
-force-pkg-config \
-nomake examples -no-compile-examples \
-qt-pcre \
-no-pch \
-evdev \
-system-freetype \
-glib \
-prefix /opt/Qt5.12 \
-qpa eglfs \
-skip qtwayland \
-skip qtscript \
-skip qtwebengine \
-skip qtlocation

查看重要的输出配置:

  EGLFS .................................. yes
  EGLFS details:
    EGLFS EGLDevice ...................... yes
    EGLFS GBM ............................ yes
    EGLFS Raspberry Pi ................... no  #树莓派4下一定要为no,是针对树莓派3以下的版本使用bcrm的gl模块编译

我认为上面3个都必须要有,前面gbm为no,开启eglgs一直不成功。
下面开始漫长的编译,使用4个线程,大概使用了6个小时。注意cpu的温度。

make -j4  (我使用 sudo make -j4   有些目录因为使用sudo make install的时候二次编译生成的)
sudo make install

三、配置qt环境

sudo nano /etc/profile

添加:

export LD_LIBRARY_PATH=/opt/Qt5.12/lib/
export PATH=/opt/Qt5.12/bin:$PATH
export QTDIR=/opt/Qt5.12
export QT_INSTALL_PLUGINS=/opt/Qt5.12/plugins
source /etc/profile

四、测试
如果出现错误看第五部分的配置
1.qml测试:
main.qml

import QtQuick 2.0

Rectangle {
   
    id: page
    width: 320; height: 480
    color: "lightgray"

    Text {
   
        id: helloText
        text: "Hello world!"
        y: 30
        anchors.horizontalCenter: page.horizontalCenter
        font.pointSize: 24; font.bold: true
    }
}

执行:qmlscene main.qml
2.cpp测试:
编写HelloWorld.cpp文件

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
   
    QApplication app(argc, argv);

    QLabel label("Hello, world");
    label.show();

    return app.exec();
}
qmake -project , 生成一个与平台无关的HelloWorld.pro文件
qmake HelloWorld.pro , 生成一个与平台无关的Makefile文件
make , 构建程序
最后执行 ./HelloWorld
出现错误:HelloWorld.cpp:1:10: fatal error: QApplication: No such file or directory
在项目文件.pro底部加上QT += widgets即可。

五、修改配置:(重要的)
1.添加pi到render组中:不添加的话会使用软件渲染(Running on a software rasterizer (LLVMpipe))

sudo usermod -a -G render pi

2.出现drmModeGetResources failed (Invalid argument),是因为使用了card0
新建eglfs.json 内容:

{
    "device": "/dev/dri/card1" } 

或者(没有测试)

{
   
 "device": "/dev/dri/card0",
 "hwcursor": true,
 "pbuffers": true,
 "outputs": [
  {
   
   "name": "HDMI1", "mode": "1024x768"
  }
 ]
}
export QT_QPA_EGLFS_KMS_CONFIG=/opt/Qt5.12/eglfs.json

同时注意:config.txt中开启:

[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2
[all]
dtoverlay=vc4-fkms-v3d

3.调试信息:在跑Qt程序之前执行下面的环境变量,可以打印一些log,并指定显示后端为eglfs

export QT_QPA_PLATFORM=eglfs
export QT_LOGGING_RULES=qt.qpa.*=true
export QT_QPA_EGLFS_DEBUG=1
export QT_QPA_EGLFS_ALWAYS_SET_MODE=1
export QT_QPA_EGLFS_INTEGRATION=eglfs_kms

#选择平台:eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb
export QT_QPA_PLATFORM=linuxfb
export QT_QPA_PLATFORM=linuxfb:fb=/dev/fb0:size=800x600:mmSize=800x600:offset=0x0:tty=/dev/tty1

4.注意事项:
检查有没有v3d_dir.so 和 vc4_dri.so
MESA-LOADER: failed to open vc4 (search paths /usr/lib/dri)
/lib/modules/4.X.X.X/kernel/drivers/gpu/drm see if you have drm.ko, v3d/v3d.ko and vc4/vc4.ko (otherwise it means some mesa drivers are missing) /usr/lib/dri see if you have v3d_dir.so and vc4_dri.so,

六、安装pyqt5
1.安装pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py   # 下载安装脚本
sudo python3 get-pip.py    # 运行安装脚本

2.编译pyqt5:
pip安装方式可以自动编译安装,采用源码方式麻烦,需先安装sip再编译,一直没有成功。
采用pip安装很慢,这里用的python3.7版本。
其实pip安装也是源码编译方式,通过ps-x查看,在/tmp目录下编译,
比如:/bin/sh-ccd QtCore/ && ( test -e Makefile || /opt/Qt5.12/bin/qmake -o Makefile /tmp/tmp0iv76kzs/QtCore/QtCore.pro ) && make -f Makefile
可以看到tmp临时编译目录,并且可以看到 -I /opt/Qt5.12/include 等编译选项,说明pyqt5编译使用了上面编译好的qt5.

sudo apt-get install python3-dev
pip install PyQt5
sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ PyQt5

输出信息:
Installing build dependencies … done #编译安装依赖成功,应该就是sip
Getting requirements to build wheel … done
Preparing wheel metadata … / #开始编译安装pyqt5下载下来的tar包(PyQt5-5.14.1.tar.gz),解压在/tmp/tmpXXX目录下可以看到,与手动编译一样。
也是漫长的编译过程:刚开始一直以为卡死了,通过top可以看到cc1plus运行就是在编译,大概3个小时
看到这个时候就基本成功了:Successfully installed PyQt5-5.14.1 PyQt5-sip-12.7.1
有一个警告先不管了:WARNING: The scripts pylupdate5, pyrcc5 and pyuic5 are installed in ‘/home/pi/.local/bin’ which is not on PATH.
3.测试pyqt
nano test.py

# !/usr/bin/python3
# coding = utf-8

from PyQt5.QtWidgets import *
import sys
class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.setWindowTitle("Hello")
        Gri_layout = QGridLayout()
        self.setLayout(Gri_layout)
        label = QLabel("Hello World")
        Gri_layout.addWidget(label, 0, 0)
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_()) 

执行:python3 test.py
测试成功

注:我的Qt5.12.4 配置输出:

pi@raspberrypi:/media/pi/rpi4 $ cat config.summary 
Build type: linux-rpi4-v3d-g++ (arm, CPU features: neon)
Compiler: gcc 8.3.0
Configuration: use_gold_linker enable_new_dtags largefile neon shared rpath release c++11 c++14 c++1z concurrent dbus reduce_exports stl
Build options:
  Mode ................................... release
  Optimize release build for size ........ no
  Building shared libraries .............. yes
  Using C standard ....................... C11
  Using C++ standard ..................... C++1z
  Using ccache ........................... no
  Using gold linker ...................... yes
  Using new DTAGS ........................ yes
  Using precompiled headers .............. no
  Using LTCG ..................</
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值