Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build Qt 4.8/5.2 statically under VS2012, using the static MSVC runtime, with Windows XP support?

I'd like to make a completely static build of Qt 4.8/5.2 under VS2012 - including static runtime libraries, and targeting Windows XP systems. This is not supported out-of-the-box by Qt, since even the static build of Qt uses the dynamically linked MSVC runtime.

This is to be a canonical question that provides the knowledge necessary to successfully build Qt meeting those requirements.

like image 532
Kuba hasn't forgotten Monica Avatar asked Dec 25 '22 13:12

Kuba hasn't forgotten Monica


1 Answers

Qt 5.2

Assuming that the environment was prepared for XP targeting, and relevant XP-targeting qt5xp.patch and the bug fix qt5fixes.patch available - both from my other answer, we have to do the following:

  1. Create a separate win32-msvc2012-static and win32-msvc2012-static-xp qmake specs by copying them from qtbase/mkspecs/win32-msvc2012 and qtbase/mkspecs/win32-msvc2012-xp, respectively.

  2. Modify the qmake specs.

  3. Teach configure and qmake makefile about the new qmake specs.

  4. Force bootstrap of configure.exe by creating an empty .gitignore file in qtbase.

  5. If you're configuring Qt with a -prefix so that the installation and build directories are separate, you must apply the patch for QTBUG-32519 - at least until that bug gets fixed.

The following batch file does the complete job. At the moment, webkit building is disabled for static Qt builds.

:: Assume that we're in an equivalent of C:\Qt prefix
@set PREFIX=%~dp0
:: Qt sources
@set QT=%PREFIX%..\5.2.1-src
:: Patch file(s)
@set SRC=%PREFIX%
@set SPEC=win32-msvc2012
@if not exist "%QT%\qt.pro" ( echo Qt source folder expected in %QT%>&2 & exit /b 1 )
::
@patch --forward --directory=%QT% -p0 --global-reject-file=%SRC%\qt5fixes.rej --input=%SRC%\qt5fixes.patch
::
@echo > %QT%\qtbase\.gitignore
@mkdir %QT%\qtbase\mkspecs\%SPEC%-xp
@copy %QT%\qtbase\mkspecs\%SPEC%\qplatformdefs.h %QT%\qtbase\mkspecs\%SPEC%-xp
@copy %QT%\qtbase\mkspecs\%SPEC%\qmake.conf %QT%\qtbase\mkspecs\%SPEC%-xp
@patch --forward --directory=%QT% -p0 --global-reject-file=%SRC%\qt5xp.rej --input=%SRC%\qt5xp.patch
::
@mkdir %QT%\qtbase\mkspecs\%SPEC%-static
@copy %QT%\qtbase\mkspecs\%SPEC%\qplatformdefs.h %QT%\qtbase\mkspecs\%SPEC%-static
@copy %QT%\qtbase\mkspecs\%SPEC%\qmake.conf %QT%\qtbase\mkspecs\%SPEC%-static
@mkdir %QT%\qtbase\mkspecs\%SPEC%-static-xp
@copy %QT%\qtbase\mkspecs\%SPEC%-xp\qplatformdefs.h %QT%\qtbase\mkspecs\%SPEC%-static-xp
@copy %QT%\qtbase\mkspecs\%SPEC%-xp\qmake.conf %QT%\qtbase\mkspecs\%SPEC%-static-xp
@patch --forward --directory=%QT% -p0 --global-reject-file=%SRC%\qt5static.rej --input=%SRC%\qt5static.patch

To undo the changes to the Qt source, run the following, with variables set as above:

@patch --reverse --directory=%QT% -p0 --global-reject-file=%SRC%\qt5static-unfix.rej --input=%SRC%\qt5static.patch
@del %QT%\qtbase\mkspecs\%SPEC%-static\qplatformdefs.h
@del %QT%\qtbase\mkspecs\%SPEC%-static\qmake.conf
@rmdir %QT%\qtbase\mkspecs\%SPEC%-static
@del %QT%\qtbase\mkspecs\%SPEC%-static-xp\qplatformdefs.h
@del %QT%\qtbase\mkspecs\%SPEC%-static-xp\qmake.conf
@rmdir %QT%\qtbase\mkspecs\%SPEC%-static-xp
::
@patch --reverse --directory=%QT% -p0 --global-reject-file=%SRC%\qt5xp-unfix.rej --input=%SRC%\qt5xp.patch
@del %QT%\qtbase\mkspecs\%SPEC%-xp\qplatformdefs.h
@del %QT%\qtbase\mkspecs\%SPEC%-xp\qmake.conf
@rmdir %QT%\qtbase\mkspecs\%SPEC%-xp
@del %QT%\qtbase\.gitignore
::
@patch --reverse --directory=%QT% -p0 --global-reject-file=%SRC%\qt5fixes-unfix.rej --input=%SRC%\qt5fixes.patch

The build is then performed by executing

configure -static -platform win32-msvc2012-static-xp (or win32-msvc2012-static)
jom (or nmake)
jom install (if doing the build separate from the installation directory)
# qt5static.patch
# Static MSVC Runtime Support for Qt 5.2
#
# Build qmake with XP targeting.
--- qtbase/qmake/Makefile.win32 2014-02-20 12:28:23.316380600 -0500
+++ qtbase/qmake/Makefile.win32 2014-02-20 12:29:07.396008900 -0500
@@ -42,7 +42,7 @@
               -DQT_NO_TEXTCODEC -DQT_NO_UNICODETABLES -DQT_NO_COMPONENT -DQT_NO_COMPRESS \
               -DQT_NO_THREAD -DQT_NO_QOBJECT -DQT_NO_GEOM_VARIANT -DQT_NO_DATASTREAM \
               -DUNICODE -DQT_CRYPTOGRAPHICHASH_ONLY_SHA1 -DQT_JSON_READONLY
-!if "$(QMAKESPEC)" == "win32-msvc2012-xp"
+!if "$(QMAKESPEC)" == "win32-msvc2012-xp" || "$(QMAKESPEC)" == "win32-msvc2012-static-xp"
 CFLAGS_BARE = $(CFLAGS_BARE) -D_USING_V110_SDK71_
 !endif
 CFLAGS   = -Yuqmake_pch.h -FIqmake_pch.h -Fpqmake_pch.pch $(CFLAGS_BARE) $(CFLAGS) $(EXTRA_CPPFLAGS)
# Add support for static qmake specs.
--- qtbase/qmake/Makefile.win32 2014-02-01 22:37:30.000000000 -0500
+++ qtbase/qmake/Makefile.win32 2014-02-17 16:21:09.329949100 -0500
@@ -1,4 +1,4 @@
-!IF "$(QMAKESPEC)" == "win32-msvc" || "$(QMAKESPEC)" == "win32-msvc.net" || "$(QMAKESPEC)" == "win32-msvc2002" || "$(QMAKESPEC)" == "win32-msvc2003" || "$(QMAKESPEC)" == "win32-msvc2005" || "$(QMAKESPEC)" == "win32-msvc2008" || "$(QMAKESPEC)" == "win32-msvc2010" || "$(QMAKESPEC)" == "win32-msvc2012" || "$(QMAKESPEC)" == "win32-msvc2012-xp" || "$(QMAKESPEC)" == "win32-msvc2013" || "$(QMAKESPEC)" == "win32-icc"
+!IF "$(QMAKESPEC)" == "win32-msvc" || "$(QMAKESPEC)" == "win32-msvc.net" || "$(QMAKESPEC)" == "win32-msvc2002" || "$(QMAKESPEC)" == "win32-msvc2003" || "$(QMAKESPEC)" == "win32-msvc2005" || "$(QMAKESPEC)" == "win32-msvc2008" || "$(QMAKESPEC)" == "win32-msvc2010" || "$(QMAKESPEC)" == "win32-msvc2012" || "$(QMAKESPEC)" == "win32-msvc2012-static" || "$(QMAKESPEC)" == "win32-msvc2012-static-xp" || "$(QMAKESPEC)" == "win32-msvc2012-xp" || "$(QMAKESPEC)" == "win32-msvc2013" || "$(QMAKESPEC)" == "win32-icc"
 
 !if "$(SOURCE_PATH)" == ""
 SOURCE_PATH = ..

# Set static runtime.
--- qtbase/mkspecs/win32-msvc2012-static/qmake.conf 2014-02-17 23:01:29.965440300 -0500
+++ qtbase/mkspecs/win32-msvc2012-static/qmake.conf 2014-02-17 23:05:51.630568400 -0500
@@ -24,9 +24,9 @@
 QMAKE_CFLAGS            = -nologo -Zm200 -Zc:wchar_t
 QMAKE_CFLAGS_WARN_ON    = -W3
 QMAKE_CFLAGS_WARN_OFF   = -W0
-QMAKE_CFLAGS_RELEASE    = -O2 -MD
-QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi
-QMAKE_CFLAGS_DEBUG      = -Zi -MDd
+QMAKE_CFLAGS_RELEASE    = -O2 -MT
+QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MT -Zi -d2Zi+
+QMAKE_CFLAGS_DEBUG      = -Zi -MTd -d2Zi+
 QMAKE_CFLAGS_YACC       =
 QMAKE_CFLAGS_LTCG       = -GL
 QMAKE_CFLAGS_MP         = -MP
# Set static runtime.
--- qtbase/mkspecs/win32-msvc2012-static-xp/qmake.conf  2014-02-17 23:01:29.965440300 -0500
+++ qtbase/mkspecs/win32-msvc2012-static-xp/qmake.conf  2014-02-17 23:05:51.630568400 -0500
@@ -24,9 +24,9 @@
 QMAKE_CFLAGS            = -nologo -Zm200 -Zc:wchar_t
 QMAKE_CFLAGS_WARN_ON    = -W3
 QMAKE_CFLAGS_WARN_OFF   = -W0
-QMAKE_CFLAGS_RELEASE    = -O2 -MD
-QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi
-QMAKE_CFLAGS_DEBUG      = -Zi -MDd
+QMAKE_CFLAGS_RELEASE    = -O2 -MT
+QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MT -Zi -d2Zi+
+QMAKE_CFLAGS_DEBUG      = -Zi -MTd -d2Zi+
 QMAKE_CFLAGS_YACC       =
 QMAKE_CFLAGS_LTCG       = -GL
 QMAKE_CFLAGS_MP         = -MP
like image 120
Kuba hasn't forgotten Monica Avatar answered Dec 28 '22 01:12

Kuba hasn't forgotten Monica