简体   繁体   中英

Distributing Qt/C++ application cross-platform

I'm struggling to deploy my Qt/C++ application, probably because I have not found a good introduction about this online. In brief my question is how do I setup an installation framework which requires only minimal, or preferably no, compilation before shipping to users .

I want to deploy the GUI to users on different platforms, who may or may not have admin rights on their machines. I have found different options:

  • Statically compile Qt -> statically compile an executable -> distribute the executable. With this setup I have encountered a Windows security warning, which requires admin privileges (I have not yet tried on Linux / macOS). And frankly this approach seems sub-optimal, as my compiler has no idea about how to compile optimally for my users.

  • Create an installer. But there I start to be confused... Do I need to provide a statically compiled executable of my GUI, or just of the installer, or neither? Or can I avoid pre-compiling on my side all together by using an installer from Qt with built-in compiler/libraries?

Obviously you need to build your application on each platform you want to distribute it to. Easiest way is to link all the QT libraries dynamically to your application. After that all you need to do is provide your application (as in exe file on windows, or executable on linux etc) and the QT libraries you used (DLLs on windows, SO file I think on linux etc)

For example (on windows) if your app is called MyApp and uses QTGui, QTWidgets and QTNetwork, then you have the following files to distribute:

  • MyApp.exe
  • QTCore.dll and few other DLLs needed called icu*.dll something, can't remember)
  • QTGui.dll
  • QTWidgets.dll
  • QTNetwork.dll

and you can zip them all in one zip, create an installer etc.

EDIT Few notes after the follow up in the comment.

The standard library (what you called default library that has vector class) is part of the c/c++ runtime (on windows) or installed on linux systems etc, so no, you don't have to worry about this. I can't say for all compilers but for some you can specify a flag/parameter to link this runtime statically (rarely there is a need to do this).

On windows there is a tool called dependency walker, which gives you the list of all DLLs needed for the application to run. On linux systems I don't know, never needed one really. But for your own application, you do know which libraries you need, since you wrote it :)

With this setup I have encountered a Windows security warning

You didn't sign the binaries. This issue has nothing to do with Qt. You'd face it even when distributing a trivial "Hello World".

Ensure that you sign all of the following:

  1. The executables.

  2. All DLLs that you redistribute and are not signed (verify each one).

  3. The installer.

my compiler has no idea about how to compile optimally for my users.

Since C++ doesn't use just-in-time compilation, this statement is a truism. When you dynamically link your compiler will also have "no idea" how to compile "optimally for your users" if you imply that you need CPU-specific variants of your code. This has to be addressed by having multiple executables, each compiled for a particular CPU, and selecting them on installation. I don't think you meant that, though. But then I have no idea what you mean by "compile optimally for my users".

Do I need to provide a statically compiled executable of my GUI

It's up to you. If you don't provide a statically compiled executable, you will need to provide all of the dependencies: the C++ runtime of your compiler, and all the libraries and plugins needed by Qt.

The procedure for producing a statically linked executable on Windows, Linux and OS X is identical. You start with a statically configured copy of Qt ( configure -static -static-runtime ), then build it, and then use that to build your application. The end product will be statically linked against C++ runtime and Qt libraries.

Do I need to provide a statically compiled executable of [...] the installer

Only if you compile the installer program yourself using a C++ compiler. Most installer generator packages take care of creating an installer that has no additional dependencies, ie you can run it on a bare Windows system.

can I avoid pre-compiling on my side all together by using an installer from Qt

Qt provides no pre-built installers for re-use.

You can use eg NSIS to deploy the compiler runtime, Qt libraries and plugins, and your application and any data files it needs.

Or you can statically compile your application so that it has no dependencies and is a single .exe file, and have it as a portable application. It could also self-install, ie you could bundle the installer within the application, and on startup the application could detect whether it's already installed, and if not it'd relaunch itself in administrative mode and perform the installation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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