Qt 5 – How to compile Qt 5 from git source – Auto Compiling batch file

Did this article help you? Let me know in the comments.

To compile Qt 5 using Visual Studio easily

Compiling Qt 5 can become annoying, as it’s nothing but a routine task that could be time wasting. Here I’ll introduce a batch script that compiles Qt 5 from its git repository for me and for all those interested. The following is the batch script, and after it you’ll find instructions on how to use it:

@echo off
set "qtver=%1"
set "prefix=Qt"
set "suffix=_src"
set "buildSuffix=%2"
set "folder=%prefix%%qtver%%suffix%"
if "%buildSuffix%" == "" set compilefolder=%prefix%%qtver%
if NOT "%buildSuffix%" == "" set compilefolder=%prefix%%qtver%_%buildSuffix%

if "%qtver%" == "" goto :leave

CALL git clone https://code.qt.io/qt/qt5.git %folder%
CALL cd %folder%
CALL git checkout .
CALL git checkout %qtver%
if NOT %ERRORLEVEL% == 0 goto :errorcheckout
CALL perl init-repository
CALL cd ..\
echo Download done... press enter to start compiling.
pause

set PATH=%cd%\%folder%\bin;%PATH%
set QTDIR=%cd%\%folder%\qtbase
mkdir %compilefolder%
call cd %compilefolder%
set QMAKESPEC=win32-msvc2013
set CL=/MP
CALL ..\%folder%\configure -developer-build -confirm-license -debug-and-release -opensource -platform %QMAKESPEC% -opengl desktop -static -nomake examples -nomake tests -mp -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg

CALL nmake
::CALL nmake clean

:leave
if "%qtver%" == "" echo Please enter a version as the first parameter, and the build directory suffix as the second parameter
exit /b 1

:errorcheckout
echo Checkout error... The version you chose does not exist exiting
exit /b 1

Important: Starting from Qt 5.7.0, the options -c++11 doesn’t work anymore, so remove it from configure. This is because c++11 became a requirement at that release.

Notice the following:

1- Notice that :: before any line means it’s commented.
2- Notice also that this compiles a static version of Qt. You’re free to modify it!
3- I commented the command “nmake clean”, which you can enable. I commented it to ensure that compilation went fine before I do a clean up. A clean up is necessary, as it saves many GB of diskspace.
4- I don’t compile demos and example because they take huge diskspace with no benefit. Again, you’re free to modify this.
5- Don’t forget to change the following line with the correct version of your visual studio.

set QMAKESPEC=win32-msvc2013

Simply save the contents of this in a batch file, for example DownloadQt.bat, put it in a new, empty folder, and then execute it after following the following instructions.

The execution command is:

DownloadQt.bat <qt version> <compiled folder suffix>

For example, if you run the following:

DownloadQt.bat 5.5.0 64_VS2013

This will download the source to Qt5_src, and then create a new folder called Qt5.5.0_64_VS2013, where the compiled source is put. I use the suffixed to add information about VS version and whether it’s 32 or 64 bit.

Requirements

1- Git
2- Perl (I recommend strawberry Perl)
3- Windows SDK
4- Visual Studio or C++ Express (this you can get for free from Microsoft, but I assume you know enough about this already since you’re here)

IMPORTANT: It’s very recommended that you add git and perl to the %PATH% environment. The installers will ask you to do that, so accept it! They’re doing you a favor! Otherwise you’ll have to do it manually later.

32-bit or 64-bit?

Whether you’d like to have a 32-bit or 64-bit version of visual studio used depends on the environment variables that are defined. The easiest way is to run the run command prompt for the version you want. For example, in Visual Studio 2013, if one goes to Start, then types in quick search “Visual”, you’ll find a folder called “Visual Studio Tools”. This folder will have both command prompts with the relevant environment variables. The following shows this folder:

VisualStudioCMDShortcuts

It’s important that you compile Qt from such a command prompt.

PS: As far as I remember, Visual Studio 2010 Express and earlier doesn’t provide a 64-bit version (unless paid for).

Caveats about perl version

Perl is necessary because the Qt 5 repository is a git repository with submodules. Manually downloading the submodules could be annoying, so the Qt people created a perl script to do the job for you. Git for Windows comes with a version of perl. This version could be old and could cause problems. If you execute the script and it fails in downloading Qt source from the repository, you have to make sure of a few things:

1) Make sure that git is in %PATH% environement variable. When you’re in the VS201x command prompt, just execute “git”. If that creates a problem in the form of “not found”, then you should add git’s bin folder to path using

set PATH=C:\Program Files (x86)\git\bin;%PATH%

Remember to replace the path with your git path.

2) After ensuring git is in %PATH%, try running the batch script. If you get an error, this most likely means that the perl version is an old one that is taken from the git directory. To fix this, go to git’s bin folder, supposedly:

C:\Program Files (x86)\git\bin

and change the file name “perl.exe” to “perl1.exe”. This works fine. If you’re unconfortable with this, you can restore it after compiling your version of Qt 5.

3) Ensure that perl is also in path. Just run “perl” in your VS201x command prompt. Again, if it creates a problem, then you have to add “C:\Strawberry\perl\bin” to your %PATH%.

Modifying Qt before compiling?

The script is made to download and compile. To give you a chance to modify the downloaded source for any reason, the script notifies you and pauses after downloading the source and expects a key pressed from your side.

For example, you may wanna edit the following file in the downloaded source

qtbase/mkspecs/common/msvc-desktop.conf

By changing the following lines

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

To the following:

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

This will make the compliled Qt version really static against the compiler standard library. Otherwise you’ll require the runtime.

19 thoughts on “Qt 5 – How to compile Qt 5 from git source – Auto Compiling batch file”

  1. Many thanks for that batch !
    I tried it.
    I encounter C2061 syntax error QGenericUnixFontDatabase and then U1077.
    I wrote DownloadQt.bat 5.6.1 64_VS2015 and 2015 in the batch file.
    If you have any idea …

    Thanks.

    1. The last version I tested was 5.6.0. I’m testing this now with 5.6.1 on VS2013. I have a colleague who tested this a while ago with VS2015. So unless there’s a problem with the Qt source, there’s no reason why this shouldn’t work. So I would like to ask you to double check that you used the appropriate command prompt for Visual Studio that have the correct environment variables defined. You can find that in your start menu, in Visual Studio tools. Also make sure that you changed the parts in the script that are 2013 to 2015.

      I’ll give you my feedback soon on my tests.

  2. I tried with 5.6.0 and got the same error.
    I installed Python27, Ruby23, Strawberry Perl 64 bit 5.24.1 , Windows SDK for W7 (7.1).
    The path is:

    C:\Python27
    C:\Strawberry\perl\bin
    C:\Program Files (x86)\Git\bin
    C:\Ruby23\bin

    I do everything from a Developer Command Prompt for VS2015.

    1. I think I found it… just remove “-fontconfig” from the list of “configure” parameters. Apparently the last commit of Qt repository got the fontconfig platform corrupted. Once you remove it from there everything works fine.

      I modified the script accordingly, too.

  3. I tried it again and I didn’t get any error message.
    So I can guess that’s OK.
    Should it be any successful message ?
    The problem now is in VS 2015. When I want to add a new Qt version (QT5 options), I browse to … qtbase, I’ve got an error: “This Qt version uses an unsupported makefile generator (used: , supported: MSVC.NET, MSBUILD)

    I tried what I found on the web but without any sucess

    1. No there’s no specific successful message indicating success. Just not having errors is good enough.

      I assume you’re talking about Qt Creator? First I’d like to emphasize that you should’ve changed every 2013 to 2015. If you did that and your compilation is OK, then these messages should not make a difference. I always have an exclamation mark beside my Qt versions. It doesn’t do anything. Just test Qt Creator.

    2. after compiling, make sure to install it! (jom install or namke install). This will create the necessary files for VS add-ons…

      1. Actually I never had to install on Windows. On Linux, sure; but on Windows, never had to, and it has always worked fine.

  4. No I’m talking about Visual Studio 2015 when you enter the Qt Version on the tab Qt5. If you take a look on this issue on the web, people tell that it comes from the file msvc-desktop.conf.

    1. I’m sorry I don’t use Visual Studio as an IDE (just the msvc compiler). I use qmake files and Qt Creator.

  5. Great tutorial, Thanks.
    I wish to compile 5.7.0 with VS2015

    I removed the -c++11 option.
    set QMAKESPEC=win32-msvc2015
    And found I needed to modify qtbase\src\plugins\platforms\direct2d\qwindowsdirect2dbitmap.cpp line 83 to:
    D2D1_SIZE_U size = {
    unsigned int(width), unsigned int(height)
    };

    However, after running your batch I have 32 unresolved external symbols. Other webpages suggest adding library dependencies, but I’m unsure where or how?
    Any suggestions would be greatly appreciated!

    1. I had this problem, too, recently. Simply remove “-direct2d” from the list. I don’t think you need that. Once you remove it, it should work.

  6. Thanks for the reply: I have been following your post here: http://stackoverflow.com/questions/14932315/how-to-compile-qt-5-under-windows-or-linux-32-or-64-bit-static-or-dynamic-on-v
    I finally built with no errors. No when I: Tools->Options->Build & Run->Qt Versions and add qtbase\bin\qmake.exe I get a warning “no qmlscene installed”

    I then cloned a working kit and changed the QT version to my new static version. Now I get a red error, and when I attempt to build with my static kit I get a warning “Qmake spec does not set MAKEFILE_GENERATOR”

    Any help is very welcomed as I am completely out of my element.

    1. If the compilation is a success and no errors appeared, then you shouldn’t have any problems with Qt creator. Make sure that the compiler detected by Qt Creator is the same one you chose to compile Qt (whether x86 or x64). I often get yellow errors when I choose the Qt version, but I don’t care about it and it still works fine. So if you get errors, try simply to use Qt Creator and ignore the error, and see if it works. If it doesn’t, try to tamper with the configuration of the Qt version you selected. Sorry that I can’t provide more help with this.

  7. I have been facing problems with static linking until I found this solution. It is heavy but it has solved all my problems related to missing dlls.

    Basically, there is another way to build statically with the QT framework from 5.5 with the options “-static -static-runtime” with the configure command. As I have seen in a comment in the Qt forum and stackoverflow:
    https://forum.qt.io/topic/69926/build-static-linked-app-with-qt5-on-windows/5
    http://stackoverflow.com/questions/1011197/qt-static-linking-and-deployment

    Maybe you should take a look at that option.

    PS: I have looked at your blog and it is very nice. Thank you for sharing your projects ^^

    1. Thank you very much for the new information! I’ll try it when I compile the next version of Qt. Sounds like it will save me the trouble of editing the qmake files manually.

      Happy that you liked my blog. You’re welcome!

      Cheers!

  8. I’m trying to compile Qt 5.8 with MSVC 2017. I noticed the downloaded qbase\mkspecs folder contained ‘win32-msvc’ but not ‘win32-msvc2017’ nor the expected family of ‘win32-msvc201X’ folders … is Qt 5.8 no longer following that pattern?

    Choosing ‘win32-msvc’ worked for quite a while, until it got to building qtwebengine. Specifically it threw the not-so-descriptive error:

    Project ERROR: — running gyp_qtwebengine failed —

    It’s unclear from the Qt documentation if Qt 5.8.0 can be built with MSVC2017, and if true, perhaps this error is just a byproduct of this of not-yet-implemented functionality? It seems more likely that they just haven’t made the necessary updates regarding chromium. Have you had success building with MSVC2017? Any suggestions?

    1. I couldn’t compile Qt 5.8.0 ever… not even once. I gave up on it! It’s so messed up that release-and-debug mode doesn’t work properly. What I do currently is use 5.7.1, until Qt get their stuff together. It’s a mess!

  9. For those who want to use this script to build their qt with msvc here’s something I did:

    I replaced
    set QMAKESPEC
    with
    :: replace version number 14 and arch parameter x64 with the ones you need.
    if “%VisualStudioVersion%” == “” call “C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat” x64
    It might work with the QMAKESPEC so your experience may vary. But I found it better to make sure the build environment terminal was set within the script itself. Theoretically you could parametrisize the script call with the arch and version number, but I did not need it.

    Now a word of caution: make sure that the Visual Studio installation had all things required installed even when modifying the installation. I found 2015 to leave crucial components out with the default selections, which it did again when modifying the installation, removing required components that were installed…

Leave a Reply to Kellari Cancel reply

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

CAPTCHA ImageChange Image

This site uses Akismet to reduce spam. Learn how your comment data is processed.