Article Directory
Qt cross platform
Qt
The cross-platform is Code once, compile everywhere, Which Java
rely on virtual machine implementationCompile once, run everywhere Is different.
Java cross-platform implementation
Java
Is to abstract system across different platforms and operating system kernel code, the separately formed JVM层(java virtual machine)
, Java
the code running JVM
on the cross-platform java语言框架问题
decomposition on different platform JVM
problem, the structure is simple, logical, easy to implement 1 .
For this, the Java
price of sacrificing efficiency was paid. Java
Language need to pass JVM
and then mapped to the operating system, and finally by the CPU
implementation 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 QSerialPort
as an example in Qserialport.h
can 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 QSerialPort
interface provides is the use of 宏
doing cross-platform process.
So Qt
for 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
- Windows
procedures and Linux
packaged release.
Qt-Windows package release
Minimal dependency library
In QtCore
, when we build the chosen way Release
, will generate a *.exe
file
You will be prompted to open the file directly 缺少xxx.dll
and other errors.
We can Qt
under 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.
Windeployqt
As a slacker, I firmly believe that laziness is the driving force behind human development.
So I found a handy tool Windeployqt
that will help us copy the dependent library to the specified directory.
In this example, I D盘
created a UHelper文件夹
, then the previously generated UHelper.exe
movement inside.
Open Qt for Desktop
;
execute windeployqt *.exe
.
It can be found that the dependent libraries are copied in
Enigma Virtual Box is packaged as a standalone exe
We can be above exe文件
and dll文件
packaging to others, but this is not cool.
There is no way to directly exe
and 所需的dll库
are packaged as a exe
file it?
Really, it Enigma Virtual Box
is a good choice.
Please see the download link : Enigma Virtual Box official website
PS : If you are using Windeployqt
the library to add, all add dll
and drag selected folders are added to the Enigma Virtual Box
can.
Remember: the folder also, and don't try to change it.
After the execution is complete, an independent executable program is generated.
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:
The file directory after installation is as follows:
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.
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 Windows
something 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 Windows
at the same, they are Qt5.9.3
;
After moving over, the UI
display was a bit problematic, so I modified it UI
;
Then Release
the compiler, which generates an executable file UHelper
.
Since Ubuntu
comes with Qt库
6 , we can run directly:
./UHelper
Program running effect:
If not Ubuntu
the 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
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:
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
linuxdeployqt
It can be understood as Windeployqt
a Linux
version, so pack your silky smooth.
At the same time, it linuxdelpoyqt
is an open source project, see Github-linuxdelpoyqt for download address
After the download is complete, we rename it to delpoyqt
.
Execute it:
However, it is very inconvenient for us to call under other paths.
It can be moved to for this purpose /usr/local/bin
so that it can be used anywhere deployqt
.
sudo mv ./deployqt /usr/local/bin
Verification:
Packaging:
AppImage
The 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 file
overhead of 8 .
Apparently Linuxdeployqt
the 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 LTS
version currently supported is 16.04
.
Installation Ubuntu16.04
mirror, remember the election arm版
and installation Qt
, Linuxdeployqt
...
Repeat the previous steps and execute again:
sudo deployqt Uhelper -appimage
The question of the icon here can be ignored for now.
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 .