如何在ubuntu交叉编译qt5程序发布到rk3568 Linux开发板运行?

如何在ubuntu交叉编译qt5程序发布到rk3568 Linux开发板运行?

本文将介绍如何在ubuntu搭建qt的开发环境和第一个helloworld程序的创建、构建过程。

1.下载安装交叉编译工具

拷贝 rk3568_linux/prebuilts/gcc/linux-x86/ 到 /opt/aarch64-none-linux-gnu/

2.qt开发环境的搭建

2.1.下载 qt-everywhere-opensource-src-5.15.8.tar.xz 源码到ubuntu解压。

链接:https://download.qt.io/archive/qt/5.15/5.15.8/

2.2.编译qt-everywhere-opensource-src-5.15.8 源码

新建make.sh

#!/bin/sh
PWD=`pwd`
mkdir install
./configure \
-confirm-license \
-prefix  $PWD/install \
-release \
-opensource \
-xplatform linux-arm-gnueabi-g++ \
-optimized-qmake -pch \
-qt-libjpeg \
-qt-libpng \
-qt-zlib \
-no-opengl \
-skip qt3d \
-skip qtcanvas3d \
-skip qtpurchasing \
-skip qtlocation \
-skip qttools \
-no-sse2 \
-no-openssl \
-no-cups \
-no-glib \
-no-iconv \
-tslib \
-linuxfb \
-I /home/rockchip/qt-everywhere-opensource-src-5.15.8/install \
-L /home/rockchip/qt-everywhere-opensource-src-5.15.8/install/lib  \
-recheck-all \
-make examples

make -j20
make install

新建env.sh

export PATH=/opt/aarch64-none-linux-gnu/bin/:$PATH
export PATH=$PATH:/home/rockchip/qt-everywhere-opensource-src-5.15.8/install/bin
declare -x ARCH="arm"
declare -x CROSS_COMPILE="arm-none-linux-gnueabihf-"

修改 ./qtbase/mkspecs/linux-aarch64-gnu-g++/qmake.conf 的 g++.conf 和 linux.conf 交叉编译配置。

#
# qmake configuration for building with aarch64-linux-gnu-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                = aarch64-rockchip1031-linux-gnu-gcc
QMAKE_CXX               = aarch64-rockchip1031-linux-gnu-g++
QMAKE_LINK              = aarch64-rockchip1031-linux-gnu-g++
QMAKE_LINK_SHLIB        = aarch64-rockchip1031-linux-gnu-g++

# modifications to linux.conf
QMAKE_AR                = aarch64-rockchip1031-linux-gnu-ar cqs
QMAKE_OBJCOPY           = aarch64-rockchip1031-linux-gnu-objcopy
QMAKE_NM                = aarch64-rockchip1031-linux-gnu-nm -P
QMAKE_STRIP             = aarch64-rockchip1031-linux-gnu-strip
load(qt_config)

编译:

source env.sh
./make.sh

执行完后在 “qt-everywhere-opensource-src-5.15.8/install/bin/” 目录下就编译安装好了qmake,待下面使用。

3.helloworld程序的创建

main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

qhelloworld.pro

#-------------------------------------------------
#
# Project created by QtCreator 2023-06-29T16:20:46
#
#-------------------------------------------------

QT  += widgets

TARGET = helloworld
TEMPLATE = app

INCLUDEPATH +=$$PWD main

SOURCES += \
        $$PWD/main.cpp \
        $$PWD/mainwindow.cpp

HEADERS += \
        $$PWD/mainwindow.h

FORMS += \
        $$PWD/mainwindow.ui

4.helloworld的构建过程

使用上面qt源码编译安装的qmake工具来编译

qmake qhelloworld.pro
# qmake成功后会在当前目录下会生成一个makefile
make

make完成后即可在当前目录下生成 “TARGET = helloworld” 这个可执行文件,然后发布到开发板即可。

5.发布helloworld程序到嵌入式开发板

可通过sd卡、u盘、ssh等方法拷贝传输到开发板。

chmod +x helloworld
./helloworld

以上5个流程下来即可完成我们想要的第一个helloworld程序。

你可能感兴趣的:(Linux,qt,ubuntu,qt,linux)