- Qt Documentation
- Contents
- The Plugin Directory
- Loading and Verifying Plugins Dynamically
- Debugging Plugins
- Qt Documentation
- Contents
- Static Linking
- Building Qt Statically
- Linking the Application to the Static Version of Qt
- Shared Libraries
- Building Qt as a Shared Library
- Linking the Application to Qt as a Shared Library
- Creating the Application Package
- Application Dependencies
- Additional Libraries
Qt Documentation
Contents
This topic explains how to deploy plugin libraries for Qt or your application to load at runtime. If you use static plugins, then the plugin code is already part of your application executable and no separate deployment steps are required.
The Plugin Directory
In Qt, when an application starts, the application’s executable directory is the base directory where Qt searches for plugins.
For example, on Windows, if the application is in C:\Program Files\MyApp and it has a style plugin, Qt looks in C:\Program Files\MyApp\styles .
To find out where your application’s executable is located, see QCoreApplication::applicationDirPath().
Qt also looks in the directory specified by QLibraryInfo::location(QLibraryInfo::PluginsPath), which typically is located in QTDIR/plugins ; QTDIR is the directory where Qt is installed. If you want Qt to look in additional places you can add as many paths as you need with calls to QCoreApplication::addLibraryPath(). If you want to set your own path(s), you can use QCoreApplication::setLibraryPaths().
Alternatively, you can use a qt.conf file to override the hard-coded paths that are compiled into the Qt library. For more information, see Using qt.conf.
Another possibility is to set the QT_PLUGIN_PATH environment variable before you run the application; multiple paths can be separated with a system path separator. When set, Qt looks for plugins in the paths specified in this variable.
Note: Do not export QT_PLUGIN_PATH as a system-wide environment variable because it can interfere with other Qt installations.
Loading and Verifying Plugins Dynamically
When loading plugins, the Qt library does some sanity checking to determine whether the plugin can be loaded and used. This sanity check enables you to have multiple Qt versions and configurations installed side by side.
The following rules apply:
- Plugins linked with a Qt library that has a higher version number will not be loaded by a library with a lower version number.
Example: Qt 5.0.0 will not load a plugin built with Qt 5.0.1.
Plugins linked with a Qt library that has a lower major version number will not be loaded by a library with a higher major version number.
Example: Qt 5.0.1 will not load a plugin built with Qt 4.8.2.
Example: Qt 5.1.1 will load plugins built with Qt 5.1.0 and Qt 5.0.3.
When building plugins to extend an application, it’s important to ensure that the plugin is configured in the same way as the application. This means that if the application was built in release mode, plugins should be built in release mode, too. Except for Unix operating systems, where the plugin system will not load plugins built in a different mode from the application.
If you configure Qt to be built in both debug and release modes, but only build your applications in release mode, you need to ensure that your plugins are also built in release mode. By default, if a debug build of Qt is available, plugins will only be built in debug mode. To force the plugins to be built in release mode, add the following line to the plugin’s project ( .pro ) file:
This ensures that the plugin is compatible with the version of the library used in the application.
Debugging Plugins
There are a number of issues that may prevent correctly-written plugins from working with the applications that are designed to use them. Many of these are related to differences in the way that plugins and applications have been built, often arising from separate build systems and processes.
To obtain diagnostic information from Qt, about each plugin it tries to load, use the QT_DEBUG_PLUGINS environment variable. Set this variable to a non-zero value in the environment where your application is launched.
The following table describes the common causes of problems developers experience when creating plugins and possible solutions.
Problem | Cause | Solution |
---|---|---|
Plugins sliently fail to load even when opened directly by the application. Qt Designer shows the plugin libraries in its Help|About Plugins dialog, but no plugins are listed under each of them. | The application and its plugins are built in different modes. | Either share the same build information or build the plugins in both debug and release modes by appending the debug_and_release to the CONFIG variable in each of their project files. |
В© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Источник
Qt Documentation
Contents
This documentation discusses specific deployment issues for Qt for Linux/X11. We will demonstrate the procedures in terms of deploying the Plug & Paint application that is provided in Qt’s examples directory.
Due to the proliferation of Unix systems (such as commercial Unixes, Linux distributions, and so on), deployment on Unix is a complex topic. Before we start, be aware that programs compiled for one Unix flavor will probably not run on a different Unix system. For example, unless you use a cross-compiler, you cannot compile your application on Irix and distribute it on AIX.
Static Linking
Static linking is often the safest and easiest way to distribute an application on Unix since it relieves you from the task of distributing the Qt libraries and ensuring that they are located in the default search path for libraries on the target system.
Building Qt Statically
To use this approach, you must start by installing a static version of the Qt library:
We specify the prefix so that we do not overwrite the existing Qt installation. The example above only builds the Qt libraries, i.e. the examples and Qt Designer will not be built. When make is done, you will find the Qt libraries in the /path/to/Qt/lib directory.
When linking your application against static Qt libraries, note that you might need to add more libraries to the LIBS line in your project file. For more information, see the Application Dependencies section.
Linking the Application to the Static Version of Qt
Once Qt is built statically, the next step is to regenerate the makefile and rebuild the application. First, we must go into the directory that contains the application:
Now run qmake to create a new makefile for the application, and do a clean build to create the statically linked executable:
You probably want to link against the release libraries, and you can specify this when invoking qmake . Note that we must set the path to the static Qt that we just built.
To check that the application really links statically with Qt, run the ldd tool (available on most Unices):
Verify that the Qt libraries are not mentioned in the output.
Now, provided that everything compiled and linked without any errors, we should have a plugandpaint file that is ready for deployment. One easy way to check that the application really can be run stand-alone is to copy it to a machine that doesn’t have Qt or any Qt applications installed, and run it on that machine.
Remember that if your application depends on compiler specific libraries, these must still be redistributed along with your application. For more information, see the Application Dependencies section.
The Plug & Paint example consists of several components: The core application (Plug & Paint), and the Basic Tools and Extra Filters plugins. Since we cannot deploy plugins using the static linking approach, the executable we have prepared so far is incomplete. The application will run, but the functionality will be disabled due to the missing plugins. To deploy plugin-based applications we should use the shared library approach.
Shared Libraries
We have two challenges when deploying the Plug & Paint application using the shared libraries approach: The Qt runtime has to be correctly redistributed along with the application executable, and the plugins have to be installed in the correct location on the target system so that the application can find them.
Building Qt as a Shared Library
We assume that you already have installed Qt as a shared library, which is the default when installing Qt, in the /path/to/Qt directory.
Linking the Application to Qt as a Shared Library
After ensuring that Qt is built as a shared library, we can build the Plug & Paint application. First, we must go into the directory that contains the application:
Now run qmake to create a new makefile for the application, and do a clean build to create the dynamically linked executable:
This builds the core application, the following will build the plugins:
If everything compiled and linked without any errors, we will get a plugandpaint executable and the libpnp_basictools.so and libpnp_extrafilters.so plugin files.
Creating the Application Package
There is no standard package management on Unix, so the method we present below is a generic solution. See the documentation for your target system for information on how to create a package.
To deploy the application, we must make sure that we copy the relevant Qt libraries (corresponding to the Qt modules used in the application), the platform plugin, and the executable to the same directory tree. Remember that if your application depends on compiler specific libraries, these must also be redistributed along with your application. For more information, see the Application Dependencies section.
We’ll cover the plugins shortly, but the main issue with shared libraries is that you must ensure that the dynamic linker will find the Qt libraries. Unless told otherwise, the dynamic linker doesn’t search the directory where your application resides. There are many ways to solve this:
- You can install the Qt libraries in one of the system library paths (e.g. /usr/lib on most systems).
- You can pass a predetermined path to the -rpath command-line option when linking the application. This will tell the dynamic linker to look in this directory when starting your application.
- You can write a startup script for your application, where you modify the dynamic linker configuration (e.g., adding your application’s directory to the LD_LIBRARY_PATH environment variable.
Note: If your application will be running with «Set user ID on execution,» and if it will be owned by root, then LD_LIBRARY_PATH will be ignored on some platforms. In this case, use of the LD_LIBRARY_PATH approach is not an option).
The disadvantage of the first approach is that the user must have super user privileges. The disadvantage of the second approach is that the user may not have privileges to install into the predetermined path. In either case, the users don’t have the option of installing to their home directory. We recommend using the third approach since it is the most flexible. For example, a plugandpaint.sh script will look like this:
By running this script instead of the executable, you are sure that the Qt libraries will be found by the dynamic linker. Note that you only have to rename the script to use it with other applications.
When looking for plugins, the application searches in a plugins subdirectory inside the directory of the application executable. Either you have to manually copy the plugins into the plugins directory, or you can set the DESTDIR in the plugins’ project files:
An archive distributing all the Qt libraries, and all the plugins, required to run the Plug & Paint application, would have to include the following files:
Component | File Name | |
---|---|---|
The executable | plugandpaint | |
The script to run the executable | plugandpaint.sh | |
The Basic Tools plugin | plugins\libpnp_basictools.so | |
The ExtraFilters plugin | plugins\libpnp_extrafilters.so | |
The Qt xcb platform plugin | platforms\libqxcb.so | |
The Qt Core module | libQt5Core.so.5 | |
The Qt GUI module | libQt5Gui.so.5 | |
The Qt Widgets module | libQt5Widgets.so.5 |
On most systems, the extension for shared libraries is .so . A notable exception is HP-UX, which uses .sl .
Remember that if your application depends on compiler specific libraries, these must still be redistributed along with your application. For more information, see the Application Dependencies section.
To verify that the application now can be successfully deployed, you can extract this archive on a machine without Qt and without any compiler installed, and try to run it, i.e. run the plugandpaint.sh script.
An alternative to putting the plugins in the plugins subdirectory is to add a custom search path when you start your application using QApplication::addLibraryPath() or QApplication::setLibraryPaths().
Application Dependencies
Additional Libraries
To find out which libraries your application depends on, run the ldd tool (available on most Unices):
This will list all the shared library dependencies for your application. Depending on configuration, these libraries must be redistributed along with your application. In particular, the standard C++ library must be redistributed if you’re compiling your application with a compiler that is binary incompatible with the system compiler. When possible, the safest solution is to link against these libraries statically.
You will probably want to link dynamically with the regular X11 libraries, since some implementations will try to open other shared libraries with dlopen() , and if this fails, the X11 library might cause your application to crash.
It’s also worth mentioning that Qt will look for certain X11 extensions, such as Xinerama and Xrandr, and possibly pull them in, including all the libraries that they link against. If you can’t guarantee the presence of a certain extension, the safest approach is to disable it when configuring Qt (e.g. ./configure -no-xrandr ).
FontConfig and FreeType are other examples of libraries that aren’t always available or that aren’t always binary compatible. As strange as it may sound, some software vendors have had success by compiling their software on very old machines and have been very careful not to upgrade any of the software running on them.
When linking your application against the static Qt libraries, you must explicitly link with the dependent libraries mentioned above. Do this by adding them to the LIBS variable in your project file.
From Qt version 5.2 onwards, the officially supported version for OpenSSL is 1.0.0 or later. Versions >= 0.9.7 and libqxcb.so . This file must be located within a specific subdirectory (by default, platforms ) under your distribution directory. Alternatively, it is possible to adjust the search path Qt uses to find its plugins, as described below.
Your application may also depend on one or more Qt plugins, such as the JPEG image format plugin or a SQL driver plugin. Be sure to distribute any Qt plugins that you need with your application. Similar to the platform plugin, each type of plugin must be located within a specific subdirectory (such as imageformats or sqldrivers ) within your distribution directory.
The search path for Qt plugins (as well as a few other paths) is hard-coded into the QtCore library. By default, the first plugin search path will be hard-coded as /path/to/Qt/plugins . As mentioned above, using predetermined paths has certain disadvantages, so you need to examine various alternatives to make sure that the Qt plugins are found:
- Using qt.conf . This is the recommended approach since it provides the most flexibility.
- Using QApplication::addLibraryPath() or QApplication::setLibraryPaths().
- Using a third party installation utility or the target system’s package manager to change the hard-coded paths in the QtCore library.
The How to Create Qt Plugins document outlines the issues you need to pay attention to when building and deploying plugins for Qt applications.
В© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Источник