Building static
From qtnode
It is possible to statically link the Qt librariess to your application. This will increase the size of the produced binary, but also eliminate .so/.dll dependencies. For example, if you build a simple single-button "hello world" program on Win32 using MinGW the generated binary will be over 5 megabytes.
Contents |
How to set up static linking for your application
Note that to use static linking you must first rebuild Qt to enable static linking, see below.
To build a static app, use CONFIG += static inside your project (.pro) file.
To build a static lib that uses qt, use CONFIG += staticlib inside your project (.pro) file.
How to enable static linking support for the Qt library
Depending on your current installation, you may have to rebuild the Qt library itself to be able to link Qt statically to your application. Before you compile Qt, when configuring, use the -static option to enable static builds:
prompt> configure -static [other configure options]
After configure has completed, do not type 'make', instead build the Qt libraries by using the following command:
prompt> make sub-src
The difference between make sub-src and just make is that the former will build only the Qt libraries and the latter will build the libraries plus examples, demos and so on. The latter takes a lot longer to complete.
It is advisable to create a separate directory for your static Qt setup. For instance if you have a C:\qt\4.1.1\ directory which holds your standard dynamic Qt libs, you might want to create a directory called C:\qt\4.1.1-static.
Using the precompiled version of Qt with MinGW (win32)
The precompiled .exe distribution of Qt4/Windows is configured for dynamic linking only. You can, however, reconfigure the installation to enable static linking.
- Start the Qt Command Prompt (typically available through Start → Programs → Qt by Trolltech v4.1.1 (OpenSource) → Qt 4.1.1 Command Prompt or similar.)
- Reconfigure the Qt source tree for static library creation:
C:\Qt\4.1.1> configure -static
- Rebuild the Qt sources with the new configuration:
C:\Qt\4.1.1> make sub-src
Using the Visual Studio compiler and creating apps with no external dependencies
If you using the Visual Studio compiler on Windows and want a single .exe file with no dependent .dll files (including the MSVC runtime DLLs), see Qt4 with Visual Studio and static C runtime.
Using a packer to reduce executable size
A "packer" program can be used to compress the size of your executable. The executable is then unpacked into memory when the user runs it.
A well known free and open-source packer is UPX.