使用 arm-linux-gnueabihf- 交叉编译链静态编译 Qt 源码为静态库,流程如下:


一、准备工作

1. 复制交叉编译工具链

复制解压T153专用工具链

mkdir ~/t153 && cd ~/t153
# 复制host.zip到~/t153目录
# 解压
unzip host.zip

host文件夹内容:

yztiot@ubuntu:~/t153$ ls host/
arm-buildroot-linux-gnueabihf  doc  include  lib64  sbin   usr
bin                            etc  lib      opt    share

2. 删除默认交叉编译链(如果有)

sudo apt remove gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf binutils-arm-linux-gnueabihf

3. 添加环境变量

在 ~/.bashrc 最后添加:

echo 'export PATH=$PATH:/home/yztiot/t153/host/bin' >> ~/.bashrc
source ~/.bashrc

验证:

yztiot@ubuntu:~/t153$ arm-linux-gnueabihf-gcc --version
arm-linux-gnueabihf-gcc (GCC) 11.3.1 20220604 [releases/gcc-11 revision 591c0f4b92548e3ae2e8173f4f93984b1c7f62bb]
Copyright © 2021 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

4. 下载或者复制 Qt 源码并解压

cd ~
wget https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.12/5.12.12/single/qt-everywhere-src-5.12.12.tar.xz
tar -xvf qt-everywhere-src-5.12.12.tar.xz
cd qt-everywhere-src-5.12.12

二、创建交叉编译配置文件

Qt 使用 mkspecs 目录下的配置文件定义编译器,需要为 ARM 创建专用配置。

# 复制并修改 ARM 配置文件
cd qtbase/mkspecs
cp -r linux-arm-gnueabi-g++ linux-arm-gnueabihf-g++
cd linux-arm-gnueabihf-g++

编辑 qmake.conf

#
# 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)

QMAKE_LIBS += -ldl
QMAKE_LIBS += -lpthread
QMAKE_CXXFLAGS += -static-libstdc++

三、配置并静态编译 Qt

1. 创建构建目录

cd ~/qt-everywhere-src-5.12.12
mkdir build-arm-static && cd build-arm-static

2. 运行 configure

../configure \
    -prefix /opt/qt5.12.12-armhf-static \
    -static \
    -release \
    -xplatform linux-arm-gnueabihf-g++ \
    -opensource \
    -confirm-license \
    -nomake examples \
    -nomake tests \
    -no-opengl \
    -no-openssl \
    -no-xcb \
    -linuxfb \
    -qt-freetype \
    -qt-pcre \
    -qt-harfbuzz \
    -qt-zlib \
    -qt-libpng \
    -qt-libjpeg \
    -skip qtwebengine \
    -skip qt3d \
    -skip qtgamepad \
    -skip qtspeech \
    -skip qtlocation \
    -skip qtsensors \
    -skip qtserialbus \
    -skip qtserialport \
    -skip qtconnectivity \
    -skip qtwayland \
    -skip qtwebchannel \
    -skip qtwebsockets \
    -skip qtpurchasing \
    -skip qtdatavis3d \
    -skip qtcharts \
    -skip qtscxml \
    -skip qtnetworkauth \
    -skip qtmultimedia \
    -skip qtactiveqt \
    -skip qtremoteobjects \
    -skip qtscript \
    -skip qtdoc

3. 编译安装

# 编译(耗时较长)
make -j$(nproc)

# 安装到 /opt/qt5.12.12-armhf-static
sudo make install

编译如果报错:

In file included from /home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent.cpp:40:
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h: 在成员函数‘void QQmlProfilerEvent::assignNumbers(const Container&)’中:
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h:314:65: 错误: ‘numeric_limits’不是‘std’的成员
  314 |                     static_cast<quint16>(numbers.size()) : std::numeric_limits<quint16>::max();
      |                                                                 ^~~~~~~~~~~~~~
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h:314:87: 错误: expected primary-expression before ‘>’ token
  314 |                     static_cast<quint16>(numbers.size()) : std::numeric_limits<quint16>::max();
      |                                                                                       ^
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h:314:90: 错误: ‘::max’ has not been declared; did you mean ‘std::max’?
  314 |                     static_cast<quint16>(numbers.size()) : std::numeric_limits<quint16>::max();
      |                                                                                          ^~~
      |                                                                                          std::max
In file included from /home/yztiot/t153/host/opt/ext-toolchain/arm-linux-gnueabihf/include/c++/11.3.1/algorithm:62,
                 from /home/yztiot/qt-everywhere-src-5.12.12/qtbase/include/QtCore/../../src/corelib/global/qglobal.h:142,
                 from /home/yztiot/qt-everywhere-src-5.12.12/qtbase/include/QtCore/qglobal.h:1,
                 from /home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerclientdefinitions_p.h:54,
                 from /home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h:43,
                 from /home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent.cpp:40:
/home/yztiot/t153/host/opt/ext-toolchain/arm-linux-gnueabihf/include/c++/11.3.1/bits/stl_algo.h:3467:5: 附注: ‘std::max’ declared here
 3467 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
In file included from /home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilertypedevent_p.h:43,
                 from /home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilertypedevent.cpp:40:
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h: 在成员函数‘void QQmlProfilerEvent::assignNumbers(const Container&)’中:
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h:314:65: 错误: ‘numeric_limits’不是‘std’的成员
  314 |                     static_cast<quint16>(numbers.size()) : std::numeric_limits<quint16>::max();
      |                                                                 ^~~~~~~~~~~~~~
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h:314:87: 错误: expected primary-expression before ‘>’ token
  314 |                     static_cast<quint16>(numbers.size()) : std::numeric_limits<quint16>::max();
      |                                                                                       ^
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h:314:90: 错误: ‘::max’ has not been declared; did you mean ‘std::max’?
  314 |                     static_cast<quint16>(numbers.size()) : std::numeric_limits<quint16>::max();
      |                                                                                          ^~~
      |                                                                                          std::max

解决方法:
找到并编辑
/home/yztiot/qt-everywhere-src-5.12.12/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h
在开头加上:

#include <limits>
...
#ifndef QQMLPROFILEREVENT_P_H
#define QQMLPROFILEREVENT_P_H

#include "qqmlprofilerclientdefinitions_p.h"

#include <QtCore/qstring.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qmetatype.h>

#include <initializer_list>
#include <type_traits>
#include <limits> // 增加头文件声明防止报错
...

然后重新编译


3. 配置环境变量

echo 'export PATH=/opt/qt5.12.12-armhf-static/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

四、验证安装

# 检查 ARM 版 qmake
file /opt/qt5.12.12-armhf-static-old/bin/qmake
# 应显示
# /opt/qt5.12.12-armhf-static-old/bin/qmake: ELF 64-bit LSB shared object, x86-64,
# version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2
# BuildID[sha1]=59ebc947e0fad1d5634135c987b50740240ad4a1, for GNU/Linux 3.2.0, with debug_info
# not stripped

# 检查静态库
ls /opt/qt5.12.12-armhf-static/lib/libQt5Core.a
# 应存在 .a 文件(静态库)

# 检查 qmake 版本 
qmake --version
# 应显示
# QMake version 3.1
# Using Qt version 5.12.12 in /opt/qt5.12.12-armhf-static/lib

五、使用静态 Qt 交叉编译项目

打开QTCreator创建项目

# 1. 进入项目目录
cd ~/your_project

# 2. 使用 ARM 静态 qmake 生成 Makefile
qmake

# 3. 编译(生成 ARM 静态链接可执行文件)
make -j$(nproc)

六、部署到目标 ARM 设备

静态编译的程序不依赖 Qt 动态库,但仍可能需要系统基础库。

# 复制到目标设备
scp your_app root@targetIP:~

# 在目标设备运行
./your_app

关键参数说明

参数 作用
-static 生成静态库(.a),程序编译时静态链接
-xplatform linux-arm-gnueabihf-g++ 指定交叉编译平台配置
-no-opengl 禁用 OpenGL
-qt-zlib 使用 Qt 内置第三方库,减少外部依赖
-skip xxx 跳过不需要的模块,减少编译时间和体积

常见问题

问题 解决
cannot find -lts 添加 -no-tslib 或安装 tslib 交叉编译版
undefined reference to __atomic_* 链接时加 -latomic
error: invalid use of incomplete type 'sockaddr_in' 添加 -no-feature-networkinterface
编译极慢 正常现象,静态编译 + 交叉编译双重耗时
作者:陈柏林  创建时间:2026-05-18 16:26
最后编辑:陈柏林  更新时间:2026-05-20 09:47