1
2
3
./configure -static -release -qt-zlib -qt-libpng -qt-libjpeg -nomake examples -nomake tests -no-opengl   -skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtcharts -skip qtconnectivity -skip qtdatavis3d -skip qtdeclarative -skip qtdoc -skip qtgamepad -skip qtgraphicaleffects -skip qtimageformats -skip qtlocation -skip qtmacextras -skip qtmultimedia -skip qtnetworkauth -skip qtpurchasing -skip qtquickcontrols -skip qtquickcontrols2 -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtserialport -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtvirtualkeyboard -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -prefix /home/landv/QT5.15.3/qt-static 

# -prefix 文件输出位置
1
2
3
# 指定linux运行库
export PATH="/home/landv/Qt/6.3.1/gcc_64/bin:$PATH"
export LD_LIBRARY_PATH="/home/landv/Qt/6.3.1/gcc_64/lib:$PATH"

QTbase 精简编译
https://download.qt.io/official_releases/qt/5.15/5.15.3/

  • submodules 单独模块
  • single 全模块打包

全模块下载
https://download.qt.io/official_releases/qt/5.15/5.15.3/single/qt-everywhere-opensource-src-5.15.3.tar.xz
在 Qt 全模块源码目录下运行下面语句,进行按需裁减

1
ls -al | grep ^d | grep qt | awk '{ if($9 != "qtbase" ) printf "-skip %s \\\n" , $9}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-skip qt3d \
-skip qtactiveqt \
-skip qtandroidextras \
-skip qtcharts \
-skip qtconnectivity \
-skip qtdatavis3d \
-skip qtdeclarative \
-skip qtdoc \
-skip qtgamepad \
-skip qtgraphicaleffects \
-skip qtimageformats \
-skip qtlocation \
-skip qtlottie \
-skip qtmacextras \
-skip qtmultimedia \
-skip qtnetworkauth \
-skip qtpurchasing \
-skip qtquick3d \
-skip qtquickcontrols \
-skip qtquickcontrols2 \
-skip qtquicktimeline \
-skip qtremoteobjects \
-skip qtscript \
-skip qtscxml \
-skip qtsensors \
-skip qtserialbus \
-skip qtserialport \
-skip qtspeech \
-skip qtsvg \
-skip qttools \
-skip qttranslations \
-skip qtvirtualkeyboard \
-skip qtwayland \
-skip qtwebchannel \
-skip qtwebengine \
-skip qtwebglplugin \
-skip qtwebsockets \
-skip qtwebview \
-skip qtwinextras \
-skip qtx11extras \
-skip qtxmlpatterns \

精简编译QTbase只需要下载这个文件即可
https://download.qt.io/official_releases/qt/5.15/5.15.3/submodules/qtbase-everywhere-opensource-src-5.15.3.tar.xz

1
./configure -static -release -qt-zlib -qt-libpng -qt-libjpeg -nomake examples -nomake tests -no-opengl 

—————————————
交叉编译
MXE介绍: MXE.
MXE是在Linux下静态编译的跨平台环境,不需要一步一步繁杂的编译工具链,直接按教程一键make就下载安装好
MXE环境依赖安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
sudo apt-get install \
autoconf \
automake \
autopoint \
bash \
bison \
bzip2 \
flex \
g++ \
g++-multilib \
gettext \
git \
gperf \
intltool \
libc6-dev-i386 \
libgdk-pixbuf2.0-dev \
libltdl-dev \
libssl-dev \
libtool-bin \
libxml-parser-perl \
lzip \
make \
openssl \
p7zip-full \
patch \
perl \
python3 \
python3-mako \
ruby \
sed \
unzip \
wget \
xz-utils

MXE获取及安装

1
2
3
cd /opt 
git clone https://github.com/mxe/mxe.git
make MXE_TARGETS='x86_64-w64-mingw32.static i686-w64-mingw32.static' qt5

如果出现mako错误,你的环境和我一样,安装了conda,且conda的虚拟环境中的python代替了系统默认python。请使用
pip install mako即可。

1
2
# This variable controls the targets that will build.
#MXE_TARGETS := i686-w64-mingw32.static i686-w64-mingw32.shared x86_64-w64-mingw32.static x86_64-w64-mingw32.shared

-prefix 文件输出位置

configure.bat -static -release -confirm-license -opensource -opengl desktop -platform win32-g++ -sql-sqlite -sql-odbc -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -make libs -prefix “E:\Qt\5.15.2\mingw81_32_static_qtbase”

mingw32-make

mingw32-make -j4

mingw32-make install

\mkspecs\common\目录下的gcc-base.conf文件中的QMAKE_LFLAGS参数值改为-static并保存
mkspecs\common\目录下的g++-win32.conf文件中的QMAKE_LFLAGS_DLL参数值改为-static并保存

@[TOC]

为什么要静态编译(static link)

https://last2win.com/qt-5.14-static-link/
在默认情况下,用QtCreator编译程序时,使用的是动态编译。编译好的程序在运行时需要另外加上相应的Qt库文件,一大堆dll文件。如果想将生成的程序连同所需要的库一起完整地打包成一个可执行程序,那就需要使用静态编译。Qt官方不提供静态编译好的安装包,所以需要我们在自己的电脑上静态编译。而且exe文件会比动态编译的要小上很多。

QT源码下载

1
wget https://download.qt.io/archive/qt/5.15/5.15.3/single/qt-everywhere-opensource-src-5.15.3.tar.xz

QT源码下载

编译工具下载

Qt for X11 要求
Qt官方文档要求编译工具:Qt for linux - Requirements Qt 5.14

解压和编译

1
2
3
4
5
6
7
8
9
10
11
12
tar -xvf qt-everywhere-opensource-src-5.15.3.tar.xz

## 仅仅编译qtbase
./configure -static -release -qt-zlib -qt-libpng -qt-libjpeg -nomake examples -nomake tests -no-
pengl -prefix /home/landv/QT5.15.3/qt-static -skip qt3d -skip qtactiveqt -skip qtandroidextra
-skip qtcanvas3d -skip qtcharts -skip qtconnectivity -skip qtdatavis3d -skip qtdeclarative -sk
p qtdoc -skip qtgamepad -skip qtgraphicaleffects -skip qtimageformats -skip qtlocation -skip qt
acextras -skip qtmultimedia -skip qtnetworkauth -skip qtpurchasing -skip qtquickcontrols -skip
qtquickcontrols2 -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtseri
lbus -skip qtserialport -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtv
rtualkeyboard -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtw
bview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns
  • Type ‘c’ if you want to use the Commercial Edition. Type ‘o’ if you want to use the Open Source Edition.
    • 选择o,开源模式
  • 选择y,同意协议就好
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     This is the Qt Open Source Edition.

    You are licensed to use this software under the terms of
    the GNU Lesser General Public License (LGPL) version 3
    or the GNU General Public License (GPL) version 2.

    Type 'L' to view the GNU Lesser General Public License version 3 (LGPLv3).
    Type 'G' to view the GNU General Public License version 2 (GPLv2).
    Type 'y' to accept this license offer.
    Type 'n' to decline this license offer.

make 开始编译

参数-static 指的是要采用静态编译Qt的方法
参数-release指的是只编译release版本的库
参数-qt-zlib -qt-libpng -qt-libjpeg 是更好确定Qt编译出的程序能在目前不知的系统下运行【可要可不要】
参数-nomake demos -nomake examples 指的是不编译demos和examples
参数 -prefix /usr/local/Qt-5.12.1_static 指明静态编译的Qt安装的目录,据说没有这个,就会覆盖掉你之前安装的动态编译安装的Qt(如果有的话)
参数-skip qttools -skip qtsensors -skip qtconnectivity -skip qtserialport -skip qtmultimedia表示跳过这些模块,不编译,若需要这些模块则去掉相关-skip

make -j4
使用4个线程同时编译Qt源码

安装Qt-5.12.1_static静态库,到-prefix后面设置的目录下

1.6 使用静态库编译Qt项目代码
(1)指定静态库的路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/Qt-5.12.1_static/lib
后面的Qt静态库的路径根据实际情况改变

(2)指定默认qmake
将静态库的qmake拷贝到/usr/bin
qmake -v检测该qmake是否是静态库的qmake路径

(4)生成Makefile
qmake xxx.pro

(5)编译Makefile
make