How to package a QT program
Under Windows system
Through the windeployqt tool
1. Run the program in release mode
2. Copy the executable file in the release folder to a path, desktop is recommended
3. Create a new folder on the desktop and put the executable file just now into it
4. Run the windeployqt.exe program
5. Go to the newly created folder path
6. Execute the command windeployqt test.exe, where test is the file name of the executable file
7. After the execution is completed, you can find that there are more dlls required to run the program in the folder
8. The folder is packaged and can be run on other computers
Under Linux system
Through the linuxdeployqt tool
1. Run in release mode
Run the QT program in release mode like Windows to generate an executable file, that is, you can directly double-click the opened file, or you can view it through the right-click property
shared library file
executable file
Note that the icon is also different. If it is an executable file, the next step is not required.
2. For the files compiled in release mode are shared library files, perform the following operations
Add the following code to the .pro file
TEMPLATE = app #这生成一个exe
QMAKE_LFLAGS += -no-pies
Remember to execute qmake after adding
3. Create a new folder and copy the generated executable file into it
The name of the folder here can be the same as the name of the executable file for easy identification. I will not demonstrate it here.
4. Install linuxdeployqt
For Qt, the tool linuxdeployqt can help us quickly package Qt projects. The corresponding software under windows is called windowsdeployqt.
The github URL of linuxdeployqt: linuxdeployoqt
There are two ways to install linuxdeployqt on github, which are directly downloading and installing the compiled installation package, or installing from source code. It's not difficult either way.
However, since linuxdeployqt has not released the corresponding version of Ubuntu18.04, it is better for Ubuntu18.04 users to compile the source code for installation, otherwise when using linuxdeployqt to package the Qt project, an error similar to the operating system version being too high will be reported.
4.1 Download the linuxdeployqt executable program, the first item in the above picture
- It is recommended to modify the file name after downloading, and change the file name to linuxdeployqt.
- To modify the permissions of linuxdeployqt, you can use the following command:
sudo chmod a+x linuxdeployqt
- Then you can consider copying the file to the directory /usr/local/bin/, so that you can use the command linuxdeployqt directly later. The copy command is as follows:
sudo cp linuxdeployqt /usr/local/bin/
4.2 Compile the source code and install linuxdeployqt
The operating system is Ubuntu18.04 and above. At present, you need to compile and install linuxdeployqt through source code. Otherwise, some error messages will appear when executing linuxdeployqt: such as the host system is too high.
- Reference URL for compiling source code installation: Reference URL
- We have already installed g++, git and other tools by default, if not, you must install them first.
- You also need to install the patchelf tool.
An example command is as follows:
sudo apt-get -y install git g++ libgl1-mesa-dev
git clone https://github.com/probonopd/linuxdeployqt.git
sudo apt install patchelf
Execute the above commands in turn, you can
- Enter the linuxdeployqt folder, the command is as follows:
cd linuxdeployqt
- Modify the source code of tools/linuxdeployqt/main.cpp, comment out the version judgment statement in the source code, and modify the source code by command gedit tools/linuxdeployqt/main.cpp:
gedit tools/linuxdeployqt/main.cpp
The code that needs to be commented is as follows:
// openSUSE Leap 15.0 uses glibc 2.26 and is used on OBS
/*if (strverscmp (glcv, "2.27") >= 0) {
qInfo() << "ERROR: The host system is too new.";
qInfo() << "Please run on a system with a glibc version no newer than what comes with the oldest";
qInfo() << "currently still-supported mainstream distribution (xenial), which is glibc 2.23.";
qInfo() << "This is so that the resulting bundle will work on most still-supported Linux distributions.";
qInfo() << "For more information, please see";
qInfo() << "https://github.com/probonopd/linuxdeployqt/issues/340";
return 1;
}*/
- There is a CMAKELIST file in the current directory, execute the cmake command:
cmake CMakeLists.txt
- Execute the make command to compile the source code:
make
- After the compilation is complete, an executable file linuxdeployqt will be generated under the directory tools/linuxdeployqt/linuxdeployqt, modify its permissions:
cd ./tools/linuxdeployqt
sudo chmod a+x linuxdeployqt
- For later use, you can copy the executable file to the /usr/local/bin directory:
sudo cp linuxdeployqt /usr/local/bin/
- The linuxdeployqt command may use the option -appImage (actually I used it), so you need to install appimagetool. The download and installation commands are as follows:
sudo wget -c "https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage" -O /usr/local/bin/appimagetool
sudo chmod a+x /usr/local/bin/appimagetool
5. Add environment variables
At this time, if you execute qmake -v
the command operation, it means that the path is not found, and you need to add environment variables.
Enter gedit ~/.bashrc
the command in the terminal, modify the .bashrc file, and append the following content at the end of the file, where /opt/Qt5.9.9/5.9.9 is my Qt installation path, and you should use your own path instead:
#add QT ENV
export PATH=/opt/Qt5.9.9/5.9.9/gcc_64/bin:$PATH
export LD_LIBRARY_PATH=/opt/Qt5.9.9/5.9.9/gcc_64/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=/opt/Qt5.9.9/5.9.9/gcc_64/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/opt/Qt5.9.9/5.9.9/gcc_64/qml:$QML2_IMPORT_PATH
It is also possible to add only the first line of commands after testing
Double quotes can be added or not
Finally, source the ~/.bashrc shell file to take effect immediately without having to log out and log in again.
$ source ~/.bashrc
Then execute qmake -v
6. How to use
- Order
linuxdeployqt ~/path/to/project/application -appimage
Example: Go to the newly created folder previously used to store executable files
cd /home/li/Desktop.qttest
linuxdeployqt untitled -appimage
Among them, untitled is an executable file, and the program can be automatically packaged after execution.
AppRun is equivalent to a shortcut.
When executing this command, the following error may be reported:
Categories entry not found in desktop file
.desktop file is missing a Categories= key
12
It is said that one attribute, Categories, is missing in the .desktop file, so we need to manually add a line to the desktop file:
Categories=Application;
7. Add qt application icon in ubuntu
Modify the desktop file in the qt directory. You can modify it according to the official ubuntu prompt.
ubuntu desktop file use
#-- 全局安装(所有用户可用),将xxx.desktop 复制到/usr/share/applications
#-- 当前用户可用, 将xxx.desktop 复制到 ~/.local/share/applications 目录即可
#--appName.desktop
[Desktop Entry]
Version=1.0 #app的版本
Name=myQtApp #app的名字
Comment= this app use for xxx #说明信息
Exec=/path/to/your/QtApp/myQtApp #app的执行路径,绝对路径
Icon=/path/to/your/app_icon/myQtApp.png #icon 路径,绝对路径
Terminal=false #是否在终端启动,效果自己试一下就知道了
Type=Application
Categories=Utility;Application;
screenplay
1. Create a new file
Create two new files in the newly created folder
- pack.sh
- untitled.sh The file name of this file must be the same as the file name of the executable file
2. Add script
- Add the following to the pack.sh file:
#!/bin/sh
exe="untitled" #你需要发布的程序名称
des="/home/li/Desktop/qttest2" #创建文件夹的位置
deplist=$(ldd $exe | awk '{
if (match($3,"/")){
printf("%s "),$3 } }')
cp $deplist $des
- Then add the following content to the untitled.sh file (the content does not need to be changed):
#!/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 "$@"
3. Execute the script
Enter the folder directory through the terminal and run:
sudo ./pack.sh
There will be an error message that the command is not found, you can execute the following command, and use the file as an executable file
chmod a+x pack.sh
Then execute sudo ./pack.sh
the command again.
Then the dependent link library of the executable file will be copied to the directory where it is located.
at last
But one thing to note, it is better to use non-Linux FHS (File System Hierarchy Standard)
When creating a folder, the structure is as follows
.
├── build
├── CI
│ ├── build.sh
│ ├── exe
├── include
│ ├── bchartdir.h
├── lib
│ ├── libchartdir.so
│ ├── libchartdir.so.6.0
│ └── libchartdir.so.6.0.0
├── src
│ ├── ubuntuapp.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
└── tool
└── deployqt
The content in build.sh is as follows
# /bin/bash
cd /home/wu/workspace/ubuntuapp/CI/exe
cp /home/wu/workspace/ubuntuapp/tool/deployqt ./
cd /home/wu/workspace/ubuntuapp/src
/home/wu/Qt5.10.1/5.10.1/gcc_64/bin/qmake /home/wu/workspace/ubuntuapp/src/ubuntuapp.pro -o /home/wu/workspace/ubuntuapp/build -spec linux-g++ CONFIG+=debug
make clean -C /home/wu/workspace/ubuntuapp/build
make -C /home/wu/workspace/ubuntuapp/build
cd /home/wu/workspace/ubuntuapp/CI/exe
cp /home/wu/workspace/ubuntuapp/build/UbuntuApp ./
./deployqt UbuntuApp -qmake="/home/wu/Qt5.10.1/5.10.1/gcc_64/bin/qmake"
rm deployqt
After writing, add permissions
sudo chmod 777 build.sh
The content in ubuntuapp.pro is as follows
QT += core gui
TARGET = UbuntuApp
TEMPLATE = app
CONFIG += C++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp \
mainwindow.cpp
HEADERS += mainwindow.h
INCLUDEPATH += ../include
LIBS += -L../lib -lchartdir
unix:MOC_DIR = ../build/moc
unix:OBJECTS_DIR = ../build/obj
In this way, after executing build.sh , you can get some files and folders in the exe folder
The hyperlink of the AppRun inside can be double-clicked to open the UbuntuApp program
Then, change the name of the exe folder, compress it into a zip format, and copy it to other machines for use
1. About qt.conf
This file specifies the running environment of the qt program, that is, we can use qt.conf to specify the running path and library path of the qt program. The content is as follows:
# Generated by linuxdeployqt
# https://github.com/probonopd/linuxdeployqt/
[Paths]
Prefix = ./ #程序的运行路径
Libraries = ./lib #程序的库路径
Plugins = plugins #插件路径
Imports = qml
Qml2Imports = qml
ld.sh/)** 后,就能在 exe 文件夹里得到 一些文件和文件夹
里面的 AppRun 的超链接可以双击后 打开 UbuntuApp 程序
然后,把 exe 文件夹改个名字,压缩成 zip 格式,就可以拷贝到其他机器上用了
##### **1.关于qt.conf**
这个文件指定了qt 程序的运行环境,即我们可以使用qt.conf 指定qt程序的运行路径和库路径。内容如下所示:
```shell
# Generated by linuxdeployqt
# https://github.com/probonopd/linuxdeployqt/
[Paths]
Prefix = ./ #程序的运行路径
Libraries = ./lib #程序的库路径
Plugins = plugins #插件路径
Imports = qml
Qml2Imports = qml