Ubuntu18.04实现Aarch64和arm32的交叉编译全步骤

本文所使用的文件下载:

在这里插入图片描述
百度网盘:

链接:https://pan.baidu.com/s/1mnpFepFY-rOlwWd3QbYZiw
提取码:5566

下载交叉编译链接工具

在这里插入图片描述

安装交叉编译器

解压

sudo tar -xvf gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar -C /opt/
sudo tar -xvf gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf.tar -C /opt/

解压完成后,配置环境变量

sudo vim ~/.bashrc

添加编译器路径:路径为你解压后路径,我的路径为:/opt/

PATH=$PATH:/opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin
PATH=$PATH:/opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rzvPboQB-1623135507861)(http://www.zlozl5566.ltd/image-20210607160531155.png)]在这里插入图片描述

配置生效

source ~/.bashrc

验证是否安装成功

aarch64-linux-gnu-gcc -v
arm-linux-gnueabihf-gcc -v

arm32如下为成功。

在这里插入图片描述

aarch64如下为成功在这里插入图片描述

测试交叉编译器

写一个简单的c程序,交叉编译后放到开发板上去运行看看 能否运行成功

#include <stdio.h>
int main(int argc, char **argv)
{
    printf("Hello, you do it succeed!!!\n");
    return 0;
}
  • 先使用arm32编译

image-20210520154146544

编译完成后,看到文件是32位的。
在这里插入图片描述

移植到arm32板子上,可以运行成功。
在这里插入图片描述

  • aarch64编译

编译完成后,看到文件是arm 64位的。

在这里插入图片描述

移植到aarch64的板子上,可以运行成功。
在这里插入图片描述

由此可见,两种交叉编译工具都安装成功,可以使用。

编译Qt源码

这里只是用aarch64编译器去编译qt源码

下载Qt源码

在这里插入图片描述

解压qt源码

sudo tar xvf qt-everywhere-src-5.12.10.tar.xz
cd  qt-everywhere-src-5.12.10

修改qmake配置文件

首先复制一份文件

sudo cp -a linux-aarch64-gnu-g++/ aarch64-linux-gnu-g++/

修改配置文件目录,后面都改成你交叉编译器安装的路径。”/opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/“

# modifications to g++.conf
QMAKE_CC                = /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc 
QMAKE_CXX               = /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ 
QMAKE_LINK              = /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ 
QMAKE_LINK_SHLIB        = /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ 

# modifications to linux.conf
QMAKE_AR                = /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ar cqs
QMAKE_OBJCOPY           = /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-objcopy
QMAKE_NM                = /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc-nm  -P
QMAKE_STRIP             = /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-strip
load(qt_config)

在这里插入图片描述

# modifications to g++.conf

QMAKE_CC                = /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabi-gcc
QMAKE_CXX               = /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabi-g++
QMAKE_LINK              = /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabi-g++
QMAKE_LINK_SHLIB        = /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabi-g++

# modifications to linux.conf

QMAKE_AR                = /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabi-ar cqs
QMAKE_OBJCOPY           = /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabi-objcopy
QMAKE_NM                = /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabi-nm -P
QMAKE_STRIP             = /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabi-strip

配置编译参数

Linux 下 qt5的安装及库编译(1)_hrx-@@-CSDN博客

qt5的configure选项说明(2)_hrx-@@-CSDN博客

  1. 建议是新建一个build目录,然后在该目录下配置configure,make之类的,这样子配置编译生成的临时文件会放在该目录下,而不会污染源码。

  2. 在build目录下创建一个build.sh的脚本,添加如下内容

../qt-everywhere-src-5.12.10/configure \
-verbose \
-opensource \
-release \
-shared \
-confirm-license \
-make libs \
-nomake tests \
-nomake examples \
-skip qtmacextras \
-skip qtandroidextras \
-no-opengl \
-xplatform aarch64-linux-gnu-g++ \
-prefix /opt/qt-5.12.10-linux-aarch64-gcc

verbose:打印配置过程中步骤信息
opensource:编译Qt的开源版本
release:编译Qt的release版本
shared:构建 Qt 共享库
confirm-license:自动确认许可
make libs:编译 lib 组件
nomake tests:不编译 tests组件
nomake examples:不编译 examples 组件
skip qtmacextras:跳过 qtmacextras 模块
skip qtandroidextras:跳过 qtandroidextras 模块

no-opengl: 我在虚拟机下编译opengl模块报错,未解决 所以选择不编译

xplatform: 选择交叉编译时的目标 mkspec

prefix :指定 make install 的位置

./configure -release -opensource -confirm-license -xplatform aarch64-linux-gnu-gcc  -prefix /opt/aarch64—qt5 -nomake examples -nomake tools -nomake tests -no-opengl

安装编译所依赖的库

  • 安装基础的编译环境
sudo apt-get build-dep qt5-default
sudo apt-get install libxcb-xinerama0-dev 
sudo apt-get install build-essential perl python git
  • Libxcb

    sudo apt-get install '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev
    
  • OpenGL

    sudo apt-get install build-essential
    sudo apt-get install libgl1-mesa-dev
    sudo apt-get install libglu1-mesa-dev
    sudo apt-get install libegl1-mesa-dev
    sudo apt-get install freeglut3-dev
    
  • Qt WebKit

    sudo apt-get install flex bison gperf libicu-dev libxslt-dev ruby
    
  • Qt WebEngine

    sudo apt-get install libssl-dev libxcursor-dev libxcomposite-dev libxdamage-dev libxrandr-dev libdbus-1-dev libfontconfig1-dev libcap-dev libxtst-dev libpulse-dev libudev-dev libpci-dev libnss3-dev libasound2-dev libxss-dev libegl1-mesa-dev gperf bison
    
  • Qt Multimedia

    sudo apt-get install libasound2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
    
  • QDoc Documentation Generator Tool

    sudo apt install libclang-6.0-dev llvm-6.0
    

执行build脚本 构建选项:

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 .............. yes
  Using LTCG ............................. no
  Target compiler supports:
    SSE .................................. SSE2 SSE3 SSSE3 SSE4.1 SSE4.2
    AVX .................................. AVX AVX2
    AVX512 ............................... F ER CD PF DQ BW VL IFMA VBMI
    Other x86 ............................ AES F16C RDRAND SHA
    Intrinsics without -mXXX option ...... yes
  Build parts ............................ libs
Qt modules and options:
  Qt Concurrent .......................... yes
  Qt D-Bus ............................... yes
  Qt D-Bus directly linked to libdbus .... yes
  Qt Gui ................................. yes
  Qt Network ............................. yes
  Qt Sql ................................. yes
  Qt Testlib ............................. yes
  Qt Widgets ............................. yes
  Qt Xml ................................. yes
Support enabled for:
  Using pkg-config ....................... yes
  udev ................................... yes
  Using system zlib ...................... yes
Qt Core:
  DoubleConversion ....................... yes
    Using system DoubleConversion ........ no
  GLib ................................... yes
  iconv .................................. no
  ICU .................................... yes
  Tracing backend ........................ <none>
  Logging backends:
    journald ............................. no
    syslog ............................... no
    slog2 ................................ no
  Using system PCRE2 ..................... no
Qt Network:
  getifaddrs() ........................... yes
  IPv6 ifname ............................ yes
  libproxy ............................... no
  Linux AF_NETLINK ....................... yes
  OpenSSL ................................ yes
    Qt directly linked to OpenSSL ........ no
  OpenSSL 1.1 ............................ yes
  DTLS ................................... yes
  SCTP ................................... no
  Use system proxies ..................... yes
Qt Gui:
  Accessibility .......................... yes
  FreeType ............................... yes
    Using system FreeType ................ yes
  HarfBuzz ............................... yes
    Using system HarfBuzz ................ yes
  Fontconfig ............................. yes
  Image formats:
    GIF .................................. yes
    ICO .................................. yes
    JPEG ................................. yes
      Using system libjpeg ............... no
    PNG .................................. yes
      Using system libpng ................ yes
  EGL .................................... yes
  OpenVG ................................. no
  OpenGL:
    Desktop OpenGL ....................... yes
    OpenGL ES 2.0 ........................ no
    OpenGL ES 3.0 ........................ no
    OpenGL ES 3.1 ........................ no
    OpenGL ES 3.2 ........................ no
  Vulkan ................................. no
  Session Management ..................... yes
Features used by QPA backends:
  evdev .................................. yes
  libinput ............................... no
  INTEGRITY HID .......................... no
  mtdev .................................. no
  tslib .................................. no
  xkbcommon .............................. yes
  X11 specific:
    XLib ................................. yes
    XCB Xlib ............................. yes
    EGL on X11 ........................... yes
QPA backends:
  DirectFB ............................... no
  EGLFS .................................. yes
  EGLFS details:
    EGLFS OpenWFD ........................ no
    EGLFS i.Mx6 .......................... no
    EGLFS i.Mx6 Wayland .................. no
    EGLFS RCAR ........................... no
    EGLFS EGLDevice ...................... yes
    EGLFS GBM ............................ no
    EGLFS VSP2 ........................... no
    EGLFS Mali ........................... no
    EGLFS Raspberry Pi ................... no
    EGLFS X11 ............................ yes
  LinuxFB ................................ yes
  VNC .................................... yes
  Mir client ............................. no
  XCB:
    Using system-provided XCB libraries .. yes
    XCB XKB .............................. yes
    XCB XInput ........................... yes
    Native painting (experimental) ....... yes
    GL integrations:
      GLX Plugin ......................... yes
        XCB GLX .......................... yes
      EGL-X11 Plugin ..................... yes
Qt Sql:
  SQL item models ........................ yes
Qt Widgets:
  GTK+ ................................... no
  Styles ................................. Fusion Windows
Qt PrintSupport:
  CUPS ................................... no
Qt Sql Drivers:
  DB2 (IBM) .............................. no
  InterBase .............................. no
  MySql .................................. no
  OCI (Oracle) ........................... no
  ODBC ................................... no
  PostgreSQL ............................. no
  SQLite2 ................................ no
  SQLite ................................. yes
    Using system provided SQLite ......... no
  TDS (Sybase) ........................... no
Qt Testlib:
  Tester for item models ................. yes
Qt SerialBus:
  Socket CAN ............................. yes
  Socket CAN FD .......................... yes
Further Image Formats:
  JasPer ................................. no
  MNG .................................... no
  TIFF ................................... yes
    Using system libtiff ................. no
  WEBP ................................... yes
    Using system libwebp ................. no
Qt QML:
  QML network support .................... yes
  QML debugging and profiling support .... yes
  QML sequence object .................... yes
  QML list model ......................... yes
  QML XML http request ................... yes
  QML Locale ............................. yes
  QML delegate model ..................... yes
Qt Quick:
  Direct3D 12 ............................ no
  AnimatedImage item ..................... yes
  Canvas item ............................ yes
  Support for Qt Quick Designer .......... yes
  Flipable item .......................... yes
  GridView item .......................... yes
  ListView item .......................... yes
  TableView item ......................... yes
  Path support ........................... yes
  PathView item .......................... yes
  Positioner items ....................... yes
  Repeater item .......................... yes
  ShaderEffect item ...................... yes
  Sprite item ............................ yes
Qt Scxml:
  ECMAScript data model for QtScxml ...... yes
Qt Gamepad:
  SDL2 ................................... no
Qt 3D:
  Assimp ................................. yes
  System Assimp .......................... no
  Output Qt3D Job traces ................. no
  Output Qt3D GL traces .................. no
  Use SSE2 instructions .................. yes
  Use AVX2 instructions .................. no
  Aspects:
    Render aspect ........................ yes
    Input aspect ......................... yes
    Logic aspect ......................... yes
    Animation aspect ..................... yes
    Extras aspect ........................ yes
Qt 3D Renderers:
  OpenGL Renderer ........................ yes
Qt 3D GeometryLoaders:
  Autodesk FBX ........................... no
Qt Wayland Drivers:
  EGL .................................... yes
  Raspberry Pi ........................... no
  XComposite EGL ......................... yes
  XComposite GLX ......................... yes
  DRM EGL ................................ yes
  libhybris EGL .......................... no
  Linux dma-buf server buffer integration . yes
  Vulkan-based server buffer integration . no
  Shm emulation server buffer integration . yes
Qt Wayland Client ........................ yes
Qt Wayland Compositor .................... yes
Qt Wayland Compositor Layer Plugins:
  VSP2 hardware layer integration ........ no
Qt Bluetooth:
  BlueZ .................................. no
  BlueZ Low Energy ....................... no
  Linux Crypto API ....................... no
  WinRT Bluetooth API (desktop & UWP) .... no
Qt Sensors:
  sensorfw ............................... no
Qt Quick Controls 2:
  Styles ................................. Default Fusion Imagine Material Universal
Qt Quick Templates 2:
  Hover support .......................... yes
  Multi-touch support .................... yes
Qt Positioning:
  Gypsy GPS Daemon ....................... no
  WinRT Geolocation API .................. no
Qt Location:
  Qt.labs.location experimental QML plugin . yes
  Geoservice plugins:
    OpenStreetMap ........................ yes
    HERE ................................. yes
    Esri ................................. yes
    Mapbox ............................... yes
    MapboxGL ............................. yes
    Itemsoverlay ......................... yes
QtXmlPatterns:
  XML schema support ..................... yes
Qt Multimedia:
  ALSA ................................... yes
  GStreamer 1.0 .......................... no
  GStreamer 0.10 ......................... no
  Video for Linux ........................ yes
  OpenAL ................................. no
  PulseAudio ............................. yes
  Resource Policy (libresourceqt5) ....... no
  Windows Audio Services ................. no
  DirectShow ............................. no
  Windows Media Foundation ............... no
Qt Tools:
  QDoc ................................... yes
Qt WebEngine:
  Embedded build ......................... no
  Full debug information ................. no
  Pepper Plugins ......................... yes
  Printing and PDF ....................... yes
  Proprietary Codecs ..................... no
  Spellchecker ........................... yes
  Native Spellchecker .................... no
  WebRTC ................................. yes
  Use System Ninja ....................... no
  Geolocation ............................ yes
  WebChannel support ..................... yes
  Use v8 snapshot ........................ yes
  Kerberos Authentication ................ no
  Support qpa-xcb ........................ yes
  Use ALSA ............................... yes
  Use PulseAudio ......................... yes
  Optional system libraries used:
    re2 .................................. no
    icu .................................. no
    libwebp, libwebpmux and libwebpdemux . no
    opus ................................. no
    ffmpeg ............................... no
    libvpx ............................... no
    snappy ............................... no
    glib ................................. yes
    zlib ................................. yes
    minizip .............................. no
    libevent ............................. no
    jsoncpp .............................. no
    protobuf ............................. no
    libxml2 and libxslt .................. yes
    lcms2 ................................ no
    png .................................. yes
    JPEG ................................. no
    harfbuzz ............................. yes
    freetype ............................. yes
  Required system libraries:
    fontconfig ........................... yes
    dbus ................................. yes
    nss .................................. yes
    khr .................................. yes
    glibc ................................ yes
  Required system libraries for qpa-xcb:
    x11 .................................. yes
    libdrm ............................... yes
    xcomposite ........................... yes
    xcursor .............................. yes
    xi ................................... yes
    xtst ................................. yes

Note: Also available for Linux: linux-clang linux-icc

Note: Disabling X11 Accessibility Bridge: D-Bus or AT-SPI is missing.

Qt is now configured for building. Just run 'make'.
Once everything is built, you must run 'make install'.
Qt will be installed into '/opt/qt-5.12.10-linux-aarch64'.

Prior to reconfiguration, make sure you remove any leftovers from
the previous build.

执行make

对源码进行编译,时间挺长的,大约三四个小时

make -j8

执行make install

sudo make install

在编译完成后执行 make install 会将qt库安装到 -prefix 指定的目录下。

查看库的系统架构

可以库安装路径下面,readelf - h xxx.so 可以看到编译出来的库系统架构是Aarch64的。

image-20210608133610282

配置qtcreator交叉编译环境

在ubuntu下搭建Qt Creator的arm交叉编译环境_bigmarshal的博客-CSDN博客

首先没有安装qtcreator,需要安装

sudo apt-get install qtcreator

添加交叉编译工具链
在这里插入图片描述

编译器路径,选择你之前安装的路径,我的路径为:

c++

/opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++

c

/opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc

选择qmake版本,路径为你编译输出的路径。

在这里插入图片描述

添加Kit

在这里插入图片描述

创建个带界面的工程进行交叉编译测试

选择Aarch64编译

image-20210608134615421

在这里插入图片描述

程序依赖库也是正常的。

在这里插入图片描述

可以看出编译生成的应用程序是ARM aarch64的。接下来打包后移植到rk3399中执行打开。

在这里插入图片描述

最后可以看出大功告成~~~

疑问?

  1. 假如用rk3399上所有qt的依赖库,再加上qmake和gcc编译工具,不知道是否可以跳过编译qt源码这个步骤,直接使用现有的依赖库?
  2. 能够直接可以使用别人交叉编译后的依赖库已经qmake等编译工具?

遇到的问题

编译opengl 模块报错

WARNING: QDoc will not be compiled, probably because libclang could not be located. This means that you cannot build the Qt documentation.

Either ensure that llvm-config is in your PATH environment variable, or set LLVM_INSTALL_DIR to the location of your llvm installation.
On Linux systems, you may be able to install libclang by installing the libclang-dev or libclang-devel package, depending on your distribution.
On macOS, you can use Homebrew's llvm package.
On Windows, you must set LLVM_INSTALL_DIR to the installation path.

WARNING: Python version 2 (2.7.5 or later) is required to build QtWebEngine.

WARNING: gperf is required to build QtWebEngine.

WARNING: bison is required to build QtWebEngine.

WARNING: flex is required to build QtWebEngine.

WARNING: host pkg-config not found

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.

imx6 qt5编译出错“ QMAKE_INCDIR_OPENGL_ES2, QMAKE_LIBDIR_OPENGL_ES2" - i.MX - 恩智浦技术社区 (nxpic.org.cn)

QMAKE_INCDIR += /usr/include
QMAKE_LIBDIR += /usr/lib

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.

QMAKE_INCDIR_EGL = /usr/include 
QMAKE_LIBDIR_EGL = /usr/lib
QMAKE_INCDIR_OPENGL_ES2 = /usr/include
QMAKE_LIBDIR_OPENGL_ES2 = /usr/lib
QMAKE_LIBS_EGL         += -lEGL
QMAKE_LIBS_OPENGL_ES2  += -lGLESv2 -lEGL -lGAL

QMAKE_INCDIR       += /usr/include
QMAKE_LIBDIR       += /usr/lib

configuring Qt5.15 source on Ubuntu 20.04 opengl necessary for qt-quick - Stack Overflow

sudo ./configure -no-opengl -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=/opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf- -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -prefix /opt/qt5_amr32
  • ./configure -release -opengl es2 -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- -sysroot ~/raspi/sysroot -opensource -confirm-license -skip qtwayland -skip qtlocation -skip qtscript -make libs -prefix /usr/local/qt5pi -extprefix ~/raspi/qt5pi -hostprefix ~/raspi/qt5 -no-use-gold-linker -v -no-gbm
  • $ ./configure -opengl es2 -device linux-rasp-pi3-g++ -device-option CROSS_COMPILE=/home/wind/opt/raspi/tools/arm-bcm2708/gcc-linaro-4.9-2015.02-3-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf- -sysroot /mnt -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -prefix /usr/local/qt5
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐