How to build a static Qt version for Windows with gcc: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(→‎Preparation: Some grammar fixes)
(14 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''English''' [[Build-Static-Qt-For-Windows-With-Gcc-German|German]]
{{LangSwitch}}


=How to build a static Qt version for Windows with <span class="caps">GCC</span>=
[[Category:Developing with Qt::General]]
[[Category:HowTo]]
[[Category:Tools::QtCreator]]


This guide contains the way, I took and which worked for me. I hope it can help you a little bit.
This guide contains the way, I took and which worked for me. I hope it can help you a little bit.


==Preparation==
== Preparation ==


First download the QtSDK you need, I took 4.7.0 and install it (incl. gcc). <br /> Then copy the complete qt file tree from Path-To-Qt-<span class="caps">SDK</span>\qt to Path-To-Qt-<span class="caps">SDK</span>\qt-static (or whatever path you prefer).<br /> copy Path-To-Qt-<span class="caps">SDK</span>\bin\qtenv.bat to static folder and adapt content —&gt; replace xxx\qt with <span class="caps">XXX</span>\qt_static
First, download the QtSDK you need, I took 4.7.0 and installed it (including gcc).
Then copy the complete qt file tree from Path-To-Qt-SDK\qt to Path-To-Qt-SDK\qt-static (or whatever path you prefer).
copy Path-To-Qt-SDK\bin\qtenv.bat to the static folder and adapt content
> replace xxx\qt with XXX\qt_static


==Cleaning up the folders==
== Cleaning up the folders ==


As you copied the sources from an existing build, you have to clean it up, before doing a new build. To get this, do the following:
As you copied the sources from an existing build, you have to clean it up, before doing a new build. To get this, do the following:
* delete all tmp folders inside Path-To-Qt-SDK\qt_static
* go to Path-To-Qt-SDK\qt_static\liband delete everything except the readme and the fonts folder
* go to Path-To-Qt-SDK\qt_static\bin and delete all executables and dlls
* search for all makefiles inside Path-To-Qt-SDK\qt_static and delete them (Makefile, Makefile.debug, Makefile.release, not the others!)


* delete all tmp folders inside Path-To-Qt-<span class="caps">SDK</span>\qt_static
== Editing the config files for static build ==
* go to Path-To-Qt-<span class="caps">SDK</span>\qt_static\lib\ and delete everything except the readme and the fonts folder
* go to Path-To-Qt-<span class="caps">SDK</span>\qt_static\bin and delete all executables and dlls
* search for all makefiles inside Path-To-Qt-<span class="caps">SDK</span>\qt_static and delete them (Makefile, Makefile.debug, Makefile.release, not the others!)


==Editing the config files for static build==
Now you have to edit some configuration files, to enable static builds and also to like statically against the mingw c library:
* edit the file Path-To-Qt-SDK\qt_static\mkspecs\win32-g++\qmake.conf and add the bold (with * ) marked stuff
** QMAKE_CFLAGS_RELEASE = '''-Os -momit-leaf-frame-pointer'''
** QMAKE_LFLAGS = '''-static -static-libgcc''' …
** DEFINES''= '''QT_STATIC_BUILD'''
* edit Path-To-Qt-SDK\qt_static\qmake\Makefile.win32-g++
** LFLAGS = '''-static -static-libgcc''' …
* edit Path-To-Qt-SDK\qt_static\src\3rdparty\webkit\WebKit.pri
** add '''CONFIG ''= staticlib* on the top


Now you have to edit some configuration files, to enable static builds and also to like statically against the mingw c library:
== build ==
1. Open a command shell and go to the following path: Path-To-Qt-SDK\qt_static
 
2. call configure with the needed options. The ones I took are:
* configure.exe -static -debug-and-release -opensource -confirm-license -platform win32-g''+ -no-exceptions -dont-process -no-qt3support -no-webkit -qt-sql-sqlite -qt-zlib -qt-libpng -qt-libjpeg
* the important ones are: * -static -platform win32-g++ -no-exceptions*
* Now you have to build the makefiles (but only for the libraries, not for the tools):
* bin\qmake.exe projects.pro QT_BUILD_PARTS="libs" JAVASCRIPTCORE_JIT="yes"
* Now you can build Qt:
* mingw32-make.exe
* go and have some coffee. On my machine, it took about 3 hours (Laptop with Intel Core2 Duo T7700 2,4 GHz, 2 GB RAM, Windows 7 Professional, 32 Bit)
 
== Other stuff ==
I do not recomend to build the tools (designer, assitant, etc.) as you have them in QtCreator so you don't need them anymore. If you really want to execute them (like lrelease, lupdate), take the ones from the dynamic libraries folder (Path-To-Qt-SDK\qt\bin, incl. the needed dlls) :-)
 
== Optimization ==


* edit the file Path-To-Qt-<span class="caps">SDK</span>\qt_static\mkspecs\win32-g++\qmake.conf and add the bold (with * ) marked stuff
As the binaries might get really big, and you perhaps want to distribute them via the net, you should make them smaller. To achieve that, you can use tools like [http://upx.sourceforge.net upx] (http://upx.sourceforge.net)
** <span class="caps">QMAKE</span>_CFLAGS_RELEASE = '''-Os -momit-leaf-frame-pointer'''
** <span class="caps">QMAKE</span>_LFLAGS = '''-static -static-libgcc''' …
** <span class="caps">DEFINES</span> += '''QT_STATIC_BUILD'''
* edit Path-To-Qt-<span class="caps">SDK</span>\qt_static\qmake\Makefile.win32-g++
** <span class="caps">LFLAGS</span> = '''-static -static-libgcc''' …
* edit Path-To-Qt-<span class="caps">SDK</span>\qt_static\src\3rdparty\webkit\WebKit.pri
** add '''<span class="caps">CONFIG</span> += staticlib''' on the top


==build==
= How to integrate static version of Qt additionally to QtCreator =


# Open a command shell and go to the following path: Path-To-Qt-<span class="caps">SDK</span>\qt_static
Open QtCreator and go to the Tools / Options menu. Select Qt4:
# call configure with the needed options. The one I took are:
#* configure.exe -static -debug-and-release -opensource -confirm-license -platform win32-g++ -no-exceptions -dont-process -no-qt3support -no-webkit -qt-sql-sqlite -qt-zlib -qt-libpng -qt-libjpeg
#* the important ones are: * -static -platform win32-g++ -no-exceptions*
#* Now you have to build the makefiles (but only for the libraries, not for the tools):
#* bin\qmake.exe projects.pro QT_BUILD_PARTS=“libs” <span class="caps">JAVASCRIPTCORE</span>_JIT=“yes”
#* Now you can build Qt:
#* mingw32-make.exe
#* go and have some coffee. On my machine, it took about 3 hours (Laptop with Intel Core2 Duo T7700 2,4 GHz, 2 GB <span class="caps">RAM</span>, Windows 7 Professional, 32 Bit)


==Other stuff==
[[Image:Options.jpg|Select Options - Qt4 - Add]]


I do not recomend to build the tools (designer, assitant, etc.) as you have them in QtCreator so you don’t need them anymore. If you really want to execute them (like lrelease, lupdate), take the ones from the dynamic libraries folder (Path-To-Qt-<span class="caps">SDK</span>\qt\bin, incl. the needed dlls) <span class="smiley">:-)</span>
Add a new version by pressing the plus button
enter a name and the path to qmake (Path-To-Qt-SDK\qt_static\bin\qmake.exe)
add the MinGW directory (copy from 4.7.0)


==Optimization==
[[Image:Options2.jpg|Changes in the settings]]


As the binaries might get really big, and you perhaps want to distribute them via the net, you should make them smaller. To achieve that, you can use tools like [http://upx.sourceforge.net upx] ''[upx.sourceforge.net]'' (http://upx.sourceforge.net)
If you create a new project, you are asked, which Qt version you want to use. Leave both checkboxes checked.
[[Image:NewProject.jpg|Create new project]]


=How to integrate static version of Qt additionally to QtCreator=
If you already have a project and want to add this version, go to Projects (left hand of QtCreator) and add a setting.


Open QtCreator and go to the Tools / Options menu. Select Qt4: [[Image:Options.jpg|Select Options - Qt4 - Add]] Add a new version by pressing the plus button enter a name and the path to qmake (Path-To-Qt-<span class="caps">SDK</span>\qt_static\bin\qmake.exe) add the MinGW directory (copy from 4.7.0) [[Image:Options2.jpg|Changes in the settings]]
[[Image:Projects details.jpg|Change project settings]]


If you create a new project, you are asked, which Qt version you want to use. Leave both checkboxes checked. <br />[[Image:NewProject.jpg|Create new project]]<br /> If you already have a project and want to add this version, go to Projects (left hand of QtCreator) and add a setting.<br />[[Image:Projects_details.jpg|Change project settings]]<br /> For the static project configuration, you should add <span class="caps">CONFIG</span>+=static in the command line.<br /> Open the project settings, and press details for the qmake step. In the additional arguments, add '''<span class="caps">CONFIG</span>+=static'''<br />[[Image:Projects_details2.jpg|changes qmake step]]
For the static project configuration, you should add CONFIG+=static in the command line.
Open the project settings, and press details for the qmake step. In the additional arguments, add '''CONFIG+=static'''


In the project (*.pro) file, add the following:<br />
[[Image:Projects details2.jpg|changes qmake step]]


What also makes sense is to add a d to the binary, if you create a debug build:<br />
In the project (*.pro) file, add the following:
<code>
static { # everything below takes effect with CONFIG ''= static
CONFIG+= static
CONFIG += staticlib # this is needed if you create a static library, not a static executable
DEFINES+= STATIC
message("~~~ static build ~~~") # this is for information, that the static build is done
mac: TARGET = $$join(TARGET,,,_static) #this adds an _static in the end, so you can seperate static build
from non static build
win32: TARGET = $$join(TARGET,,,s) #this adds an s in the end, so you can seperate static build from
non static build
}
</code>


Now you have it. Build the static version (select he static project settings and build it).<br />[[Image:BuildSettings.jpg|Build settings]]
What also makes sense is to add a d to the binary, if you create a debug build:
<code>
# change the nama of the binary, if it is build in debug mode
CONFIG (debug, debug|release) {
mac: TARGET = $$join(TARGET,,,_debug)
win32: TARGET = $$join(TARGET,,,d)
}
</code>


===Categories:===
Now you have it. Build the static version (select he static project settings and build it).


* [[:Category:Developing-with-Qt|Developing with Qt]]
[[Image:BuildSettings.jpg|changes qmake step]]
** [[:Category:Developing-with-Qt::General|General]]
* [[:Category:HowTo|HowTo]]
* [[:Category:Tools|Tools]]
** [[:Category:Tools::QtCreator|QtCreator]]

Revision as of 19:24, 4 March 2017

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

This guide contains the way, I took and which worked for me. I hope it can help you a little bit.

Preparation

First, download the QtSDK you need, I took 4.7.0 and installed it (including gcc). Then copy the complete qt file tree from Path-To-Qt-SDK\qt to Path-To-Qt-SDK\qt-static (or whatever path you prefer). copy Path-To-Qt-SDK\bin\qtenv.bat to the static folder and adapt content —> replace xxx\qt with XXX\qt_static

Cleaning up the folders

As you copied the sources from an existing build, you have to clean it up, before doing a new build. To get this, do the following:

  • delete all tmp folders inside Path-To-Qt-SDK\qt_static
  • go to Path-To-Qt-SDK\qt_static\liband delete everything except the readme and the fonts folder
  • go to Path-To-Qt-SDK\qt_static\bin and delete all executables and dlls
  • search for all makefiles inside Path-To-Qt-SDK\qt_static and delete them (Makefile, Makefile.debug, Makefile.release, not the others!)

Editing the config files for static build

Now you have to edit some configuration files, to enable static builds and also to like statically against the mingw c library:

  • edit the file Path-To-Qt-SDK\qt_static\mkspecs\win32-g++\qmake.conf and add the bold (with * ) marked stuff
    • QMAKE_CFLAGS_RELEASE = -Os -momit-leaf-frame-pointer
    • QMAKE_LFLAGS = -static -static-libgcc
    • DEFINES= QT_STATIC_BUILD
  • edit Path-To-Qt-SDK\qt_static\qmake\Makefile.win32-g++
    • LFLAGS = -static -static-libgcc
  • edit Path-To-Qt-SDK\qt_static\src\3rdparty\webkit\WebKit.pri
    • add 'CONFIG = staticlib* on the top

build

1. Open a command shell and go to the following path: Path-To-Qt-SDK\qt_static

2. call configure with the needed options. The ones I took are:

  • configure.exe -static -debug-and-release -opensource -confirm-license -platform win32-g+ -no-exceptions -dont-process -no-qt3support -no-webkit -qt-sql-sqlite -qt-zlib -qt-libpng -qt-libjpeg
  • the important ones are: * -static -platform win32-g++ -no-exceptions*
  • Now you have to build the makefiles (but only for the libraries, not for the tools):
  • bin\qmake.exe projects.pro QT_BUILD_PARTS="libs" JAVASCRIPTCORE_JIT="yes"
  • Now you can build Qt:
  • mingw32-make.exe
  • go and have some coffee. On my machine, it took about 3 hours (Laptop with Intel Core2 Duo T7700 2,4 GHz, 2 GB RAM, Windows 7 Professional, 32 Bit)

Other stuff

I do not recomend to build the tools (designer, assitant, etc.) as you have them in QtCreator so you don't need them anymore. If you really want to execute them (like lrelease, lupdate), take the ones from the dynamic libraries folder (Path-To-Qt-SDK\qt\bin, incl. the needed dlls) :-)

Optimization

As the binaries might get really big, and you perhaps want to distribute them via the net, you should make them smaller. To achieve that, you can use tools like upx (http://upx.sourceforge.net)

How to integrate static version of Qt additionally to QtCreator

Open QtCreator and go to the Tools / Options menu. Select Qt4:

Select Options - Qt4 - Add

Add a new version by pressing the plus button enter a name and the path to qmake (Path-To-Qt-SDK\qt_static\bin\qmake.exe) add the MinGW directory (copy from 4.7.0)

Changes in the settings

If you create a new project, you are asked, which Qt version you want to use. Leave both checkboxes checked. Create new project

If you already have a project and want to add this version, go to Projects (left hand of QtCreator) and add a setting.

Change project settings

For the static project configuration, you should add CONFIG+=static in the command line. Open the project settings, and press details for the qmake step. In the additional arguments, add CONFIG+=static

changes qmake step

In the project (*.pro) file, add the following:

static { # everything below takes effect with CONFIG ''= static
 CONFIG+= static
 CONFIG += staticlib # this is needed if you create a static library, not a static executable
 DEFINES+= STATIC
 message("~~~ static build ~~~") # this is for information, that the static build is done
 mac: TARGET = $$join(TARGET,,,_static) #this adds an _static in the end, so you can seperate static build
from non static build
 win32: TARGET = $$join(TARGET,,,s) #this adds an s in the end, so you can seperate static build from
non static build
}

What also makes sense is to add a d to the binary, if you create a debug build:

# change the nama of the binary, if it is build in debug mode
CONFIG (debug, debug|release) {
 mac: TARGET = $$join(TARGET,,,_debug)
 win32: TARGET = $$join(TARGET,,,d)
}

Now you have it. Build the static version (select he static project settings and build it).

changes qmake step