Question:
I'm developing a solution in QtCreator Qt version 5.4.2. Well, I have the following problem: When I run on my machine and any Ubuntu Vivid (15.04) it works. But when I try to run on Ubuntu 14.04 LTS it gives an error. There are ways to make this application work on various distros. From what I researched, the best is:
You can write a startup script for your application, where you modify the dynamic linker configuration (eg, adding your application's directory to the LD_LIBRARY_PATH environment variable. Site Source
Only I can't figure out how to do this or the script. I need this application to work without being .deb, that is, without needing to be installed and without the user having to install anything for it to work. Can someone who already works with QT help me? Att
Answer:
To do this you need to know how to link the executable to the libraries you are distributing and which libraries to distribute.
Linking executable to libraries
There are two ways to link your executable to the Qt libraries you are distributing with your package: create a script and export the LD_LIBRARY_PATH
and change the RPATH
of the executable.
Create Bash Script
Way number 1 is the one mentioned on the site: create a script adding the directory containing your libraries to LD_LIBRARY_PATH
. Since you are distributing a program for Linux, I recommend making a Bash script. Assuming all the libraries and executable are in a directory called app
, an example script would be:
# Caminho absoluto para o diretório onde o script se encontra
# Achei esse truque no SO internacional, mas não tenho o link salvo
DIRETORIO_ATUAL="$(cd "$(dirname "$0")"; pwd)"
# Coloca o diretório com as bibliotecas no LD_LIBARY_PATH:
export LD_LIBRARY_PATH="${DIRETORIO_ATUAL}/app"
# Executa o aplicativo:
./app/aplicativo
There are lots of great tutorials and tips on the internet about Bash on Google and on the OS itself, so do some research and you'll be fine – and post your questions here on the OS in Portuguese.
Change RPATH
Another way to link libraries to your app is to change its RPATH
. The RPATH
is a list of directories inserted into your executable's binary at compile time; ld
, which is the program that links libraries to the executable, looks, among other places, in the application's RPATH
to determine where to look for libraries.
If you're using qmake, I don't recommend this method, as qmake gives you some RPATH
to modify the binary's RPATH
. If you're using CMake (which I recommend), it's relatively simple:
set(CMAKE_INSTALL_RPATH "\$ORIGIN")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
Where \$ORIGIN
the directory where the executable is located. If you wanted, for example, to put all libraries in a directory called diretorio
, you would use "\$ORIGIN/diretorio"
.
Required libraries
dependencies
It really depends on what you are using, but the most basic distribution of Qt on an OS such as Ubuntu needs the following libraries:
-
libQt5Core.so.5
-
libQt5DBus.so.5
-
libQt5Gui.so.5
-
libQt5Widgets.so.5
-
libicudata.so.53
-
libicui18n.so.53
-
libicuuc.so.53
This is assuming Qt 5.4. The libicu*
version may change depending on the version of Qt – and at this point I recommend you use Qt 5.6, which is the latest stable release and which is an extended support release.
Without the above libraries, the program won't even start. If you are going to run in a terminal and a library that the executable depends on is missing, a message similar to this one will appear:
./aplicativo: error while loading shared libraries: libQt5Widgets.so.5: cannot open shared object file: No such file or directory
To verify that all libraries that the executable depends on are present, use ldd
.
Plugins
Furthermore, you also need to distribute the plugins. This is the most complicated part, because it's something that doesn't give your machine a problem, as it has all the plugins installed and all the environment variables set, and the necessary plugins don't show up in ldd
.
You can find the plugins in the $QTDIR/plugins
folder. On Linux, the minimum plugins you have to distribute are as follows:
-
platforms/libqxcb.so
-
platformthemes/libqgtk2.so
, if you want the application to adopt the system theme in Ubuntu and GNOME
It's important to note that the plugin directory structure must be maintained – that is, if you simply libqxcb.so
into your libraries directory, it won't work.
Basically, that's it. You might still have a few more things to do, but doing that is a bit of a trial and error process at times. Hope my guide is helpful!