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.





35 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.

  2. Hi there, you have a lovely tutorial here and it is so easy to read and follow. However, I seem to have made a mistake or did not do something right (although I followed your steps with as much as attention I could give), as I am given the following error:

    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

    I am unsure whether putting the whole dump was a good idea but still here it.

    Any idea why is this happening?

    Thank you, once again, for all the effort you have put into this wonderful tutorial.

    1. You haven’t sent any errors, this is the configure command which seems to be fine. Please send me the exact error message you’re receiving.

  3. Hi.
    First of all, thanks for this great tutorial.
    After putting the command, I get this:
    + cd qtbase
    + C:\Qt\Static\Qt5.8.0src\qtbase\configure.bat -top-level -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
    Please wait while bootstrapping configure …
    Perl not found in PATH. Aborting.

    What do you think I’ve done wrong?
    Thank you for your help

  4. When I try nmake install

    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\nmake.exe “‘ : return code ‘0x2’

    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

  5. I tried to build Qt following your instructions though I used MSVS Community 2017 and the newest version of Qt from git repository, and I slightly changed “configure” command:
    configure -prefix “f:\Programs\Qt\Qt5.9_MSVC” -developer-build -opensource -confirm-license -nomake examples -nomake tests -opengl desktop -debug-and-release -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -sql-sqlite -sql-odbc -make libs
    The build process took very much time and terminated during building of qtwebengine. There was a LONG list of errors and I had to divert output into a log file in order to get to the beginning of that list. In its beginning I found messages like
    “error LNK2038: mismatch detected for ‘RuntimeLibrary’: value ‘MDd_DynamicDebug’ doesn’t match value ‘MTd_StaticDebug’ in access_token_store_qt.obj”
    I suppose the problem may disappear if I discard changes in msvc-desktop.conf, but I don’t want to make dependencies on msvc dlls in my Qt. Do you have any idea how to fix it?

    1. Do you need to have QtWebEngine module in your build? My first suggestion is to try excluding Web Engine module and then rebuild since web engine is not configured to be built statically and you’ll need to change some parameters in that module too.

      1. I’ve built static version with changes in msvc-desktop.conf as you recommended and dynamic versions without such changes, keeping dependencies on msvc dlls. It will do. NMAKE completed building without errors, though without any messages indicating that the building has completed. Is it fine?
        Now I have a little test project (Qt Widgets Application) which works in a release build but have a “debug assertion failed” error in a debug build:
        File: minkernel\crts\ucrt\appcrt\heap\debug_heap.cpp
        Line: 904
        Expression: CrtlsValidHeapPointer(block)
        Also there is a compiler warning:
        “cl.exe” is used by qmake, but “” is configured in the kit.
        Please update your kit or choose a mkspec for qmake that matches your target environment better.
        These problems arise only with kits using MSVC compiler. What is wrong?

        1. Yes the build usually doesn’t provide any “Completed” message so that’s fine. As for the errors regarding the kit, you should make sure that everything is configured correctly including compiler and debugger. I recommend making a copy (clone) of an already working kit and just modifying the parts that should be changed.

  6. I checked that msvc-desktop.conf has these lines:
    QMAKE_CFLAGS_RELEASE = -O2 -MT
    QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MT -Zi
    QMAKE_CFLAGS_DEBUG = -Zi -MTd
    But when I run the executable in another pc, I get: vcruntime140d.dll, ucrtbased.dll, vccorlib140d.dll, msvcp140d.dll, and concrt140d.dll are missing, even after I installed Microsoft Visual C++ redistributable 2015.
    So, two questions:
    1. How can I remove dependency on those dll’s ?
    2. Why the executable depends on “d” versions ?
    thank you for your time !!

    1. Which version of Qt did you use? To answer your last question, it doesn’t, the executable does not depend on d versions, but you need to configure your conf file like that, since you don’t want a half-static build 🙂

      1. Solve it !!! I’m using Qt 5.9.1, and the problem was that I was building the app with other Debug libraries, that’s why the .exe depended on Debug C++ Runtime Compiler, but when I changed those libraries to Release versions, my .exe depended on Release MSVC.

        Thanks for your time and help

        1. I’m glad I have been of help. Also thanks for sharing your experience here. So you are able to build and run Qt 5.9.1 using the same instructions.

  7. Thanks for your tutorial, but I got some errors when I used configure. Here is my error:

    NMAKE : fatal error U1077: ‘”C:\Program Files (x86)\Microsoft Visual Studio 14.0
    \VC\BIN\link.EXE”‘ : return code ‘0x486’
    Stop.
    Could not find output file ‘arch.exe’ or ‘arch’ in C:/Qt/5.6.2/qtbase/config.tes
    ts/arch : No such file or directory
    Done running configuration tests.

    Do you have any ideas how to solve this?

    1. Can you please share the exact configure command that you used? I can guess what versions you used but can you also confirm that too? And your os and so on.

  8. hi sir , i m build qt5.9.1 statically and add needed libs from bin and platform directory to visual studio 2015 . project builds successfully but when run it i get an error “this application failed to start because it could not find or load the qt platform plugin “windows” in “”.” please help me . Tanks a lot , یا علی

    1. Hi Ali,
      Can you please be more specific? If you are doing a static build then you shouldn’t have anything in bin folder, see? So what you’re saying is somewhat confusing. That’s because you’ll only need lib files and not any DLLs. Can you confirm if your static build ends successfully?

      1. hi mr , how we can use this lib in visual studio . qtdesigner was not build in this case , how we can work with designer in visual studio ?

        1. Of course you can use Qt libraries regardless of the IDE. So basically, Yes, you can use the libraries in visual studio same as any other library. But I’d recommend using Qt Creator solely for ease of integration.

  9. My application uses custom plugins, activated with QPluginLoader, to extend the functionality. Is it possible to use a static build in this case?

    1. Can you be a little bit more specific? Do you want your application to be static or your plugin?
      Plugins are essentially separate from your app (they are apps that can’t run by themselves, or basically libraries), so you can have a plugin (built static or dynamic of course) and your app (build static or dynamic again) which use completely different set of Qt kits. You just need to make sure about the following:

      Plugins linked with a Qt library that has a higher version number will not be loaded by a library with a lower version number.
      Example: Qt 5.0.0 will not load a plugin built with Qt 5.0.1.
      Plugins linked with a Qt library that has a lower major version number will not be loaded by a library with a higher major version number.
      Example: Qt 5.0.1 will not load a plugin built with Qt 4.8.2.
      Example: Qt 5.1.1 will load plugins built with Qt 5.1.0 and Qt 5.0.3.

      Let me know if this is what you meant?!

      See this link for more info:
      http://doc.qt.io/qt-5/deployment-plugins.html

      1. I want to be undependent from OS qt version (some Linux distros have Qt 5.3 and older, but I using 5.9.1), so my app should be static. From other hand, me app architecture separate by two part: shell and plugins. Each plugin has standard interface and shell can load it using QPluginLoader::load.

        1. To be completely independent from Qt libraries provided by the OS you simply need to build everything statically. There’s simply no possible way around this as long as you want to use Qt. Otherwise you’ll be reminded about this with application crashes at run-time.

        2. Or, deploy the same version of Qt libraries that you use when you build your application and plugins. This is definitely the recommended, legal, and safest method.

          1. Yesterday I tried:
            1. Build Qt statically – OK
            2. Build my shell app statically – OK
            3. Build my plugins statically and load it in static shell – NOK.
            4. Build my plugins dynamically and load it static shell – NOK
            So, it’s looks like impossible to use dynamic plugins loading with static application.

Leave a Reply

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