一.编译静态QT
1. 修改配置文件
(1) Qt creator
Qt 4.3 and newer
Edit <QTDIR>/mkspecs/win32-g++/qmake.conf:
Change
QMAKE_LFLAGS = -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc
to (add -static)
QMAKE_LFLAGS = -static -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc
(2). Visual Studio
See Trolltech's knowledgebase: Why does a statically built Qt use the dynamic Visual Studio runtime libraries? Do I need to deploy those with my application?
Applications built with MSVC depend on msvc*.dll. Following changes must be done to mkspecs in order to eliminate this dependency.
Edit <QTDIR>/mkspecs/win32-X/qmake.conf, where 'X' stands for the version of Visual Studio:
msvc | Visual Studio 6 |
msvc.net | Visual Studio.NET 2002-2003 |
msvc2005 | Visual Studio.NET 2005 |
msvc2008 | Visual Studio.NET 2008 |
1. Change
QMAKE_CFLAGS_RELEASE = -O2 -MD
to
QMAKE_CFLAGS_RELEASE = -O2 -MT
If the string you find is "-O2 -MD -GL", be sure to remove "-GL" parameter (tested on msvc-2008) [ ] For Versions since 4.3.2
(tested with Qt Open Source 4.3.2, Visual Studio 2005 and Visual Studio 2008)
Additionally change
CONFIG += qt warn_on release incremental flat link_prl precompile_header autogen_precompile_source copy_dir_files debug_and_release debug_and_release_target embed_manifest_dll embed_manifest_exe
to
CONFIG += qt warn_on release incremental flat link_prl precompile_header autogen_precompile_source copy_dir_files debug_and_release debug_and_release_target
2.重新编译
在开始菜单里找到Qt Command Prompt并运行,输入命令:
configure -static -debug-and-release -no-exceptions
上一条命令运行成功后再输入命令: mingw32-make sub-src
等1 ~3 小时可能就运行完了 完成后就ok了。。。
当然你能编译成功的前提是你的一些路径配置都弄好了,还有许多人都只编译了release即 发行版本的静态库,没有编译debug(调试版本)的静态库,于是你用Qt Creator的时候,如果debug选择调试模式的话,那么此时就会出现: collect2: ld returned 1 exit status 以及can't find lQtGuid4 这样的错误提示,解决办法就是输入命令:configure -static debug,然后在mingw32-make sub-src,可能要编译两到三个小时,如果你不加sub-src编译的时间还要长,10个多小时吧,直接make的结果是包括qt程序在内全部编译, 当然花的时间要长很多。
二.编译静态程序工程设置:
All you need to do is to add one variable to the .pro file:
CONFIG += static
or
CONFIG += staticlib
depending on the type of the project.