Saturday, March 12, 2011

Qt 4.7.4 / 4.8 in Visual Studio 2010







Hello folks!
I know, my posts aren’t really active lately. But who cares! At least when I post, there’s something useful or interesting to read. So, this post is a little tutorial about getting Qt 4.7.4 (or 4.8 currently) working in Visual Studio 2010. Most of you probably think that VS 2010 isn’t really native supported by Qt. But guess what, it is! There just isn’t a precompiled package to download and use. So, I will guide you through the necessary steps to get this thingy working flawlessly.
Just 16 steps!
  1. Dude what the hell. Go install Visual Studio 2010 already!
    My installation only included the VC++ compiler, since it’s all that I needed. Be sure however that you include this compiler, or you won’t be able to compile the Qt library. (Now that would be stupid, wouldn’t it?)

  2. [Optional] (I guess). Install Service Pack 1 of VS 2010.
    But beware: according to a friend, it might have some bugs still :/

  3. Install the Qt Visual Studio Add-In:
    http://qt.nokia.com/downloads/visual-studio-add-in

  4. Download the Qt source code as .zip from:
    http://get.qt.nokia.com/qt/source/qt-everywhere-opensource-src-4.7.4.zip
  5. Download and install the following patch for VS 2010:
    http://support.microsoft.com/kb/2280741

  6. Extract the thingy from step 4 to some decent location like C:\Qt\4.7.4\
    Be sure the location has no spaces or special characters included.
    After this step, C:\Qt\4.7.4\ should contain a lot of folders.

  7. Add the folder from step 6 to your environment variables.
    Windows 7: Control Panel –> System –> Advanced System Settings –> Environment Variables
    Call it QTDIR.

  8. Open a Visual Studio Command Prompt.

  9. Make your way by cd’ing to the folder from step 6.
  10. It seems that 4.8.0 needs Perl to configure correctly. Install Strawberry Perl and everything should be fine. (Only for 4.8.0, not for previous versions like 4.7.3 etc.)

  11. Run the following command:
    ”configure -debug-and-release -opensource -shared -no-qt3support -qt-sql-sqlite -phonon -phonon-backend -no-webkit -no-script -platform win32-msvc2010”

    Explanation:
    -debug-and-release: … if you don’t know what this means, please stop reading now.
    -opensource: install open source edition of Qt.
    -shared: makes the Qt libraries shared.
    -no-qt3support: sorry my retro friends, don’t like old code.
    -qt-sql-sqlite: enable support for the sqlite driver.
    -phonon: compile the Phonon module (some multimedia thingy of Qt).
    -phonon-backend: compile the platform-specific Phonon backend-plugin.
    -no-webkit: disable WebKit module (heard of some bugs in combination with VS, just to be safe and since I don’t need it).
    -no-script: same as no-webkit.
    -platform win32-msvc2010: build for VS 2010! (Don't try win64, does not work)
  1. Skip reading and press “y”.

  2. Wait while Qt is getting configured for your platform.

  3. When done, run “nmake” to start compiling Qt.

  4. Wait again, long this time.
  5. When it’s done (few warnings, no errors on my side) open Visual Studio 2010. Go to Qt –> Qt Options and add the folder from step 6.

  6. Create a new Qt project (or open a .pro project). Should work flawlessly!
So that’s how I got it working here. I wish you all the best of luck on your Qt + VS 2010 adventures!

Feel free to share this tutorial by linking to this post, not copy+pasting the content.
Else I will be forced to get my friend Mathy to trace your IP and let your PC explode in your face.
See you all later,
Thomas
P.S.: This tutorial was originally written for Qt 4.7.2, but should also work with 4.7.3, 4.7.4 and 4.8!

45 comments:

  1. Merci stockx voor de tutorial! Een van de weinige tutorials die drekt van den eerste keren werken zonder al te veel gerommel. Stap 15 was bij mij zelfs niet nodig.

    Nice gedaan!

    Greetz

    ReplyDelete
  2. Hi, I have a question. What happens if i don't follow step 5?

    thanks

    ReplyDelete
  3. Well I guess it would still work. However, you might get some access violations when working on a x64 platform and using the -O2 optimization flag.

    This is a Visual Studio bug that has actually nothing to do with Qt. One of the optimizations the -O2 flag does, is converting 64-byte data to 16-byte data. When the data is larger than 64-bytes, a segmentation fault (access violation in VS) will occur.

    It's just a patch which I think everyone using VS 2010 with or without Qt should install.

    By the way, how did you stumble upon this tutorial?

    ReplyDelete
  4. i saw this blog on google while searching for building qt with visual studio.

    ReplyDelete
  5. Until step 11 everything is fine, but then the procedure is stoped and I get this message:
    Creating qmake...
    execute:File or path is not found (nmake)
    execute:File or path is not found (nmake)
    Cleaning qmake failed,return code -1

    Doyou know how to solve this? What I sould type next to run nmake?

    thanks for your instructions

    ReplyDelete
  6. Are you sure you haven't opened a regular command prompt instead of a Visual Studio one? You should find it in your Start Menu -> Visual Studio 2010 -> Visual Studio Command Prompt or something like that.

    ReplyDelete
  7. yes, I am sure about that.How the nmame is running from the command line prompt?Just typing namke?Sould I configure somehow nmake's path?

    ReplyDelete
  8. As found on:

    http://www.qtcentre.org/archive/index.php/t-11502.html

    If you go to your msvc installation directory, do a search for something called "vsvars32.bat"
    (in my installation, it's:

    c:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat )

    I copy that to somewhere accessible (and maybe in my path) and a name keyed to the compiler version (it exists in all the versions - I name it "vsvars32_2008.bat", for instance) - run that from the command line, and your environment will be set up for Visual Studio. Test by doing 'nmake /help' or 'cl' from the command line.

    Maybe that helps?

    ReplyDelete
  9. This helps to solve the problem.Thanks!

    ReplyDelete
  10. Hello,

    After the configuration we can run Qt Designer form vs2010 toolbar.The procedure is the same for configuring the Qt creator?

    ReplyDelete
  11. I am not completely sure why you would want to run QT Creator? Since Qt Creator is an IDE (like Visual Studio 2010 itself) it is not needed. If you want to use Qt Creator instead of Visual Studio you could just download a package from their site and you wouldn't have to go through all this trouble..

    ReplyDelete
  12. Thanks for your reply.Your answer is very clarifing!

    ReplyDelete
  13. One more queastion...After the configuration, we must build the .sln file, with cmake for example?

    ReplyDelete
  14. You can just open the .sln with visual studio and build your project using Visual Studio. No console commands like cmake needed since Visual Studio does that on its own.

    ReplyDelete
  15. after completing the step 10, i received an error like this. Please let me what should i do.

    Running syncqt...
    Creating qmake...

    Microsoft (R) Program Maintenance Utility Version 1.50
    Copyright (c) Microsoft Corp 1988-94. All rights reserved.

    Makefile(169) : fatal error U1088: invalid separator '::' on inference rule
    Stop.

    Microsoft (R) Program Maintenance Utility Version 1.50
    Copyright (c) Microsoft Corp 1988-94. All rights reserved.

    Makefile(169) : fatal error U1088: invalid separator '::' on inference rule
    Stop.
    Cleaning qmake failed, return code 2

    ReplyDelete
  16. Are you sure you are using Visual Studio 2010? Cause it seems that your nmake is the wrong version. Your version number is 1.50, while I have
    Microsoft (R) Program Maintenance Utility Version 10.00.30319.01

    ReplyDelete
  17. hi yes, its fixed.
    yes, i have VS 2010. but in system32 folder namke version in 1.5. thats the problem.. now its configured perfectly.

    how much space need in c: drive, its taken now 2.54 GB. so i received out of memory exception.

    actually i downloaded Qt4.7.2,so i created folder "c:\Qt\4.7.2" , but the VS searhing qmake.exe in c:\Qt\4.7.3.

    ReplyDelete
  18. I actually have no idea how much space it needs for building, I did it on a partition on which I had a lot of space left.
    Have you also added C:\Qt\4.7.2 to your environment variables?

    ReplyDelete
  19. YES BUILD SUCCEEDED.. THANKS THOMAS FOR YOUR GREAT HELP.

    ReplyDelete
  20. Hey... great article! I wasted my day downloading and fighting with their web installer and their add-in which even says 2010 support, but getting nowhere fast. Found yours and I'm compiling at step 13 now, so fingers crossed!

    Just a friendly FYI though... the text of your link to the SDK says 4.7.3 but the underlying href is still pointing to 4.7.2 (no biggie as I just copied the url, fixed it and typed it in my browser, but I thought you may wanna know! :)

    ReplyDelete
  21. One other thing... I actually *do* want the WebKit installed as I want to have browser functionality built in and I know so many things are based on WebKit (Safari, Chrome, Android & iOS Browsers, etc.) so is the only thing I have to do are repeat these steps without the flag? (I saw that I'll also have to clean out the older config first, but that's easy enough with the nmake flag.)

    Better yet, is there any way I can compile and include that one thing without having to recompile everything all over again?

    ReplyDelete
  22. And yup, if you want the WebKit installed just don't use that flag. Normally it *should* work but I can't guarantee it since I didn't try it myself.

    ReplyDelete
  23. About your last question, I actually have no idea. Never tried to change a configuration on the fly, I think configuring it again without the flag is the safest option..

    ReplyDelete
  24. Cancel that last comment. While I can create a project now which I couldn't even do before, I can't however compile (well 'link' specifically.) I'm getting "error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'"

    For the record, this is Win7 64-bit and I did use the 64-bit command prompt. Not sure why it's detecting a machine type of x86. Any ideas?

    ReplyDelete
  25. Hmm strange. I also have Win7 64-bit so that can't be the problem. A quick google showed me:

    Right click on project->properties->configuration manager button-> Active solution configuration

    Make sure that if you are using a 32 bit machine, then solution configuration is x86 and if you are using 64 bit, then it should be x64 .

    I have no idea if that fixes your problem though, since I haven't encountered it myself.

    ReplyDelete
  26. I ended up resolving this by just manually uninstalling everything and starting over according to your steps above (except again, I added WebKit and a few other things back in.) I think the issue is I had tried the stand-alone installer for the VS Add-In first (which said it supported VS 2010) let alone had already run the Win32 QT installer. Again, just removing all of that, then clearing out the registry and finally following your steps, it works.

    Quick question for you though... I want to develop for both PC and Mac (hence QT!) Do I build the mac executable on the PC, or do I have to move the source code over to the Mac and build there under Xcode, even if I'm using the same source?

    ReplyDelete
  27. I must admit that i can't answer your last question, since the only cross-platform developing I did, was using Java :D

    Glad you got it to work though.

    ReplyDelete
  28. I jumped step 2 and step 5, and download qt-vs-addin-1.1.9.exe from http://qt.nokia.com/downloads/visual-studio-add-in.

    Everything goes well, no troubles.

    Thank you very much.

    ReplyDelete
  29. No problem, I wasn't sure about step 2, but skipping step 5 will get you into trouble sooner or later if you're developing on 64-bit.

    (Or it could be that Windows Update already got you the fix ofcourse)

    ReplyDelete
  30. Hi,
    thanks for the tutorial first.
    I receive an error when configure tries to run qmake. All paths are set. That's the output:

    [...]
    Binaries installed to.......D:\Program Files\QtVS2010473\bin
    [...]
    Creating qmake...

    Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
    Copyright (C) Microsoft Corporation. All rights reserved.

    Creating makefiles in src...
    Generating Visual Studio project files...
    execute: File or path is not found (D:\Program Files\QtVS2010473\bin\qmake)
    Qmake failed, return code -1

    Any idea how fix that?
    Thanks!
    Tim running Qt 473 on Win7 32

    ReplyDelete
  31. P.S.: Same counts for other paths like C:\Qt\473

    ReplyDelete
  32. Hey Tim,

    Are you sure you have added your other paths to your environment variables?
    Putting the Qt environment in your Program Files is not something I'd recommend, that's bound to give problems. Something like C:\Qt\473 should work however, but be sure to add that path to your environment variables, since it seems to be a problem of that kind.

    ReplyDelete
  33. Hi Thomas,
    thanks for your reply. What other paths do you mean? I added c:\qt\bin to PATH and QTDIR=c:\qt. Is there any more to do? It doesn't matter where I install qt to. I cannot remember any difficulties installing qt 472 on XP so I'm wondering whether this is win7 or 473 related.
    Cheers,
    TiM

    ReplyDelete
  34. If you put Qt in C:\Qt\473, you should add C:\Qt\473 to your environment variables. Not just C:\Qt. That should do the trick. Try playing around a bit. If it still doesn't work, I have actually no idea.

    Also running win7 shouldn't be a problem, since I wrote this tutorial on a Win7 x64.

    ReplyDelete
  35. Hi,
    thanks again. I just abreviated the qt dirs. Of course i addded the whole path to PATH. I just switched back to 472, runs flawlessly.
    When i come up with a solution (or stumble over it) I'll post it here.
    TiM

    ReplyDelete
    Replies
    1. I got the same problems as yours, I even tried to switch to 472, but still no luck. Any ideas?

      Thanks.

      Delete
  36. This comment has been removed by the author.

    ReplyDelete
  37. YOU can speed up the compilation by using Bjam that's support multi core.
    Download the latest version of jom here ftp://ftp.qt.nokia.com/jom/
    and replace 13) run “nmake” to start compiling Qt with ..\jom\jom.exe -j 4 (if jom is into c:\qt\jom)

    ReplyDelete
  38. It seems that 4.8.0 needs Perl to configure correctly. Installed strawberry Perl and everything works fine.

    Btw. don't try to change -platform win32-msvc2010 to win64-msvc2010, this platform is unknown.

    ReplyDelete
  39. Thanks Puksa, I'll add it to the tutorial.

    ReplyDelete
  40. I got no error from any steps. But, when I open my VS, I don't see any Qt menu? Did I do something wrong?

    ReplyDelete
  41. I tried again, but I got this while doing step 11:
    =======
    copy qmake.exe D:\MsCV\PROJECT\3D+MultiSpetral\Qt\4.7.4\bin\qmake.exe
    The syntax of the command is incorrect.
    Creating makefiles in src...
    Generating Visual Studio project files...
    execute: File or path is not found (D:\MsCV\PROJECT\3D+MultiSpetral\Qt\4.7.4\bin
    \qmake)
    Qmake failed, return code -1

    Generating Makefiles...
    execute: File or path is not found (D:\MsCV\PROJECT\3D+MultiSpetral\Qt\4.7.4\bin
    \qmake)
    Qmake failed, return code -1

    =====

    Not sure why. Please answer both my problems.. thanks. :D

    ReplyDelete
    Replies
    1. Hi Andru,

      Since you get errors saying path not found, the cause probably has something to do with the path your library is located.

      You should probably double check all folder names and make sure that the correct path is added as an environment variable.

      Delete