Switching Between Shared and Static Libraries in Qt 4.8.0

Qt is convenient for cross-platform development, but on Windows, if you link the shared libraries, running the program on machines other than the development machine that do not have the Qt runtime DLLs is troublesome. On the other hand, linking the static libraries makes the executable considerably larger.

A normal installation of the Qt SDK on Windows installs only the shared libraries, so to switch between static and shared libraries as needed, you must build the static libraries yourself from the source code.

Assumed environment

  • Windows 7 64bit
  • Visual Studio 2010
  • A Perl environment is required, so download and install ActivePerl. http://www.activestate.com/activeperl/downloads

Downloading the Qt SDK

Download from http://qt.nokia.com/downloads-jp

  • Qt 4.8.0 for Windows(VS 2010, 275 MB)
  • Qt Creator 2.4.0 for Windows (53 MB)

Download from ftp://ftp.qt.nokia.com/vsaddin/

  • qt-vs-addin-1.1.10.exe

Installation directories

  • C:LangQt4.8.0st Static libraries
  • C:LangQt4.8.0 Shared libraries

Installing the static libraries

(1) Run qt-win-opensource-4.8.0-vs2010.exe and follow the wizard to install. (Installation directory: C:LangQt4.8.0st)

(2) Create a qt.bat file like the one below and run it from the command prompt.

SETLOCAL
CALL "C:Program Files (x86)Microsoft Visual Studio 10.0VCvcvarsall.bat" x86
ECHO ON
SET QTBASEDIR=C:LangQt4.8.0st
SET QTDIR=%QTBASEDIR%
SET PATH=%QTDIR%bin;%PATH%
SET PATH=%SystemRoot%System32;%PATH%
SET QMAKESPEC=win32-msvc2010
%comspec%
ENDLOCAL
SETLOCAL
CALL “C:Program Files (x86)Microsoft Visual Studio 10.0VCvcvarsall.bat” x86
ECHO ON
SET QTBASEDIR=C:LangQt4.8.0st
SET QTDIR=%QTBASEDIR%
SET PATH=%QTDIR%bin;%PATH%
SET PATH=%QTBASEDIR%bin;%PATH%
SET PATH=%QTBASEDIR%mingwbin;%PATH%
SET PATH=%SystemRoot%System32;%PATH%
SET QMAKESPEC=win32-msvc2010
%comspec%
ENDLOCAL[/shell]

(3) Edit C:LangQt4.8.0mkspecswin32-msvc2010qmake.conf and change it as follows.

QMAKE_CFLAGS_RELEASE = -O2 -MD => QMAKE_CFLAGS_RELEASE = -O2 -MT
QMAKE_CFLAGS_DEBUG = -Zi -MDd => QMAKE_CFLAGS_DEBUG = -Zi -MTd

(4) At the command prompt, move to C:LangQt4.8.0st and run the following.

configure -debug-and-release -static -D _CRT_SECURE_NO_WARNINGS

(5) After running configure, enter the following, then wait a while.

o (use as open source)
y (LGPL)

(6) Build the static libraries with the following command (this takes quite a long time). Plain nmake would try to rebuild even the tools using the static libraries, so add the option.

nmake sub-src

(7) Delete unnecessary files.

nmake clean

(8) For unnecessary files that were not deleted (listed below), search for those created that day and delete them too.

  • Makefile*
  • *.vcxproj
  • *.vcxproj.*
  • *.sln
  • tmp

Installing the shared libraries

Run qt-win-opensource-4.8.0-vs2010.exe and follow the wizard to install. (Installation directory: C:LangQt4.8.0)

Switching libraries in Visual Studio 2010

  1. From the menu, go to [Qt]-[Qt Options] and add 4.8.0st and 4.8.0.
  2. From the menu, go to [Qt]-[Qt Project Settings] and select 4.8.0st or 4.8.0.
  3. From the menu, go to [Project]-[… Properties] and set the runtime library for C/C++ code generation to MT (static) or MD (shared).

Qt Creator settings

  1. If you want to use Qt Creator instead of VS2010, install qt-creator-win-opensource-2.4.0.exe. You don’t need to install MinGW.
  2. After installation, in [Tools]-[Options], under [Build & Run], click the “Add” button and add C:LangQt4.8.0binqmake.exe.