How do you static link Qt (/MT) with visual studio 2013?



  • I'm trying to use Qt with openCV with visual studio. I have open CV statically linked (/MT) and working and I need to do the same with Qt. I read through several previous similar topics but none provide all of the necessary info needed.
    I have Qt installed at C:\Qt\Qt5.3.1
    I need the paths to the include directories to use, the libraries directory, and the long list of additional dependencies (*.lib files) so I can add all that to my project settings.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You'll have to first completely rebuild Qt yourself for that, and there's just a flag to change in the mkspec. However be aware that you'll be locked to that version of VS runtime and that you will have to ensure that all your libraries are using the same. It's generally recommended that even if you create a static lib/application you link it to the dynamic runtime.

    Hope it helps



    1. What do you mean I'll be locked into that version of VS runtime? I'll be locked into that version of Qt but that's the only consequence as far as I can tell.
    2. What or where are the instructions for rebuilding Qt the way you describe down to the level of "Open your Qt folder and click the file called xx. Select this option.." etc?

  • Lifetime Qt Champion

    1. "This":http://msdn.microsoft.com/en-us/library/ms235460.aspx has some explanation about what can happen with MT.

    It also means that if an update (bug fixe or other) to the runtime is provided by Microsoft, your application won't benefit unless you rebuild your application, Qt and all other libraries again with it.

    1. "Here":https://qt-project.org/faq/answer/why_does_a_statically_built_qt_use_the_dynamic_visual_studio_runtime_librar It boils down to modify the qmake.conf from your mkspec and follow the standard procedure.


  • Alright. That's good to know. Now what are the very specific steps to rebuilding it for static use?


  • Lifetime Qt Champion

    The "deployment guide":http://qt-project.org/doc/qt-5/windows-deployment.html shows how to build statically and the other wiki entry I gave you explain the additional step you need to do.



  • OK. The only actual steps I found that applied to me were:

    Open a command prompt and run:
    cd C:\Qt\Qt5.3.1
    configure -static

    Then, from qt-project.org:
    "Edit the qmake.conf file for your qmakespec. Where it says -MD you need to change it to be -MT"
    (That's simple enough except there are 279 qmake.conf files in different directories within the C:\Qt\Qt5.3.1 directory. It's also not clear if this is supposed to be done before the first step.)

    "For Visual Studio 2005 it is also necessary to change the relevant files in mkspecs/features to remove the call to mt.exe" Yea, this isn't clear at all and needs to be translated into actual steps.

    The good news is, if these questions get answered, by the end we'll have an actual step-by-step guide to doing something that, in my humble opinion, should already exist :-D


  • Lifetime Qt Champion

    Like written, this flag combination is not supported so there's no reason to have a guide for that.

    You have mkspecs for each VS version supported so just go there edit qmake.conf and then configure/build Qt. You have been warned, it will probably break.



  • The version of visual studio should be irrelevant. OpenCV supports what I'm asking just fine and even has the libraries already built for it without caring about what IDE I'm using. I want the same thing from Qt. I don't mind not having the Qt plugin work and I'm not trying to use the Qt IDE. I just want to make the gui portion of my program with statically linked Qt. Is that too much to ask?


  • Lifetime Qt Champion

    On the contrary the version of Visual Studio is very relevant, look at the OpenCV binaries, they provide one version for each version of VS they support. They are not compatible between them.

    I already wrote what you need to do: take the mkspec for your version of visual studio e.g. win32-msvs2012, edit the qmake.conf in it and replace /MD with /MT and then it's a normal static Qt build like described in the documentation.



    1. There is already a libs folder full of *.lib files at C:\Qt\Qt5.3.1\5.3\msvc2013\lib. Does that mean static compilation has already been done? (That was the case with OpenCV. A forum member thought it had to be rebuilt but it already came with what was needed for an /MT build)

    If not:

    1. So I would edit the qmake.conf file in:
      C:\Qt\Qt5.3.1\5.3\msvc2013\mkspecs\win32-msvc2013 ?

    2. I would change the following lines:
      QMAKE_CFLAGS_RELEASE = -O2 -MD
      QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi
      QMAKE_CFLAGS_DEBUG = -Zi -MDd

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

    ?


  • Lifetime Qt Champion

    1. No, on windows static libraries and import libraries have the same extension.

    2. First download the sources, then edit that file in the sources

    3. Correct

    Then build



  • I downloaded what was labeled as the source. It is a folder called "qt-everywhere-opensource-src-5.3.1" It seems to be non-specific and not designed for any particular IDE or anything which goes back to my original intuition. Almost every subfolder has a .qmake.conf file and none seem to have any mention of /MD switches. I'm guessing now that we're suddenly switching gears and using the source instead of the VS2013 specific Qt version that downloading the VS2013 version was a waste of time and should be deleted.

    1. What are the specific next steps after downloading the source?
      1a. What or which .qmake.conf files need to be edited and in what very very specific way?

  • Lifetime Qt Champion

    1. Download zip file (yes, the tar.gz is smaller but the line endings won't please windows)
    2. Uncompress zip file
    3. Edit qt-everywhere-opensource-src-5.3.1\mkspecs\win32-msvc2013\qmake.conf
    4. start a VS command line (64 or 32 bit depending on your needs)
    5. configure Qt with the options you want and -static, if you don't use webkit anywhere, don't build it it'll save you time. Also take extra care of what plugin you will want to use and build them in it will also simplify your life.
    6. call jom.exe (that you can find in your current Qt install) or nmake
    7. Enjoy you favorite beverage while Qt's compiling


  • Thank you very much for posting specific steps.

    1. I do not see a mkspecs folder. What is wrong here? (see attached image)
    2. Is there a link that outlines all of the different build options?

    !http://i.imgur.com/TcSarwP.png(pic)!


  • Lifetime Qt Champion

    Sorry, they are in qtbase

    configure --help will show you what you can do.

    I'd also recommend an out of source build so if something goes wrong you don't have to clean the sources.



  • I've done up to step 5 but didn't know what options to use so I just ran "configure -static". Now I need to run nmake but what should the specific commands and flags be?
    nmake clean
    qmake ?
    namke ?


  • Lifetime Qt Champion

    just nmake, it's like *nix make but from Visual Studio



    1. Is the following error worth worrying about? (it looks like it is trying to run python for some reason..)
    2. Is it intelligent enough to still try to compile everything else or did it stop and possibly not get to other things that would have successfully compiled?

    C:/Users/John/Desktop/qt-everywhere-opensource-s
    rc-5.3.1/qtbase/include/QtCore/5.3.1/QtCore -IC:/Users/John/Desktop/qt-everywhere-opensource-src-5.3.1/qtbase/include/QtCore jsapi\qjsengine.h -o .moc\debu
    g\moc_qjsengine.cpp
    python C:/Users/John/Desktop/qt-everywhere-opensource-src-5.3.1/qtdeclarative/src/3rdparty/masm/create_regex_tables > RegExpJitTables.h
    'python' is not recognized as an internal or external command,
    operable program or batch file.
    NMAKE : fatal error U1077: 'python' : return code '0x1'
    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.

    C:\Users\John\Desktop\qt-everywhere-opensource-src-5.3.1>


  • Lifetime Qt Champion

    Sorry, I've been building it for so long that I forgot the requirements.

    You'll find "here":http://qt-project.org/doc/qt-5/build-sources.html#windows the requirements as well as a more verbose set of instructions to build from source.



  • The overall goal here is to just be able to have an embedded OpenCV window, buttons, labels, checkboxes, sliders, etc in a GUI for a desktop application. The other requirement is that the desktop application not depend on any DLLs except what always comes with windows.

    ActivePerl, Python and Ruby are needed for ANGLE and Webkit respectively. I don't think I need those.. What are the specific flags to bypass those and to just build what I need for what is described above?


  • Lifetime Qt Champion

    If you just want the "base" configure and build only qtbase, to avoid ANGLE use -opengl desktop


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.