How to Build Qt 5.7 Statically Using MSVC14 (Microsoft Visual Studio 2015)

Posted on Posted in Qt Tutorials, Tutorials

In this post I will describe how to build Qt 5.7 statically for Windows using MSVC 14 compiler or in other words Microsoft Visual C++ 2015 which is included in Microsoft Visual Studio 2015. Quite obviously you need to install Microsoft Visual Studio 2015 (Community edition is free) before continuing with this guide. You can find it here: https://www.visualstudio.com/

Next, you need to download the latest sources (in this case 5.7.0) of Qt from here: https://download.qt.io/archive/qt/5.7/5.7.0/single/





Create a folder named Static under default Qt installation.

C:\Qt\Static

Copy downloaded Qt archive to this folder and extract it there. So you will have a folder containing all Qt sources here:

C:\Qt\Static\qt-everywhere-opensource-src-5.7.0

Now go to C:\Qt\Static\qt-everywhere-opensource-src-5.7.0\qtbase\mkspecs\common and change msvc-desktop.conf like this (change all MD to MT to remove dependency on msvc dlls, only in the following lines.)

initial values:

QMAKE_CFLAGS_RELEASE = -O2 -MD
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi
QMAKE_CFLAGS_DEBUG = -Zi -MDd

should be changed to:

QMAKE_CFLAGS_RELEASE = -O2 -MT
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MT -Zi
QMAKE_CFLAGS_DEBUG = -Zi -MTd





Now press Start and run “Developer Command Prompt for VS2015” as Administrator.

Change directory using CD command to “C:\Qt\Static\qt-everywhere-opensource-src-5.7.0” where you extracted Qt source code.

Enter the following command to configure your Qt build in static mode. (You can get all configure options here but below is the minimum options I use which includes most widely used stuff.)

configure -static -debug-and-release -prefix “C:\Qt\Static\5.7.0” -platform win32-msvc2015 -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -qt-sql-sqlite -qt-sql-odbc -no-openssl -opensource -confirm-license -make libs -nomake tools -nomake examples -nomake tests

When configuration is completed, run the following command to start the build process. Go grab a cup of coffee cause it’s going to take some time.

nmake

When building is completed, you need to enter one last command to get a ready to use static build kit in “C:\Qt\5.7.0”

nmake install

And it’s done.





To use your new development kit with Qt Creator you need to add it to list of known kits. So open Qt Creator and go to Tools / Options from the main menu.

Then select Build & Run from the list of options and open Qt Versions tab. Then click Add.

qt-kit-1

Now select the qmake.exe that you just created from “C:\Qt\Static\5.7.0\bin” as seen below:

qt-kit-2

 





 

Next you have to change the kit name as seen in this picture:qt-kit-3

Then change to Kits and press the Add button.qt-kit-4

Now make sure you set all the options as seen in the picture below:

qt-kit-5

 





 

Now if you create a new project you will notice you can use the newly create kit to develop Qt applications that do not require DLLs to be deployed with them.

qt-kit-6

You’ll also notice that EXE files created using this kit are a little bit bigger than usual and that is because all the code is included in the app instead of DLL files.





6 thoughts on “How to Build Qt 5.7 Statically Using MSVC14 (Microsoft Visual Studio 2015)

  1. When i enter the command for configuring the Qt build in static mode, i get the follwoing…
    C:\Qt\Static_Raad\qt-everywhere-opensource-src-5.7.0>configure -static -debug-an
    d-release -prefix “C:\Qt\Static\5.7.0” -platform win32-msvc2015 -qt-zlib -qt-pcr
    e -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -qt-sql-sqlite -qt-sql-odb
    c -no-openssl -opensource -confirm-license -make libs -nomake tools -nomake exam
    ples -nomake tests
    + cd qtbase
    + C:\Qt\Static_Raad\qt-everywhere-opensource-src-5.7.0\qtbase\configure.bat -top
    -level -static -debug-and-release -prefix “C:\Qt\Static\5.7.0” -platform win32-m
    svc2015 -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -q
    t-sql-sqlite -qt-sql-odbc -no-openssl -opensource -confirm-license -make libs -n
    omake tools -nomake examples -nomake tests

    This is the Qt for Windows Open Source Edition.

    You have already accepted the terms of the license.

    Creating qmake…
    execute: File or path is not found (nmake)
    execute: File or path is not found (nmake)
    Cleaning qmake failed, return code -1

    Please provide suggestions, where i have gone wrong…

    1. Here’s what you need to check:

      1. Make sure you are using Visual Studio command line tools, or you have NMAKE on your PATH.
      2. Might be a problem with your VS installation, if you made sure about first item (1) above!

      1. Thanks for the help, I followed your instructions, But after i finish configuration and when i enter the command nmake, the following are displayed..
        C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xdebug(33
        ): could be ‘void *operator new(size_t,const std::_DebugHeapTag_t &,char *,int)’

        C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\crtdbg.h(
        1072): or ‘void *operator new(size_t,int,const char *,int)’
        while trying to match the argument list ‘(size_t, const std::_DebugHeapT
        ag_t, const char [72], int)’
        C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xlocnum(65) : err
        or C2665: ‘operator new[]’ : none of the 5 overloads could convert all the argum
        ent types
        C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xdebug(37
        ): could be ‘void *operator new[](size_t,const std::_DebugHeapTag_t &,char *,int
        )’
        C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\crtdbg.h(
        1080): or ‘void *operator new[](size_t,int,const char *,int)’
        while trying to match the argument list ‘(size_t, const std::_DebugHeapT
        ag_t, const char [71], int)’
        Generating Code…
        NMAKE : fatal error U1077: ‘”C:\Program Files (x86)\Microsoft Visual Studio 12.0
        \VC\BIN\x86_amd64\cl.EXE”‘ : return code ‘0x2’
        Stop.
        NMAKE : fatal error U1077: ‘”C:\Program Files (x86)\Microsoft Visual Studio 12.0
        \VC\BIN\nmake.exe”‘ : return code ‘0x2’
        Stop.
        NMAKE : fatal error U1077: ‘cd’ : return code ‘0x2’
        Stop.
        NMAKE : fatal error U1077: ‘cd’ : return code ‘0x2’
        Stop.
        NMAKE : fatal error U1077: ‘cd’ : return code ‘0x2’
        Stop.

        1. OK one thing that I noticed is that previously you mentioned setting platform to MSVC2015, but now you’re using MSVC2012. Make sure you go through all options carefully. Set the platform in CONFIGURE according to the VS version you are using.

Leave a Reply

Your email address will not be published. Required fields are marked *