# Static linking for windows

• Hi, I have been developing on mobile devices so far, and today I tried to port my application to windows, but this was no easy task..

I tried to follow this guide:
"http://doc.qt.nokia.com/4.7/deployment-windows.html#static-linking
":http://doc.qt.nokia.com/4.7/deployment-windows.html#static-linking
Its just terrible.. I does not explain anything, I had to find out things by google, and now I am stuck..

First thing:
cd C:\path\to\Qt
configure -static <any other options you need>

That was not easy, solution was:
configure -static -platform win32-g++
I hope this was right?

Anyway, the next step is:
nmake sub-src
This gives me:
C:\Qt\4.7.0>namke sub-src
'namke' is not recognized as an internal or external command,
operable program or batch file.

What now? Why is the guide not telling me all I need to know, namke is not to be found..
I installed this: qt-win-opensource-4.7.0-mingw.exe

Any help please? :)

• mingw32-make (not nmake) is the "make" command on win32-g++ (MinGW).

But, you might want to look at mingw-cross-env which includes Qt and builds all static libs.

• Thank you, but running "mingw32-make sub-src" still gets me nowhere..

• Nobody knows how to accomplish this with a few simple commands ? Do I really have to waste hours on research just for this? Qt is so good at everything else, why not when it comes to deploying the code ?

For anyone else following this some day: This link looks good: http://www.formortals.com/build-qt-static-small-microsoft-intel-gcc-compiler/
But it fails here:
bq.
Open a command-line window appropriate for your compiler.
GCC: Open the command prompt (Start –> Run –> “cmd”), then run “mingwvars.bat”. For example, if GCC is installed in “c:\MinGW”, then enter the command: “c:\MinGW\mingwvars.bat“
bq.
there is no mingwvars.bat file to be found.

• Have you tried mingw-cross-env yet?

• No, I have not figured out how to use it, and there is no guides for windoes.
I have tried this:
Downloaded : http://sourceforge.net/projects/mingw/files/
Installed it and selected MSYS and and the C++ compiler.
I then opened msys.bat and did this:
@
\$ configure -release -nomake examples -nomake demos -no-exceptions -no-qt3supp
ort -no-scripttools -no-openssl -no-webkit -no-phonon -no-sql-sqlite -qt-libjpe
g -qt-zlib -qt-libpng -static -platform win32-G++
@

but that retuned this:

Creating qmake. Please wait...
rm: cannot remove directory mkspecs/default': Is a directory ln: creating symbolic linkmkspecs/default/win32-G++' to win32-G++': No such f
ile or directory
g++ -c -o project.o -DQMAKE_OPENSOURCE_EDITION -I. -Igenerators -Igenerators/un
ix -Igenerators/win32 -Igenerators/mac -Igenerators/symbian -I/c/qt/4.7.0/includ
e -I/c/qt/4.7.0/include/QtCore -I/c/qt/4.7.0/src/corelib/global -I/c/qt/4.7.0/sr
c/corelib/xml -I/c/qt/4.7.0/tools/shared -DQT_NO_PCRE -DQT_BUILD_QMAKE -DQT_BOOT
STRAPPED -DQT_NO_TEXTCODEC -DQT_NO_UNICODETABLES -DQT_NO_COMPONENT -DQT_NO_STL -
DQT_NO_COMPRESS -I/c/qt/4.7.0/mkspecs/win32-G++ -DHAVE_QCONFIG_CPP -DQT_NO_THREA
D -DQT_NO_QOBJECT -DQT_NO_GEOM_VARIANT project.cpp
project.cpp: In member function 'QStringList& QMakeProject::values(const QString
&, QMap<QString, QStringList>&)':
project.cpp:3024:51: error: cannot convert 'wchar_t*' to 'CHAR*' for argument '1
' to 'BOOL GetComputerNameA(CHAR*, DWORD*)'
make.exe: *** [project.o] Error 1

• No, I asked if you tried this:

http://mingw-cross-env.nongnu.org/

This is a cross building environment for unix targeting MinGW/Windows. Check out the website.

• use make instead of nmake.
that should do it

• Thank you for all your input, but this continues to be Qt's biggest flaw to date, this limits our development process so much that we are considering moving to another language.

We feel really tricked here, Qt is such a nice language that we came to love it very easily, but when we try to actually deploy our code, there is no documentation on this very crucial step for us. (Well, there is documentation, but it is deeply flawed and does not at all reflect the tools that are delivered to developers).

• It looks like you've had two problems with the process.

The first problem has already been covered, by other people in here and in the documentation. From the docs:

bq. Also, we have used nmake in all the examples, but if you use MinGW you must use mingw32-make instead.

I'm not sure it counts as a deep flaw if the documentation is unable to prevent people from skimming :)

The second problem is a typo, indicated by the error message:

bq. creating symbolic link mkspecs/default/win32-G++’ towin32-G++’: No such file or directory

If you try again with configure -platform win32-g++ <all of your other configure options> you'll probably have more luck. I believe all of the options to configure are case sensitive.

Hopefully that helps.

• Thank you for your feedback david, but I got a bit confued, and I think the forum is the fault:

bq. creating symbolic link mkspecs/default/win32-G++’ to`win32-G++’: No such f
ile or directory

The text i entered into the quote was not: "...win32-G+..." it was: "...win32-G++..."

So, I cannot really be sure what you mean david, the forum does not correctly reflect what you and I wrote.

Moderators, please fix this?

• Hi,

if all you need is a static build of Qt, done with mingw, do the following:

extract Qt-...-src.zip (that is without any temporary file that mighjt disturb) :)

Change path-to-qt\mkspecs\win32-g++\qmake.conf:
@
QMAKE_CFLAGS_RELEASE = -Os -momit-leaf-frame-pointer
QMAKE_LFLAGS = -static -static-libgcc -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc

cd %qtdir%
configure -static -release -no-exceptions -[other parameters like -mmx -sse -sse2 -3dnow etc...]
mingw32-make sub-src [here you can make some coffee etc.. it will take some time]
@

If you use webkit, you have to change the webkit pri file also:
\src\3rdparty\webkit\ WebKit.pri:
CONFIG += staticlib

For builds after making the changes in the config files, I used a batch file:
@set QTDIR=D:\Developement\Qt\2010.04\qt_static
set PATH=D:\Developement\Qt\2010.04\qt_static\bin
set PATH=%PATH%;D:\Developement\Qt\2010.04\bin;D:\Developement\Qt\2010.04\mingw\bin
set PATH=%PATH%;%SystemRoot%\System32
set QMAKESPEC=win32-g++

mingw32-make confclean
configure.exe -release -confirm-license -qt-libpng -opensource -no-incredibuild-xge -process -static
mingw32-make
@

• Could you please change the topic back to its original text. The forum does not have a bug.

• Looks like the forum wants to make an underline (uses a + for markup) instead of letting you write "g++" in a block quote.

Not a bug - just a very eager feature ;) We shall have a look.

• If you are on a unixoid OS it might be a typo. Instead of

@-platform win32-G++@

use

@-platform win32-g++@

with a lower case g not a capital G.

• Hi, I created a wiki page on how to create static libaries for windows with gcc:
"BuildStaticQtForWindowsWithGcc":http://developer.qt.nokia.com/wiki/Category:QtDevelopment::General::BuildStaticQtForWindowsWithGcc

I hope it might help.

• If anyone is trying to compile libraries for Qt 4.7.1 with mingw read this:

Qt libraries 4.7.1 for Windows do not compile with mingw, there are errors in src/declarative/qml/ in 5 files, they use "intptr_t" which is defined in "stdint.h" as "int", which generates an error in casts. I've looked on gitorious qt 4.7.2 and it has been fixed, "intptr_t" has been replaced with "uintptr" which is defined in qglobal.h as "QIntegerForSizeof[void*]::Unsigned" and works fine.

The fix: search for "intptr_t" in src/declarative/qml and replace it with "uintptr" (should find 5 files).

After the the search & replace libs compile with no errors.

• Gerolf Reinwardt: I've been compiling qt libs yesterday, I didn't know about that wiki page, it's been a lot of pain.

My compile solution differs a little from the one in wiki, but it works, so it should be ok, right? Here are the differences:

• I deleted the whole lib/ directory along with fonts and readme.
• I didn't touch bin/ directory, all files are still there
• win32-g++/qmake.conf, I didn't add "DEFINES += QT_STATIC_BUILD"
• also not this: bin\qmake.exe projects.pro QT_BUILD_PARTS=”libs” JAVASCRIPTCORE_JIT=”yes”

But i've added more arguments in configure like -no-make examples -no-make tools etc. So only lib/ and qmake/ have been compiled.

And in qt creator I've added this static version and provided qmake from the qmake/ directory, not the bin/ directory.

There are more articles about building qt static:

http://www.qtcentre.org/wiki/index.php?title=Building_static_Qt_on_Windows

http://www.qtcentre.org/wiki/index.php?title=Building_static_Qt_on_Windows_with_MinGW

http://www.formortals.com/build-qt-static-small-microsoft-intel-gcc-compiler/

Formortals.com provides cflags for size-optimized libraries, but they don't work: -Os -mpreferred-stack-boundary=2 -finline-small-functions -momit-leaf-frame-pointer (I got compile errors)

Btw. I've been compiling with mingw 4.5.0 (g++ version), the mingw included in qt sdk is 4.4.0. I compared them by compiling project with shared libraries and the executable created with 4.5.0 was 85 KB, compare it to 130 KB with 4.4.0. But there is an additional .dll required when compiling with 4.5.0 (libstdc++.dll which is 850 KB), 4.4.0 only requires mingwm10.dll and libgcc_s_dw2-1.dll. When compiling statically I only used 4.5.0 so can't compare them, which ones generates smaller exe.

But here here is some info for my program, using 4.5.0 compiled staticaly: using CORE, GUI and DECLARATIVE libraries, the compiled exe is 8.7 MB, after upx'ing it compresses to: 3.2 MB.

There is no difference when running the 8.7 MB exe or the upx'ed 3.2 MB exe, they both execute n less than a second. Compare it with exe with the dlls: it executes 3-4 seconds! (tested on old one computer - 1.5 GHz 1 core). Yes it takes so long to load these dlls, together the dlls size is 23.3 MB. I use only core, gui and declarative but they require others too when running the program: QtNetwork4.dll, QtScript4.dll, QtSql4.dll, QtXmlPatterns4.dll - so together it is 23.3 MB. It is not so bad when using an NSIS installer (nsis.sourceforge.net), it can pack it using lzma to 7.2 MB. But using static libraries and upx gives us 3.2 MB which is 2.5x less.

Btw. when you disable not needed libaries, as I was compiling my app disabling qtwebkit and others (see configure.exe arguments) the lib/ and qmake/ can compile in less than 30 minutes (using mingw 4.5.0).

• You can use VC Compiler instead of MinGW.
That's should been fastest.

• From my experiences, MSVC and gcc compiler are not so different in time.
Regarding the mingwm10.dll and libgcc_s_dw2-1.dll libraries, thta's why I added LFLAGS = -static -static-libgcc which adds these as static libraries.
I know, there are several pages describing statically build, but I also tried long time to get it compiled, until I had all Info I needed :-). That's why I created the page describing my steps one after the other (I left the round trips out).
It's clear, that the dll size is much bigger, as it contains everything, and you exe only what you need. So static executables are always faster and smaller. But they have more effort in fixing and maintaining, as you can't fix just one part, you always have to distribute all, incl the Qt code (as it's in the exe) and don't get fixes, just by changing the Qt dlls.

• Thank You Cezary Tomczak!

this:
[quote author="Cezary Tomczak" date="1291037300"]

• I deleted the whole lib/ directory along with fonts and readme.
• I didn't touch bin/ directory, all files are still there
• win32-g++/qmake.conf, I didn't add "DEFINES += QT_STATIC_BUILD"
• also not this: bin\qmake.exe projects.pro QT_BUILD_PARTS=”libs” JAVASCRIPTCORE_JIT=”yes”
[/quote]

helps me a lot, after days tearing out my hair.

• Could You Explain Even more on this
[quote author="Gerolf" date="1290842572"]Hi,

if all you need is a static build of Qt, done with mingw, do the following:

extract Qt-...-src.zip (that is without any temporary file that mighjt disturb) :)

Change path-to-qt\mkspecs\win32-g++\qmake.conf:
@
QMAKE_CFLAGS_RELEASE = -Os -momit-leaf-frame-pointer
QMAKE_LFLAGS = -static -static-libgcc -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc

cd %qtdir%
configure -static -release -no-exceptions -[other parameters like -mmx -sse -sse2 -3dnow etc...]
mingw32-make sub-src [here you can make some coffee etc.. it will take some time]
@

If you use webkit, you have to change the webkit pri file also:
\src\3rdparty\webkit\ WebKit.pri:
CONFIG += staticlib

For builds after making the changes in the config files, I used a batch file:
@set QTDIR=D:\Developement\Qt\2010.04\qt_static
set PATH=D:\Developement\Qt\2010.04\qt_static\bin
set PATH=%PATH%;D:\Developement\Qt\2010.04\bin;D:\Developement\Qt\2010.04\mingw\bin
set PATH=%PATH%;%SystemRoot%\System32
set QMAKESPEC=win32-g++

mingw32-make confclean
configure.exe -release -confirm-license -qt-libpng -opensource -no-incredibuild-xge -process -static
mingw32-make
@[/quote]

• you can have a look at the wiki: "How to build a static Qt version for Windows with GCC":http://developer.qt.nokia.com/wiki/How_to_build_a_static_Qt_version_for_Windows_with_gcc

• The solution for me is the following currently:

1. Download Qt SDK 4.7.0 (4.7.1 - 4.7.3 not working):

ftp://ftp.qt.nokia.com/qtsdk/qt-sdk-win-opensource-2010.05.exe

1. Statically build Qt according to instructions:

http://doc.trolltech.com/4.7.1/deployment-windows.html#static-linking

1. Statically build your apps according to the above instructions.

I hope these will help you.

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.