Qt5.12静态编译方案,size优化(2020)

6 篇文章 0 订阅

Qt5.12静态编译方案

截止当前,Qt最新的LTS版本是5.15,是今年才发布的,次新的LTS版本就是Qt5.12。因为由于官方提供下载的二进制版本的Qt均为动态库的形式,对于开发小工具来讲,往往需要进行静态编译以方便发布。目前网络上的一些博客中的Qt静态编译方案已经比较老旧了,不适合新版本使用,而且目前的新版本Qt静态编译已经变得非常简单了,这里在此记录描述。本文所使用的配置编译出的静态库仅包含release版本,且安装size优化,目的是尽可能生成较小的二进制程序。版本选择为qt5.12.8版本。分别在Win和Linux下编译。

注意文中的路径均修改为适合你的路径。

win

1.安装qt5.12.8官方二进制版本,同时安装Src源码或者下载qt-everywhere-src-5.12.8.zip源码包并解压
2.开始菜单找到Start Menu\Programs\Qt 5.12.8\5.12.8\MinGW 7.3.0 (32-bit)打开这个命令行
3.cd进入源码目录
4.设置安装静态版本的路径,输入示例

set QT_INSTALL_PREFIX=C:\Qt\Qt5.12.8\5.12.8\mingw73_32_static

5.依次执行以下命令,按如下配置进行编译可以得到最优化的size

configure.bat -static -static-runtime -release -no-pch -optimize-size -platform win32-g++ -prefix C:\Qt\Qt5.12.8\5.12.8\mingw73_32_static -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -no-openssl -opensource -confirm-license -skip webengine -make libs -nomake tools -nomake examples -nomake tests
mingw32-make -k -j8
mingw32-make -k install

6.上述配置使用了-no-openssl,如果你想使用openssl并且希望也是静态编译的,那么就需要先静态编译openssl1.1.x版本(注意qt5.12.8版本是需要配置使用openssl1.1.x版本的,1.0.x是不行的),openssl源码下载地址:https://www.openssl.org/source/

6.1.编译openssl,需要下载windows可用的perl,并openssl1.1.x要求的版本和老版的1.0.x不一样,最终我下载了,msys2以及对应的msys2 perl,将其加入环境变量可以成功编译openssl1.1.x,下载链接:
https://sourceforge.net/projects/msys2/
https://liquidtelecom.dl.sourceforge.net/project/msys2/REPOS/MSYS2/i686/perl-5.30.2-1-i686.pkg.tar.xz
注意还要加入qt目录内tools里的编译器的路径到环境变量

perl Configure mingw no-shared no-asm --prefix=C:/QtStaticLibs/OpenSSL
make 
make install

6.2.完成后回到之前的qt编译环境,configure.bat参数改为如下

configure.bat -static -static-runtime -release -no-pch -optimize-size -platform win32-g++ -prefix C:\Qt\Qt5.12.8\5.12.8\mingw73_32_static_with_openssl -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -ssl -openssl-linked OPENSSL_LIBS="-lssl -lcrypto -lcrypt32 -ladvapi32 -lws2_32 -lgdi32 -luser32" -L "C:\QtStaticLibs\OpenSSL\lib" -I "C:\QtStaticLibs\OpenSSL\include" -opensource -confirm-license -skip webengine -make libs -nomake tools -nomake examples -nomake tests

linux(ubuntu18.0)

1.下载源码包并解压
2.进入源码目录
3.依次执行以下命令,按如下配置进行编译可以得到最优化的size

sudo apt-get install libxkbcommon-dev
sudo apt install '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev
./configure -static -release -ltcg -optimize-size -no-pch -prefix "/home/xiaoming/Desktop/gcc_64_static" -system-xcb -recheck-all -skip webengine -nomake tools -nomake tests -nomake examples
make -k -j8
make -k install

参考:

https://retifrav.github.io/blog/2018/02/17/build-qt-statically/

https://forum.qt.io/topic/104197/error-running-statically-linked-application-could-not-find-the-qt-platform-plugin-xcb/10

编译完成的qt可以在其他机器上使用,需要在输出的bin目录内增加qt.conf用来指定PrefixPath以适应其他机器的路径,内容如下:

###################################################################
#       The qt.conf file can be used to override 
#       the hard-coded paths that are compiled into the Qt library
#       now it is up layer floder
###################################################################


[Paths]
Prefix = /home/qqm/opt/Qt/gcc_64_static/


###################################################################

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quard_D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值