Qt static linking and deployment

98,753

Solution 1

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.

Solution 2

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:

  1. Do you want a static Qt library?

-static option should be passed to configure

  1. 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

  1. 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.

Solution 3

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.

Solution 4

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.

Solution 5

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

Share:
98,753

Related videos on Youtube

Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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

    • Md. Minhazul Haque
      Md. Minhazul Haque about 11 years
      do you have static copies of QtCore4.a, QtGui4.a?Then adding CONFIG += static would solve this. If you don't have static copy of Qt, go grab the source and build. It takes several hours to get static libs.
  • Lisa
    Lisa over 11 years
    Be aware of licensing issues if you statically link.
  • EMPraptor
    EMPraptor about 11 years
    Wayback Machine links below. First link is the article about static linking of Qt4 to application. Second link is the article about building Qt to be smaller and able to do static linking. web.archive.org/web/20120729010312/http://www.formortals.com‌​/… web.archive.org/web/20120723142656/http://www.formortals.com‌​/…
  • Charles Burns
    Charles Burns about 11 years
    @JaredGlass You'll be happy to know that the link is back up.
  • FRD
    FRD over 10 years
    @CharlesBurns, hm, broken again?
  • GraehamF
    GraehamF over 10 years
    not working for me either with vs2012 and qt5.1.1. Is there an up-to-date version of a this step-by-step guide?
  • Kuba hasn't forgotten Monica
    Kuba hasn't forgotten Monica over 9 years
    -1 As this is a link-only answer with no other content.
  • Charles Burns
    Charles Burns over 9 years
    @KubaOber: Sorry I couldn't fit my detailed, illustrated, ad-free article into a StackOverflow post, but notice there is content besides just a link: Please read the portion of my answer beginning with, "In short, it turns out..."
  • Andreas Haferburg
    Andreas Haferburg almost 7 years
    @CharlesBurns The problem with link-only answers is that links tend to die. Which is what your links did.
  • tofutim
    tofutim about 6 years
    I want to use this, but I'm not sure how to set CMAKE_AUTOSTATICPLUGINS. I am using qmake then make. Can you advise?
  • Darklighter
    Darklighter about 6 years
    Did you mistype qmake there since it’s about CMake here. You can just set it globally: set(CMAKE_AUTOSTATICPLUGINS ON) or as a target specific property: set_target_properties(${PROJECT_NAME} PROPERTIES AUTOSTATICPLUGINS ON).
  • Admin
    Admin about 6 years
    great this helped me, I was missing one of your .lib that you mentioned here and get wired linking errors, many thanks
  • Admin
    Admin almost 6 years
    Hey Kevin! Thanks for sharing! May I ask you please what configuration did you use to build qt with static link? I went to the Src folder and did: "configure -platform win32-msvc -static -release -nomake examples -nomake tests -opensource" and I also noticed that in your answers the libs you linking against with are the debug ones, so I changed to the release one of course and still getting linking errors. I guess I'll have to re-build qt again and try to build for debug and release
  • Oshada
    Oshada over 5 years
    @CharlesBurns I know it is after a long time, but could you please tell me where did you find these instructions, because I couldn't find them in the qt documentation on static linking
  • Charles Burns
    Charles Burns over 5 years
    @Oshada doc.qt.io/QtForDeviceCreation/qtee-static-linking.html is the current official documentation.
  • 0xAA55
    0xAA55 over 2 years
    linuxdeployqt requires old linux version which installed old version of glibc to run. None of my linux environments were capable of using it.