简体   繁体   English

如何为 Linux 部署 Qt 应用程序

[英]How to deploy Qt applications for Linux

I followed all the steps successfully as mention in the Qt documentation:我成功地按照 Qt 文档中提到的所有步骤进行了操作:

But I still couldn't make static Qt application, the executable generated by the above documented steps still needs Qt shared objects on other system.但是我仍然无法制作静态Qt应用程序,上述文档步骤生成的可执行文件仍然需要其他系统上的Qt共享对象。

Any ideas?有任何想法吗?

You need to deploy the application, for this purpose I use the utility cqtdeployer您需要部署应用程序,为此我使用实用程序cqtdeployer

This utility itself collects all the necessary dependencies of your application and you do not have to spend your time on it, or you can automate this process.此实用程序本身会收集应用程序的所有必要依赖项,您无需花费时间,或者您可以自动执行此过程。

You can install from github releases (Windows)您可以从github版本安装 (Windows)

or要么

from snapstore (Linux)来自snapstore (Linux)

sudo snap install cqtdeployer

You can use as follows:您可以按如下方式使用:

  • Windows:视窗:
%cqtdeployer% -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake.exe -qmlDir path/to/my/qml/files/dir
  • Linux: Linux:
cqtdeployer -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake -qmlDir path/to/my/qml/files/dir
  • path/to/Qt/5.xx/build/bin/qmake - This is the way qmake is used to build your program. path/to/Qt/5.xx/build/bin/qmake - 这是使用 qmake 构建程序的方式。

  • path/to/my/qml/files/dir - this is the path directly to your qml file (which you wrote) path/to/my/qml/files/dir - 这是直接指向您的 qml 文件(您编写的)的路径

And Run application with sh script (Linux) or exe (Windows)并使用 sh 脚本 (Linux) 或 exe (Windows) 运行应用程序

If you'll use the version from snap then make sure that you have all the permissions.如果您将使用 snap 中的版本,请确保您拥有所有权限。 cqtdeployer

If you need use windows version just install application from installer如果您需要使用 Windows 版本,只需从安装程序安装应用程序

Updated更新

If you want create a simple installer for you application just add qif option for command of cqtdeployer.如果您想为您的应用程序创建一个简单的安装程序,只需为cqtdeployer的命令添加qif选项。 Example :例子 :

cqtdeployer -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake -qmlDir path/to/my/qml/files/dir qif

Details on all the intricacies of cqtdeployer can be found on the official wiki project.有关 cqtdeployer 的所有复杂功能的详细信息可以在官方wiki项目中找到。

The best way to deploy your application is not necessarily to statically link it for the following reasons:部署应用程序的最佳方式不一定是静态链接它,原因如下:

  • LGPL licencing means that your application must now be made public and may not sold (I think) - ie since its statically linked and the qt libs are within your executable your executable is now part of the open source. LGPL 许可意味着您的应用程序现在必须公开并且不得出售(我认为) - 即,由于它的静态链接和 qt 库在您的可执行文件中,您的可执行文件现在是开源的一部分。
  • Its a massive pain in the arse... I have gone around this loop and know the pain well.这是屁股的巨大痛苦......我已经绕过这个循环并且很了解这种痛苦。

Installing qt-everywhere is also not so great, I just don't see how you can garantee that the libraries will be the same version as the ones that your program needs.安装 qt-everywhere 也不是很好,我只是不知道您如何保证这些库与您的程序需要的库版本相同。

So what I started to do was create my own script to deploy qt for me.所以我开始做的是创建我自己的脚本来为我部署 qt。 The basic "jist" of this is that you use ldd to find out which qt libraries you need and copy them into a sub folder ( ./lib ) within the same folder as your executable to make an install bundle.基本的“jist”是您使用ldd找出您需要的 qt 库,并将它们复制到与可执行文件相同的文件夹中的子文件夹 ( ./lib ) 中,以制作安装包。

Note: on Windows there is a deployqt application which does somthing similar (can't remember exactly what it is called).注意:在 Windows 上有一个 deployqt 应用程序,它做类似的事情(记不清它叫什么了)。

Below I have copied a version of my deploy script.下面我复制了我的部署脚本的一个版本。 Note that it is quite old now, but I don't see why it should not work (its not written particularly well), but if not it will give you a start place.请注意,它现在已经很老了,但我不明白为什么它不能工作(它写得不是特别好),但如果不是,它会给你一个起点。 Also look out for the plugin's.还要注意插件的。 In this script I have added code to copy the audio plugin since I was using that.在这个脚本中,我添加了代码来复制音频插件,因为我正在使用它。 If you are using other plugins then you will need to copy those (they are usually in sub dir's of the qt libs like .../audio)... I had a todo to try to figure out what plugins are needed from the .pro file but I never got around to that (I would have to pass in the .pro file to this script as well)...如果您正在使用其他插件,那么您将需要复制这些插件(它们通常位于 qt 库的子目录中,例如 .../audio)...我有一个待办事项来尝试从 . pro 文件,但我从来没有考虑过(我也必须将 .pro 文件传递​​给这个脚本)......

To run, just run this script and pass in the directory that your executable lives in.要运行,只需运行此脚本并传入可执行文件所在的目录。

#!/bin/bash

# Rememeber start dir
START_DIR=$PWD

# Determine which dir to deploy in and cd to that dir
if [ -d "$1" ]; then
   DEPLOY_DIR=$1
else
   DEPLOY_DIR=$PWD
fi
echo "Deploy dir: $DEPLOY_DIR"
cd $DEPLOY_DIR

# Run ldd on all files in the directory and create a list of required qt libs
flag=false
for entry in `ldd $DEPLOY_DIR/* | grep -i qt`; do
   if $flag; then
      # Only add to the array if it is not already in it
      if ! [[ $libsArray =~ $entry ]]; then
         echo "adding $entry"
         libsArray="$libsArray $entry"
      fi
      flag=false
   fi

   # If we see a "=>" then the next line will be a library
   if [ $entry == "=>" ]; then
      flag=true
   fi
done
echo 
echo

# Create the required folder structure. Note here we are need the qt audio plugin so we are going to manually copy that as well.
mkdir -p lib
mkdir -p lib/audio
# Now copy these files to the deploy directory
for entry in $libsArray; do
   echo "cp -v -f $entry $DEPLOY_DIR/lib"
   cp -v -f $entry $DEPLOY_DIR/lib
done

# Now get the audio lib - this is a plugin that we are using so we need these libs as well.
# Add other plugins here as well.
# TODO: maybe we can read this in from the *.pro file.
cp -v -f `qmake -query QT_INSTALL_BINS`/../plugins/audio/* $DEPLOY_DIR/lib/audio

# Go back to start dir
cd $START_DIR

Once you have all the files you need you should be able to copy the whole lot to another PC and run it.拥有所需的所有文件后,您应该能够将整个文件复制到另一台 PC 并运行它。 Note: you may have to set the export LD_LIBRARY_PATH=<path-to-libs> so that the libs can be found... or install the libs into somewhere like /usr/lib/your-appplication/ .注意:您可能必须设置export LD_LIBRARY_PATH=<path-to-libs>以便可以找到库...或将库安装到诸如/usr/lib/your-appplication/

But installing libs is another question/subject!但是安装库是另一个问题/主题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM