Qt Link App Statically with 3rd Party Libraries on Windows using MinGW

Qt Link App Statically with 3rd Party Libraries on Windows using MinGW


To build a Qt Application on Windows where you can compile your entire Qt App statically into one executable without dynamically linking against the Qt Libraries, do the following:

1.) Download the Qt Source from http://qt-project.org/downloads ( http://releases.qt-project.org/qt4/source/qt-everywhere-opensource-src-4.8.4.zip ). Unpack the zip archive into a new directory.

2.) Download and install MinGW (win32-g++) from http://www.mingw.org/.

3.) Open the MinGW Command Prompt and change directories to your unpacked version of the Qt Source.

4.) From the command prompt issue the command ( make sure you use configure.exe and NOT ./configure ):

>> configure.exe -static -release -qt-libjpeg -qt-zlib -qt-libpng -nomake examples -nomake demos -no-exceptions -no-qt3support -no-scripttools -no-openssl -no-opengl -no-webkit -no-phonon -no-sql-sqlite -platform win32-g++

5.) When prompted, choose the open source version and accept the terms of the license

6.) When the configure step completes, issue the following:

>> mingw32-make sub-src

7.) Edit the \qt-everywhere-opensource-src-4.8.4\mkspecs\win32-g++/qmake.conf file and change the QMAKE_LFLAGS line to:

QMAKE_LFLAGS = -static -static-libgcc -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc

8.) From within Qt Creator choose the newly built qmake for static build of Qt and add it as a known Qt Version.

9.) Also from within Qt Creator, choose the C:\MinGW\bin\mingw32-g++.exe compiler executable as a MINGW32 G++ compiler and add it to your 'Toolchain' options under the 'Options' menu.

10.) Add "CONFIG+=static" as 'Additional Arguments' to your Build Steps in the Qt Creator Build configuration panel.

10.) Now edit your .pro file for a project and add the following configuration specifics - make sure that the 3rd party libs you are linking against have the '.a' or '.lib' suffixes! ( not dll ):

# Build statically - no dynamic linking!

CONFIG += staticlib

CONFIG += static

LIBS += -static

static {

CONFIG += static

DEFINES += STATIC

message("Static build.")

LIBS += C:\test\app\lib\libthirdparty.a

QT += core

TARGET = MyTestApp

SOURCES += a.cpp \

b.cpp \

c.cpp

HEADERS += a.h\

b.h \

c.h

}

11.) Now click the green arrow button to build and run your application while statically linking against your new Qt!



ClassyBits 2016