[Seek Help] 有木有人知道c++ 和Qt的程序在linux以及window下怎么打包
Tofloor
poster avatar
捕风
deepin
2021-10-31 06:31
Author

有木有人知道c++ 和Qt的程序在linux以及window下怎么打包成一个独立可以运行的软件?

windows生成exe,linux直接生成二进制可执行文件?

 

Reply Favorite View the author
All Replies
派了个萌
deepin
2021-10-31 08:28
#1

就想问一下,qt里边预览跟实际运行的时候风格不一样是怎么回事

Reply View the author
海天鹰
deepin
2021-10-31 18:42
#2

把要打包的程序复制到单独到目录,linuxdeployqt 程序名 -appimage

缺陷:

1.打包需要在ubuntu16下。

2.纯界面、网络没问题,音频视频我还没有成功。

Reply View the author
安洛
deepin
2021-10-31 20:27
#3

如果没什么依赖的话其实直接生成可执行文件也是ok的。

就算要打包也得先生成可执行文件然后按官方wiki的教程打包吧。

Reply View the author
luming
deepin
2021-11-01 05:38
#4

1、在windows打包非常容易,直接用windowsdeployqt命令就可以

2、linux就比较麻烦,首先必须所用机器的glibc小于2.26版本。

大概过程如下:

1. 配置Qt环境

首先,我们先把Qt环境配置好,在~/.bashrc中加入:

 

1
2
3
4
export PATH=/opt/Qt/5.12.0/gcc_64/bin:$PATH
export LD_LIBRARY_PATH=/opt/Qt/5.12.0/gcc_64/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=/opt/Qt/5.12.0/gcc_64/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/opt/Qt/5.12.0/gcc_64/qml:$QML2_IMPORT_PATH

其中/opt/Qt目录要根据自己电脑上安装的Qt路径进行修改。  0

 然后执行source ~/.bashrc使配置生效。

2. 编译linuxdeployqt

项目地址: https://github.com/probonopd/linuxdeployqt.git 。

 虽然有放出编译好的包,但是由于我使用的是Ubuntu18, 系统版本过高,因此还是选择编译代码的方式。

 为了避免编译好的包运行时检测到我们的系统版本过高,不继续执行的问题,我们编译前,将tools/linuxdeployqt/main.cpp中的下述代码注释掉:

 

1
2
3
4
5
6
7
8
9
10
// 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";
      return 1;
    }*/

然后就可以使用cmake和make进行编译。生成好的可执行程序是tools/linuxdeployqt/linuxdeployqt

 最后为了方便使用,可以将生成的可执行程序拷贝到系统的/usr/local/bin/目录。

3. 打包

将Qt编译的好的程序拷贝到一个单独的文件夹中。

 然后执行linuxdeployqt appname. -appimage

一般情况下会很顺利的完成,当前目录下会有个Apprun,直接执行它就可以。

 但是有时候并不是那么顺利,应该是系统中还缺少相应的库。比如,我遇到的错误是:

 

1
2
3
4
ERROR: Could not start patchelf.
ERROR: Make sure it is installed on your $PATH.
ERROR: Error reading rpath with patchelf "libQt5Widgets.so" : ""
ERROR: Error reading rpath with patchelf "libQt5Widgets.so" : ""

这个错误是表明缺少需要的pathchelf工具,直接安装即可解决:

sudo apt install patchelf

然后又出现了下面这个错误:

ERROR: ldd outputLine: "libjasper.so.1 => not found"
ERROR: for binary: "/home/xl/Qt5.9.2/5.9.2/gcc_64/plugins/imageformats/libqjp2.so"
ERROR: Please ensure that all libraries can be found by ldd. Aborting.

这表明我们系统中是缺少了libqjp2.so这个库的。其实很奇怪,本地明明是已经可以跑起来了,为什么还缺少这个库文件。但是解决方法很简单,缺什么就装什么:

1
2
3
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

安装完成之后,就顺利打包了。
Reply View the author
捕风
deepin
2021-11-02 23:49
#5
luming

1、在windows打包非常容易,直接用windowsdeployqt命令就可以

2、linux就比较麻烦,首先必须所用机器的glibc小于2.26版本。

大概过程如下:

1. 配置Qt环境

首先,我们先把Qt环境配置好,在~/.bashrc中加入:

 

1
2
3
4
export PATH=/opt/Qt/5.12.0/gcc_64/bin:$PATH
export LD_LIBRARY_PATH=/opt/Qt/5.12.0/gcc_64/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=/opt/Qt/5.12.0/gcc_64/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/opt/Qt/5.12.0/gcc_64/qml:$QML2_IMPORT_PATH

其中/opt/Qt目录要根据自己电脑上安装的Qt路径进行修改。  0

 然后执行source ~/.bashrc使配置生效。

2. 编译linuxdeployqt

项目地址: https://github.com/probonopd/linuxdeployqt.git 。

 虽然有放出编译好的包,但是由于我使用的是Ubuntu18, 系统版本过高,因此还是选择编译代码的方式。

 为了避免编译好的包运行时检测到我们的系统版本过高,不继续执行的问题,我们编译前,将tools/linuxdeployqt/main.cpp中的下述代码注释掉:

 

1
2
3
4
5
6
7
8
9
10
// 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";
      return 1;
    }*/

然后就可以使用cmake和make进行编译。生成好的可执行程序是tools/linuxdeployqt/linuxdeployqt

 最后为了方便使用,可以将生成的可执行程序拷贝到系统的/usr/local/bin/目录。

3. 打包

将Qt编译的好的程序拷贝到一个单独的文件夹中。

 然后执行linuxdeployqt appname. -appimage

一般情况下会很顺利的完成,当前目录下会有个Apprun,直接执行它就可以。

 但是有时候并不是那么顺利,应该是系统中还缺少相应的库。比如,我遇到的错误是:

 

1
2
3
4
ERROR: Could not start patchelf.
ERROR: Make sure it is installed on your $PATH.
ERROR: Error reading rpath with patchelf "libQt5Widgets.so" : ""
ERROR: Error reading rpath with patchelf "libQt5Widgets.so" : ""

这个错误是表明缺少需要的pathchelf工具,直接安装即可解决:

sudo apt install patchelf

然后又出现了下面这个错误:

ERROR: ldd outputLine: "libjasper.so.1 => not found"
ERROR: for binary: "/home/xl/Qt5.9.2/5.9.2/gcc_64/plugins/imageformats/libqjp2.so"
ERROR: Please ensure that all libraries can be found by ldd. Aborting.

这表明我们系统中是缺少了libqjp2.so这个库的。其实很奇怪,本地明明是已经可以跑起来了,为什么还缺少这个库文件。但是解决方法很简单,缺什么就装什么:

1
2
3
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

安装完成之后,就顺利打包了。

放弃了,直接换成go语言了。

Reply View the author
cuisanzhang
deepin
2021-11-03 00:45
#6

看到你这教程我直接关机了, 打包个毛线. 再见,这货反人类,原因是为什么, 没有图形界面, 内核天天升级.

Reply View the author
luming
deepin
2021-11-04 19:23
#7
捕风

放弃了,直接换成go语言了。

不用放弃,其实c++很好用的,编译出来本身就是可执行文件,如果不需要打包,直接把各种动态库复制过来就行了。

Reply View the author