Qt-Cross-platform program package release

Qt cross platform

Qt The cross-platform is Code once, compile everywhere, Which Javarely on virtual machine implementationCompile once, run everywhere Is different.

Java cross-platform implementation

JavaIs to abstract system across different platforms and operating system kernel code, the separately formed JVM层(java virtual machine), Javathe code running JVMon the cross-platform java语言框架问题decomposition on different platform JVMproblem, the structure is simple, logical, easy to implement 1 .

For this, the Javaprice of sacrificing efficiency was paid. JavaLanguage need to pass JVMand then mapped to the operating system, and finally by the CPUimplementation of a multi-step implementation process. Since early Java语言mainly used 解释性编译器, resulting in a further lowering operating efficiency. But with the 即时编译技术(JIT)introduction, in particular, significantly enhance the computing speed of the hardware, Java运行效率basically resolved the problem 1 .

Qt cross-platform implementation

Qt平台Encapsulates class libraries for different platforms API. These are upper made packages for our developers a variety of operating platforms interfaces are the same 1 . In my recently used QSerialPortas an example in Qserialport.hcan be found in the following clues:

Qserialport.h (source code can be stamped)

//line 74
#if defined(Q_OS_WIN32)
    typedef void* Handle;
#else
    typedef int Handle;
#endif

//line 307
#if defined(Q_OS_WIN32)
    Q_PRIVATE_SLOT(d_func(), bool _q_startAsyncWrite())
#endif

QSerialPort.cpp (source code can be stamped)

//line 90
#if defined(Q_OS_WIN32)
    : readChunkBuffer(QSERIALPORT_BUFFERSIZE, 0)
#endif
{
    
    
    writeBufferChunkSize = QSERIALPORT_BUFFERSIZE;
    readBufferChunkSize = QSERIALPORT_BUFFERSIZE;
}
//line 1253
qint64 QSerialPort::bytesToWrite() const
{
    
    
    qint64 pendingBytes = QIODevice::bytesToWrite();
#if defined(Q_OS_WIN32)
    pendingBytes += d_func()->writeChunkBuffer.size();
#endif
    return pendingBytes;
}

Visible QSerialPortinterface provides is the use of doing cross-platform process.

So Qtfor us to provide cross-platform interface are those who do?

The interface of Qt, some library packages encapsulated by Qt, data structures and algorithms...

For Qt项目not provide cross-platform part, you need to realize ourselves, it is commonly used .

For example, line break operation:

#ifdef Q_OS_WIN32
    qDebug() << "Windows换行!\r\n";
#else
	//假设是Linux
    qDebug()<<"unWindows换行!\n ";
#endif

Well, following entered, Qt- Windowsprocedures and Linuxpackaged release.

Qt-Windows package release

Minimal dependency library

In QtCore, when we build the chosen way Release, will generate a *.exefile

Insert picture description here
You will be prompted to open the file directly 缺少xxx.dlland other errors.

We can Qtunder the compiler search path missing dll文件;

My path is xxx\QT5.9.3\5.9.3\mingw53_32\bin, after finding it, put it under the executable file 同一路径.

Below is my copy dll库, it should be 最小依赖库a.

Insert picture description here

Windeployqt

As a slacker, I firmly believe that laziness is the driving force behind human development.
Insert picture description here
So I found a handy tool Windeployqtthat will help us copy the dependent library to the specified directory.

In this example, I D盘created a UHelper文件夹, then the previously generated UHelper.exemovement inside.

Open Qt for Desktop;
Insert picture description here
execute windeployqt *.exe.

Insert picture description here
It can be found that the dependent libraries are copied in

Insert picture description here

Enigma Virtual Box is packaged as a standalone exe

We can be above exe文件and dll文件packaging to others, but this is not cool.
Insert picture description here
There is no way to directly exeand 所需的dll库are packaged as a exefile it?

Really, it Enigma Virtual Boxis a good choice.

Please see the download link : Enigma Virtual Box official website

Insert picture description here
PS : If you are using Windeployqtthe library to add, all add dlland drag selected folders are added to the Enigma Virtual Boxcan.

Remember: the folder also, and don't try to change it.

After the execution is complete, an independent executable program is generated.

Insert picture description here

Inno Setup is packaged as an installation package

Please see the download link : Inno Setup official website

For the usage process, please refer to 2 Inno setup packaging tutorial

The generated installer is as follows:

Insert picture description here
The file directory after installation is as follows:
Insert picture description here
Unlike direct migration, the program can be found in the control panel (that is, the registry is modified). And it provides us with an uninstaller.

How to modify version information

So far we have generated executable files without detailed information.

Insert picture description here

Modify .pro

The easiest way is to directly pro文件add the following code.

Here are some commonly used information 3 4 :

//版本信息
VERSION = xx.xx.xx.xx
//图标
RC_ICONS = xxxx.ico
//公司名称
QMAKE_TARGET_COMPANY = ""
//产品名称
QMAKE_TARGET_PRODUCT = ""
//文件说明
QMAKE_TARGET_DESCRIPTION = ""
//版权信息
QMAKE_TARGET_COPYRIGHT = ""
//中文(简体)
RC_LANG = 0x0004

rc resource file

Because rc文件it is Windowssomething platform- Qt助手in for rc文件almost no introduction 5 .

Use can view this article reference- portal .

Qt-Linux package release

Copy the project to Ubuntu18.04;

Qt环境And Windowsat the same, they are Qt5.9.3;

After moving over, the UIdisplay was a bit problematic, so I modified it UI;

Then Releasethe compiler, which generates an executable file UHelper.

Insert picture description here
Since Ubuntucomes with Qt库 6 , we can run directly:

./UHelper

Program running effect:

Insert picture description here
If not Ubuntuthe system will need to copy so库.

Copy dependent libraries

It can be used ldd命令to view library and its dependent path:

ldd UHelper
//or
ldd ./UHelper

Insert picture description here
One copy a lot of trouble, for this purpose can make use shell脚本complete so库replication:

#!/bin/sh
# ldd $exe (所以这里写你的可执行文件名)
exe="UHelper"
# copy 目录
des="/home/hsy/SW/Qt5.9.3/Project/UHelper_test"
# awk 匹配第三个参数“/”,排除掉没有路径的
deplist=$(ldd $exe | awk '{
     
     if (match($3,"/")){
    
    printf("%s\n"),$3}}')
# 将so库拷贝至des目录
# -L:--dereference 始终遵循源中的符号链接
# -n: --no-clobber 不要覆盖已存在的文件
cp -L -n $deplist $des

carried out:

chmod 777 pack.sh
./pack.sh

The result after copying is shown below:

Insert picture description here
Write another executable file;

UHelper.sh

#!/bin/sh
appname=`basename $0 | sed s,\.sh$,,`
dirname=`dirname $0`
tmp="${dirname#?}"
if [ "${dirname%$tmp}" != "/" ]; then
dirname=$PWD/$dirname
fi
LD_LIBRARY_PATH=$dirname
export LD_LIBRARY_PATH
$dirname/$appname "$@"

Note: The script name here must be consistent with the executable file name.

By running this script instead of the executable file, you can ensure that the dynamic linker will find the Qt library 7 .

linuxdelpoyqt

linuxdeployqtIt can be understood as Windeployqta Linuxversion, so pack your silky smooth.

At the same time, it linuxdelpoyqtis an open source project, see Github-linuxdelpoyqt for download address

Insert picture description here
After the download is complete, we rename it to delpoyqt.

Execute it:
Insert picture description here
However, it is very inconvenient for us to call under other paths.

It can be moved to for this purpose /usr/local/binso that it can be used anywhere deployqt.

sudo mv ./deployqt /usr/local/bin

Verification:
Insert picture description here
Packaging:
Insert picture description here
too young too over
AppImageThe document mentions that its philosophy is that applications should be built on the oldest system so that they can run on newer systems8 .

As for the reason, the explanation is: This will exclude some 基础库, and these 基础库can in all major 桌面Linux发行版find, thus reducing the One app = one fileoverhead of 8 .

Apparently Linuxdeployqtthe author is very much agree with this approach.

probonopd : I think app developers are just "lazy" developing for the latest and largest release, and tell users "just upgrade your OS to use this app" 9 .

The latest Ubuntu LTSversion currently supported is 16.04.

Installation Ubuntu16.04mirror, remember the election arm版and installation Qt, Linuxdeployqt...

Repeat the previous steps and execute again:

sudo deployqt Uhelper -appimage

Insert picture description here
The question of the icon here can be ignored for now.
Insert picture description here
How to set an icon, how to make software boot from Kai, how packaged as deb文件can refer to the use of linuxdeployqt released Qt program in Linux 10 .

Reference thanks


  1. "Qt Platform System and Application-Qt5.5+ Core Methods, Skills and Cases" ↩︎ ↩︎ ↩︎

  2. Inno setup packaging tutorial ↩︎

  3. How to Get Current App Version in Qt ↩︎

  4. Qt generates Window resource file (.rc file) ↩︎

  5. Qt add Windows resource file (.rc file) ↩︎

  6. 01-Why use Qt development (Qt cross-platform application development) ↩︎

  7. Qt for Linux/X11 - Deployment ↩︎

  8. AppImage-Docs-Introduction-Concepts ↩︎ ↩︎

  9. Latest continuous linuxdeployqt build does not work on Ubuntu 16.04 LTS and openSUSE Leap 15.0 #340 ↩︎

  10. Use linuxdeployqt to publish Qt programs under Linux↩︎

Guess you like

Origin blog.csdn.net/weixin_40774605/article/details/105913281
Recommended