在阅读本文之前,请先参考我的另一篇文章:
https://www.jianshu.com/p/3b5c0e0fd674
其中有讲解编译链工具的下载和配置,以及QT5.9.9源码的编译,但是上次是没有配置OpenGL的,所以涉及3D渲染的开发是没法使用的,这次重新来过,主要解决OpenGL的编译问题。
配置QT源码:
同样地解压QT5.9.9的源码到armv7l目录。
进入QT源码根目录,在qtbase/mkspecs子目录下已经为各种平台准备好了相应的编译配置,
但是我们这里是linux-arm-gnueabihf平台,发现找不到,复制一个来修改:
cp linux-arm-gnueabi-g++ linux-arm-gnueabihf-g++ -r
编辑linux-arm-gnueabihf-g++/qmake.conf,把里面的arm-linux-gnueabi改成arm-linux-gnueabihf,修改内容如下:
#
# 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-linux-gnueabihf-gcc
QMAKE_CXX = arm-linux-gnueabihf-g++
QMAKE_LINK = arm-linux-gnueabihf-g++
QMAKE_LINK_SHLIB = arm-linux-gnueabihf-g++
# modifications to linux.conf
QMAKE_AR = arm-linux-gnueabihf-ar cqs
QMAKE_OBJCOPY = arm-linux-gnueabihf-objcopy
QMAKE_NM = arm-linux-gnueabihf-nm -P
QMAKE_STRIP = arm-linux-gnueabihf-strip
load(qt_config)
回到QT源码根目录,继续执行:
mkdir build
cd build
然后创建autoconfig.sh,输入如下内容:
#!/bin/bash
rm config.cache
SYSROOT=/home/kdjie/armv7l/sysroot-glibc-linaro-2.25-2019.12-arm-linux-gnueabihf
PREFIX=/home/kdjie/armv7l
../configure \
-v \
-confirm-license -opensource \
-prefix ${PREFIX}/qt5.9.9 \
-sysroot ${SYSROOT} \
-xplatform linux-arm-gnueabihf-g++ \
-no-pch -no-pkg-config -release -shared -make libs \
-nomake examples -nomake tests \
-xcb -xcb-xlib -no-eglfs \
-qpa xcb \
-linuxfb \
-opengl es2
然后执行:
chmod +x autoconfig.sh
./autoconfig.sh
稍等几分钟,不出意外的话会提示如下错误:
Note: No wayland-egl support detected. Cross-toolkit compatibility disabled.
WARNING: Feature xcb-xlib is insignificant in this configuration, ignoring related command line option(s).
ERROR: Feature 'opengles2' was enabled, but the pre-condition 'config.win32 || (!config.watchos && !features.opengl-desktop && libs.opengl_es2)' failed.
ERROR: The OpenGL functionality tests failed!
You might need to modify the include and library search paths by editing QMAKE_INCDIR_OPENGL[_ES2],
QMAKE_LIBDIR_OPENGL[_ES2] and QMAKE_LIBS_OPENGL[_ES2] in the mkspec for your platform.
解决问题:
查看config.log文件内容,它记录了配置过程的细节,搜索”ES2",找到如下内容:
> main.cpp:6:12: fatal error: GLES2/gl2.h: 没有那个文件或目录
> # include <GLES2/gl2.h>
> ^~~~~~~~~~~~~
> compilation terminated.
> gmake: *** [main.o] 错误 1
=> source failed verification.
test config.gui.libraries.opengl_es2 FAILED
显示缺少GLES2/gl2.h,我们可以去树莓派镜像上找,关于树莓派镜像文件的提取,请参考本人另一篇文章:
https://www.jianshu.com/p/41879d5864f8
进入树莓派系统目录后,执行:
[root@localhost raspios_rootfs]# find . | grep gl2.h
./opt/vc/include/GLES2/gl2.h
拷贝头文件:
cd /home/kdjie/armv7l/sysroot-glibc-linaro-2.25-2019.12-arm-linux-gnueabihf/usr/include
拷贝GLES2所需的头文件:
cp /mnt/raspios_rootfs/opt/vc/include/GLES2 . -r -i
cp /mnt/raspios_rootfs/opt/vc/include/KHR . -r -i
或者全拷:
cp /mnt/raspios_rootfs/opt/vc/include/* . -r -i
然后重新执行autoconfig.sh,仍然配置不通过,继续查看错误细节,找到如下内容:
> /home/kdjie/armv7l/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld.gold: error: cannot find -lGLESv2
> main.o:main.cpp:function main: error: undefined reference to 'glUniform1f'
> main.o:main.cpp:function main: error: undefined reference to 'glClear'
> collect2: error: ld returned 1 exit status
> gmake: *** [opengl_es2] 错误 1
=> source failed verification.
test config.gui.libraries.opengl_es2 FAILED
显示缺少libGLESv2.so这个文件,再到树莓派系统目录中找:
[root@localhost raspios_rootfs]# find . | grep libGLESv2.so
./opt/minecraft-pi/lib/brcm/libGLESv2.so
./opt/minecraft-pi/lib/mesa/libGLESv2.so
./usr/lib/scratch3/swiftshader/libGLESv2.so
./usr/lib/scratch3/libGLESv2.so
./usr/lib/chromium-browser/swiftshader/libGLESv2.so
./usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.1.0
./usr/lib/arm-linux-gnueabihf/libGLESv2.so.2
拷贝库:
cd /home/kdjie/armv7l/sysroot-glibc-linaro-2.25-2019.12-arm-linux-gnueabihf/usr/lib
cp /mnt/raspios_rootfs/usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.1.0 . -i
ln -s libGLESv2.so.2.1.0 libGLESv2.so
ln -s libGLESv2.so.2.1.0 libGLESv2.so.2
这里建立了两个符号链接,其中libGLESv2.so.2是后面编译应用程序的时候要用。
还有要注意:
libGLESv2.so 通常情况下会依赖 libGLdispatch.so,我里一并把它拷贝过来。
到树莓派系统目录中找:
[root@localhost raspios_rootfs]# find . | grep libGLdispatch.so
./usr/lib/arm-linux-gnueabihf/libGLdispatch.so.0
./usr/lib/arm-linux-gnueabihf/libGLdispatch.so.0.0.0
拷贝库:
cd /home/kdjie/armv7l/sysroot-glibc-linaro-2.25-2019.12-arm-linux-gnueabihf/usr/lib
cp /mnt/raspios_rootfs/usr/lib/arm-linux-gnueabihf/libGLdispatch.so.0.0.0 . -i
ln -s libGLdispatch.so.0.0.0 libGLdispatch.so.0
继续重新执行autoconfig.sh,终于配置通过:
OpenGL:
Desktop OpenGL ....................... no
OpenGL ES 2.0 ........................ yes
OpenGL ES 3.0 ........................ no
OpenGL ES 3.1 ........................ no
......
Qt is now configured for building. Just run 'gmake'.
Once everything is built, you must run 'gmake install'.
Qt will be installed into '/home/kdjie/armv7l/sysroot-glibc-linaro-2.25-2019.12-arm-linux-gnueabihf/home/kdjie/armv7l/qt5.9.9'.
编译:
make
在我的i5机器上大约等待3个小时,编译完成后,安装:
make install
qt5.9.9软件包将被放到 {PREFIX} 指定的目录下,我这里是/home/kdjie/armv7l/sysroot-glibc-linaro-2.25-2019.12-arm-linux-gnueabihf/home/kdjie/armv7l。