I'm a Qt beginner and I want to know how to build Qt5 apps with their static libraries. Is that possible to build with static libs everything that can be build with dll's ?. I want to know because I don't want to install Qt libs everywhere where I want my apps to be running. For example I want to build with static libs app which code is in this post:
Widgets must be created in the GUI thread Error !. How to correct the code?
Is that possible ?
How to do that ?
In order to use Qt as a static library, you have to rebuild Qt itself.
You can find a guide here
It recommends rebuilding Qt as following:
cd C:\path\to\Qt
configure -static <any other options you need>
nmake sub-src
Make sure you've embedded all plugins you need. (see configure options)
However, not all Qt parts can be built as a static libraries. As far as I know, you may expose some difficulties with Webkit.
After rebuilding Qt, you can build your apps, as usual.
Note also, that Qt is licensed under LGPL or GPL or commercial licence. Using static version of Qt can impose some limitations on your app distributions.
Related
I'm trying to build an installer for a Qt-based application. It depends on some Qt libraries, which is not a problem to build an installer, but it also depends on 3rd party libraries: OpenCV, SuiteSparse, FFTW, and some other libraries coded by me.
Is it possible to create a "one-click" installer with Qt Creator that handles both Qt libraries and these too? Or should I create a script that installs the dependencies and integrate it somehow in the Qt Creator? If so, how can I do that?
I'd like to pass most of the responsibility to Qt because this application should be installable in both OSX (in which I'm developing) and Windows, and I wanted to avoid maintaining two codebases if possible.
I am trying to deploy(release to public) a simple qt application I made recently, but got stuck at static linking qt libs.
I followed the guide on qt docs to re-build qt and my app statically. But the release build still require qtgui / qtcore dll for no apparent reasons, I wonder if anyone has seen this kind of problems before ? Or even better, has successfully resolved it ?
http://doc.qtsoftware.com/4.5/deployment-windows.html
I wrote a guide to static linking
and
How to build Qt static with multiple compilers and keep it small
(because it can get pretty big, especially for simple programs).
You may also want to check out the BitRock installer, which is free for open source projects.
In short, it turns out to be a little more complex if you are using anything Qt thinks of as a plugin, such as support for most image types (JPEG, GIF) or databases.
For example, if you want to include support for Oracle DBMS and GIF images for your icons, you add the following to your .PRO file:
QTPLUGIN += qsqloci qgif
CONFIG += static
You will then need to:
#include <QtPlugin>
in your project, and import any plugins used. You need to change these settings back order to get it to compile with dynamic linking again (like when debugging or adding features), though this can be easily automated. There are also considerations when building the Qt libraries for use with static linking, though the Qt instructions will at least get you started.
With Qt 5.5, things are quite easy. There's the configure script you have to run before building Qt. There are following orthogonal settings that you pass to configure:
Do you want a static Qt library?
-static option should be passed to configure
Do you want the build of Qt, and of your application, to use a static C++ runtime?
-static-runtime option should be passed to configure
Do you want XP targeting?
-target xp option should be passed to configure
Additionally, follow the instructions from this blog post.
Qt Creator didn't support XP targeting automagically at least until v.3.5.0 since it doesn't set up the environment for the build tools properly. You have to modify the build environment manually per the blog post.
Also, be aware that your static build will still link to the visual studio runtimes dynamically!
See this faq (internet archive link, in case the link goes away) :
Why does a statically built Qt use the dynamic Visual Studio runtime libraries ? Do I need to deploy those with my application ?
Qt is built using the -MD(d) switch, which links against the dynamic C/C++ runtime libraries. This is necessary as we have experienced memory problems when using anything but the -MD(d) flag, and in general, it is recommended to use. You should not alter this flag yourself for your application, because it conflicts with how the Qt library is built if you change the flag to -MT. You should not change it for Qt either, since it is likely to cause problems.
Qt is still built statically when using the -static option though, meaning you do not need to distribute the Qt dlls when deploying your application. You will have to distribute the C runtimes though (if they don't already exist on the target machine), see our deployment documentation http://doc.qt.io/qt-5/windows-deployment.html#application-dependencies.
msys2 has a pre-built static Qt5 package
e.g. pacman -S mingw-w64-qt5-static
Currently, to also get all dependencies of Qt statically linked, with CMake, you need to add:
list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
before calling find_package(Qt5…
CMAKE_AUTOSTATICPLUGINS was replaced by a new upstream implementation and is not needed anymore.
I just compiled an application statically (Debug)
with QT Plugins(5.9),
with VS (2015) (Win).
a) Add to your code.
#include <QtPlugin>
Q_IMPORT_PLUGIN (QWindowsIntegrationPlugin);
b) Add the following to the link paths
\5.9.0_x86_static_install\lib
\5.9.0_x86_static_install\bin
\5.9.0_x86_static_install\plugins
\5.9.0_x86_static_install\plugins\platforms
\5.9.0_x86_static_install\plugins\imageformats
c) Add the list of QT static libraries and internal VS libraries to your link list.
version.lib
imm32.lib
shlwapi.lib
rpcrt4.lib
Ws2_32.lib
Mpr.lib
Netapi32.lib
Rpcrt4.lib
Iphlpapi.lib
winmm.lib
gdi32.lib
advapi32.lib
msimg32.lib
UxTheme.lib
translatord.lib
preprocessord.lib
d3d9.lib
dxguid.lib
libEGLd.lib
libGLESv2d.lib
iphlpapi.lib
psapi.lib
ws2_32.lib
Dwmapi.lib
Qt5CoreD.lib
Qt5Guid.lib
Qt5Xmld.lib
Qt5Widgetsd.lib
Qt5Networkd.lib
Qt5Winextrasd.lib
Qt5PlatformCompositorSupportd.lib
qicod.lib
qtmaind.lib
qtlibpngd.lib
qtharfbuzzd.lib
qtpcre2d.lib
qwindowsd.lib
Qt5FontDatabaseSupportd.lib
Qt5ThemeSupportd.lib
Qt5EventDispatcherSupportd.lib
Qt5AccessibilitySupportd.lib
qtfreetyped.lib
Kevin Higgins
I recommend you to use linuxdeployqt this tool. It can help solve most of the dependency problems. And I use it to package my qt application successfully.
This answer applies to using MSYS2 with mingw-w64 as the compiler, in Windows, and qmake. The QT version is 5.15.0.
Installing the package qt5-static gives you a build of QT that produces executables who don't rely on any QT DLLs. (Example commandline - pacman -Ss mingw64/mingw-w64-x86_64-qt5-static).
However a new problem is introduced here: it does not pass the -static flag to gcc. Meaning that although the executable does not depend on QT DLLs, it does depend on libgcc-s, libwinpthread.dll etc.
Normally, this problem would be fixed by using CONFIG += static which causes qmake to pass -static to gcc. However, and as noted in the other answers, for a qt-static build that config option is ignored!
To solve this I had to manually specify the gcc flags for static linking in the qmake file, i.e. :
QMAKE_CXXFLAGS += -static
QMAKE_LFLAGS_WINDOWS += -static
Which results in a (large) binary with no external dependencies other than Windows system DLLs.
(In case it matters: my use case was building a COM in-process server DLL that should have no external dependencies; I was also using TEMPLATE=lib and CONFIG += dll).
I downloaded qt recently.
I want to create an standalone exe,
but I don't know what can I do.
In Qt5.2, the folder "(Qt)\lib" is exists, but i can't use it in Qt Creator.
So, should I build from source,
or configure Qt Creator and use the "lib***.a"?
Any ideas?
Environment: MinGW-32 4.8.1(TDM) / Windows 7
You seem to have tried CONFIG+=static, but that is not meant for this use case. That is used when you would like to use build your library to be static after the end of the build.
This is not the case here because you already have static Qt libraries available, so what you wish instead, to link those statically against your executable.
You would need to use this in your qmake project file:
LIBS += -L/path/to/the/static/QtCore -lQtCore
You could also use, albeit this would make the build-system less portable across different platforms:
LIBS += /path/to/the/statis/QtCore/libQtCore.a
I am trying to deploy(release to public) a simple qt application I made recently, but got stuck at static linking qt libs.
I followed the guide on qt docs to re-build qt and my app statically. But the release build still require qtgui / qtcore dll for no apparent reasons, I wonder if anyone has seen this kind of problems before ? Or even better, has successfully resolved it ?
http://doc.qtsoftware.com/4.5/deployment-windows.html
I wrote a guide to static linking
and
How to build Qt static with multiple compilers and keep it small
(because it can get pretty big, especially for simple programs).
You may also want to check out the BitRock installer, which is free for open source projects.
In short, it turns out to be a little more complex if you are using anything Qt thinks of as a plugin, such as support for most image types (JPEG, GIF) or databases.
For example, if you want to include support for Oracle DBMS and GIF images for your icons, you add the following to your .PRO file:
QTPLUGIN += qsqloci qgif
CONFIG += static
You will then need to:
#include <QtPlugin>
in your project, and import any plugins used. You need to change these settings back order to get it to compile with dynamic linking again (like when debugging or adding features), though this can be easily automated. There are also considerations when building the Qt libraries for use with static linking, though the Qt instructions will at least get you started.
With Qt 5.5, things are quite easy. There's the configure script you have to run before building Qt. There are following orthogonal settings that you pass to configure:
Do you want a static Qt library?
-static option should be passed to configure
Do you want the build of Qt, and of your application, to use a static C++ runtime?
-static-runtime option should be passed to configure
Do you want XP targeting?
-target xp option should be passed to configure
Additionally, follow the instructions from this blog post.
Qt Creator didn't support XP targeting automagically at least until v.3.5.0 since it doesn't set up the environment for the build tools properly. You have to modify the build environment manually per the blog post.
Also, be aware that your static build will still link to the visual studio runtimes dynamically!
See this faq (internet archive link, in case the link goes away) :
Why does a statically built Qt use the dynamic Visual Studio runtime libraries ? Do I need to deploy those with my application ?
Qt is built using the -MD(d) switch, which links against the dynamic C/C++ runtime libraries. This is necessary as we have experienced memory problems when using anything but the -MD(d) flag, and in general, it is recommended to use. You should not alter this flag yourself for your application, because it conflicts with how the Qt library is built if you change the flag to -MT. You should not change it for Qt either, since it is likely to cause problems.
Qt is still built statically when using the -static option though, meaning you do not need to distribute the Qt dlls when deploying your application. You will have to distribute the C runtimes though (if they don't already exist on the target machine), see our deployment documentation http://doc.qt.io/qt-5/windows-deployment.html#application-dependencies.
msys2 has a pre-built static Qt5 package
e.g. pacman -S mingw-w64-qt5-static
Currently, to also get all dependencies of Qt statically linked, with CMake, you need to add:
list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
before calling find_package(Qt5…
CMAKE_AUTOSTATICPLUGINS was replaced by a new upstream implementation and is not needed anymore.
I just compiled an application statically (Debug)
with QT Plugins(5.9),
with VS (2015) (Win).
a) Add to your code.
#include <QtPlugin>
Q_IMPORT_PLUGIN (QWindowsIntegrationPlugin);
b) Add the following to the link paths
\5.9.0_x86_static_install\lib
\5.9.0_x86_static_install\bin
\5.9.0_x86_static_install\plugins
\5.9.0_x86_static_install\plugins\platforms
\5.9.0_x86_static_install\plugins\imageformats
c) Add the list of QT static libraries and internal VS libraries to your link list.
version.lib
imm32.lib
shlwapi.lib
rpcrt4.lib
Ws2_32.lib
Mpr.lib
Netapi32.lib
Rpcrt4.lib
Iphlpapi.lib
winmm.lib
gdi32.lib
advapi32.lib
msimg32.lib
UxTheme.lib
translatord.lib
preprocessord.lib
d3d9.lib
dxguid.lib
libEGLd.lib
libGLESv2d.lib
iphlpapi.lib
psapi.lib
ws2_32.lib
Dwmapi.lib
Qt5CoreD.lib
Qt5Guid.lib
Qt5Xmld.lib
Qt5Widgetsd.lib
Qt5Networkd.lib
Qt5Winextrasd.lib
Qt5PlatformCompositorSupportd.lib
qicod.lib
qtmaind.lib
qtlibpngd.lib
qtharfbuzzd.lib
qtpcre2d.lib
qwindowsd.lib
Qt5FontDatabaseSupportd.lib
Qt5ThemeSupportd.lib
Qt5EventDispatcherSupportd.lib
Qt5AccessibilitySupportd.lib
qtfreetyped.lib
Kevin Higgins
I recommend you to use linuxdeployqt this tool. It can help solve most of the dependency problems. And I use it to package my qt application successfully.
This answer applies to using MSYS2 with mingw-w64 as the compiler, in Windows, and qmake. The QT version is 5.15.0.
Installing the package qt5-static gives you a build of QT that produces executables who don't rely on any QT DLLs. (Example commandline - pacman -Ss mingw64/mingw-w64-x86_64-qt5-static).
However a new problem is introduced here: it does not pass the -static flag to gcc. Meaning that although the executable does not depend on QT DLLs, it does depend on libgcc-s, libwinpthread.dll etc.
Normally, this problem would be fixed by using CONFIG += static which causes qmake to pass -static to gcc. However, and as noted in the other answers, for a qt-static build that config option is ignored!
To solve this I had to manually specify the gcc flags for static linking in the qmake file, i.e. :
QMAKE_CXXFLAGS += -static
QMAKE_LFLAGS_WINDOWS += -static
Which results in a (large) binary with no external dependencies other than Windows system DLLs.
(In case it matters: my use case was building a COM in-process server DLL that should have no external dependencies; I was also using TEMPLATE=lib and CONFIG += dll).
Does it make sense to add relevant Qt Libs and headers (opensource version) to a projects repository or should I just reference them from the sdk directory ?
I (alone) am developing a cross-platform (windows/linux) c++ application which, will be using a number of thirdparty opensource libraries including Qt, OpenCV, log4cpp to name a few.
I started off development in Qt Creator for convenience (speedy GUI creation). Currently I have the Qt Sdk installed on both Linux and Windows outside the project directory.
I'm planning to move to CMAKE to for better configurability, e.g out-of-source builds and independance from Qt Creator and I would like to have all dependencies included in the project directory structure -so that I or anyone else can checkout the project and build without needing to install any other dependancies.
I have added all libs but Qt's to the directory as I am a little unsure weather its worth it -it seems like CMAKE depends on the Qt Sdk for compiling Qt specific files i.e moc, ui etc... (see here) is this the case ? or is there a better suited build tool ? (Originally I was I going to use Scons but I decided not to as the there seemed to be limited support.. and there hasn't been developement on Qt4 Tools since 2010, which doesnt inspire confidence either.)
Qt is too big to be put in a project directory, even if you include only one of its modules. Also you have some convenient functions in CMake to use Qt (add moc, rcc, ui). You can even write these functions by yourself (in fast, they just add a pre-build command).
For the build operations: you will need to use the moc (and maybe ui, rcc, ...), which is not provided by CMake. So, a person wanting to build your application must have the Qt SDK installed. Luckily, this installation is very easy.