Qt Open Source 4.3.4 and Visual Studio

The commercial version of Qt has a very nice feature. Given a qmake project file, it is able to create a .vcproj file that can be opened with Visual Studio. If everything is setup correctly you can instantly compile your project using the Visual Studio C++ compiler. This way it is not much of a problem to develop a C++ project where some developers are linux geeks and some are lazy windows users who do not like mingw or make (for good reasons, of course) and who absolutely hate gdb (for even better reasons).

Most unfortunately, this feature was not available in the Open Source version of Qt. You either had to create their project files manually, which is a very painful task for several reasons – one of them being that the MOC and the UIC have to be integrated into the build process. Or you had to set up a Makefile project using Visual Studio. That’s what I was about to do this morning, when I stumbled across the following article:

Qt/Windows Open Source Edition to support VS Express

The most important statement in above article is the following:

“We have decided to support Visual Studio Express with Qt/Windows Open Source – we are dual licensing the MSVC Makefile and project generator (Sorry, no VS Integration for Open Source users)”

——RANT MODE: ON—–

Obviously, second-most important statement is this one:

“The Visual Studio Express environment is just so much superior and easier to use for existing Windows developers compared to what MinGW provides.” – I could not agree more.

——RANT MODE: OFF—–

So since 4.3.2 even the Open Source version of Qt is able to create vcproj files. However, since the Open Source version is configured for mingw when installed, just throwing qmake at your project file will only create a usual Makefile.

Creating a Visual Studio Project File from a qmake Project File

  1. To create a vcproj file, you will have to use the correct mkspec and change the template from “app” to “vcapp”. The following command does the job for Visual Studio 2005:
    qmake -tp vc -spec win32-msvc2005
    If you are using a different version of Visual Studio, have a loot at QTDIR/mkspecs to find the correct mkspec for you. QTDIR is the directory where you installed Qt.
    If your project file was named “myproject.pro” you will now find a file named “myproject.vcproj” in the same directory.
  2. Try to build the application. You will be greeted by the following error message:
    fatal error LNK1181: cannot open input file 'c:\<QTDIR>\lib\qtmain.lib', where QTDIR corresponds to the directory of your Qt installation.

Building Qt with the Visual Studio Compiler

The problem is, that the prebuilt binaries that come with the Open Source Qt distribution for Windows cannot be used by the Visual Studio compiler. Heck, they do not even have the right name.

How to fix this? Well, you have to build those files from the Qt sourcecode using the Visual Studio compiler. Do the following:

  1. (Skip this step, if you are using the commercial version of Visual Studio)
    The Express Version of Visual Studio does not include the Platform SDK. Unfortunately this SDK contains some headers and libs that are required to compile Qt. Go to Windows Server 2003 R2 Platform SDK Web Install to install it. Make sure to include the Core SDK AND the Web Workshop SDK during installation.
    Have a look in the directory of the SDK and locate the file SetEnv.Cmd. Add the directory to your path (Should be something like C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2. Do not forget to surround it with double quotes (“) if it contains blanks.)
  2. Add <VisualStudioDirectory> \VC\bin to your path.
  3. Then open a command line and go to the Qt installation directory. Type vcvars32.bat.
    This will create the environment variables required for the next step. The batch file resides in the VC\bin directory of your Visual Studio installation.
  4. (Skip this step, if you are using the commercial version of Visual Studio)
    Type SetEnv.cmd to create the environment variables necessary for including the files of the Platform SDK
  5. Type configure -platform win32-msvc2005.
    This will tell Qt to prepare itself for being compiled by the Visual Studio compiler. Again, if you use another version of VS than 2005, replace win32-msvc-2005 with the makespec appropriate for you.
  6. Type nmake. Then go and have a break. Compiling Qt will take a while.
    Actually, I was surprised that this last step works. Up to this day I thought that the OS version of Qt could not be built with Visual Studio. You had to apply a batch distributed by some helpful people under the name qtwin (Here is a german tutorial on that stuff). It appears that those dark days are over now.
  7. Now try building your application again. Everything should work fine now.

I hope this guide helps you to get going with Visual Studio and Qt Open Source. If you have any trouble or suggestions, feel free to drop me a mail or post a comment.

28 thoughts on “Qt Open Source 4.3.4 and Visual Studio

  1. Dhruv

    Thanks a ton. Was able to compile Qt using Visual Studio. But now…. What do i use to develop Qt apps. As in.. How do i run the ‘designer.exe’ for Qt?

    Reply
  2. admin Post author

    Hi Dhruv!
    I’m glad to hear that the post helped you.
    To run Qt’s Designer, have a look in the “bin” directory of your Qt installation (e.g. C:\Qt\4.3.4\bin). There you will find the designer.exe.
    Hth.
    Tom

    Reply
  3. Ali

    Thanks a lot for your post. How would you modify these settings for the Visual C++ 9.0 Express and Windows SDK 6.1? When I run SetEnv.cmd, I get this error:

    The x86 compilers are not currently installed.
    Please go to Add/Remove Programs to update your installation.
    Setting SDK environment relative to C:\Program Files\Microsoft SDKs\Windows\v6.1
    The system cannot find the batch label specified – Set_x86

    Reply
  4. admin Post author

    Hi Ali!
    I have not yet tested above guide with Visual Studio Express 9 and the new Windows SDK and I’m not sure if I will have a chance to do so in the near future.

    Judging by the error message, the SDK seems to have trouble finding the compiler. If this post
    http://permalink.gmane.org/gmane.comp.lang.eiffel.ise/2233
    is correct, it should be under VC\Bin in the Visual Studio directory just as in VS Express 8.0. Try adding that directory to your path.
    You can also have a look at SetEnv.cmd. In my version of the SDK it’s just a usual batch file. Perhaps there is a chance to figure out what’s wrong.

    If this works, please tell me, so I can add the step to the guide.
    Hth.
    Tom

    Reply
  5. Medleykupps

    Thank you very much.

    I’m new to the world of QT and was scratching my head, wondering how to get it kicking along with Visual Studio, until I found this page.

    Very helpful!

    Reply
  6. Andy

    Hi Tom,

    Useful information!

    I would just add (and I’ve not checked this, but I think it’s correct) that you don’t have to mess around with all of the setting environment variables if you just use the VS Command Prompt, which is located in my start menu at “Microsoft Visual Studio 2005 > Visual Studio Tools > Visual Studio 2005 Command Prompt”. This sets all the env variables for you.

    Cheers!

    Reply
  7. Ahmet ARDAL

    Thanks a lot… What I wonder is that how did Trolltech build the binaries in the commercial edition that makes it not work, don’t they know the problem? Anyway, after 2.5 hours build process it know works, thanks…

    Reply
  8. Jurrian

    Thank you very very much. This worked for me!
    Especially the part on the first project and creating the vcproj file helped me a lot.

    I now run a working Qt 4.4.3 with VC2005 Proffesional

    Reply
  9. Pingback: ThisIsNotATest » Blog Archive » Getting Qt and Ruby (=QtRuby) running on Windows XP

  10. Jan

    Thanks a lot, I have went through nmake project based tutorials and it finally failed for me…. this one really helped me to move on with Qt.

    But still I wonder how should I start completely new project in Qt within VS2005 and how to easily integrate the Qt Designer output files with project.

    thx

    Reply
  11. admin Post author

    Hi Jan!
    For starting a new project with Qt I suggest that you simply create a QMake project file (.pro) in which you put all your .cpp, h., and .ui files. Do not forget to add include and library pathes if your project needs them. Then go to the command line to create a vcproj file:
    qmake-tp vs your_project.pro
    A file named your_project.pro will be created in the same folder and can be opened in Visual Studio.
    So much for a short hint. If you require more detailed information, please let me know.

    Reply
  12. Jan

    I found very helpful, this link
    http://blog.idevelopsoftware.com/2006/02/14/howto-use-visual-studio-2005-for-qt-open-source-development/
    Actuly, it does the same what you suggested. But it uses the button toolbar to execute it. I thing it can speed up working with Qt.
    More over, you can start your Qt project like Win32 Consol Application.
    Then add all files you needed in standard way.
    Generate the .pro file using the Project File Gen. button.
    And then generate the .vcproj file using the VSProject File Gen. button.
    And Reload the project… its really comfortable, and lately it helps when adding more .ui files in project.
    Just click those 2 buttons and its done.
    Thanks anyway

    Reply
  13. admin Post author

    Hi Jan!
    Yes, Visual Studio’s ability of adding command line tools to the toolbar can help when working with Qt. Thanks for the link.
    However, I recommend, that one learns using the command-line version of QMake to understand what’s going on. Because when trouble strikes, some insight might be necessary to sort things out.
    Apart from that, having it all in one IDE is pretty comfortable – after all that is what Trolltech gives to the Customers of the Commercial Qt Version (called Visual Studio Integration). So, once more thanks for the link!

    Reply
  14. wjchoi

    hi, your post gave me a way to the lights.
    thank you. and i have a one question.

    i have already installed open source of qt 4.4.2 and it works well and you said that these open source binarys that provided from trolltech don’t work with visual studio complier, so same way i think these offsprings(qt library) from compiled opensource with visualstudio won’t work with mingw,eclipse.

    is this idea right? so i mean, is it possible to use both of 2 IDEs with that ?
    thank you again for good post.

    Reply
  15. admin Post author

    Hi Whchoi!
    The basic rule is: If you want to compile your application with compiler X you need Qt-binaries that have been built with X as well. Otherwise your binaries will be incompatible to the Qt-binaries.
    Working with 2 IDEs on the same application and on the same computer requires switching between two sets of Qt-binaries. This is possible, but probably cumbersome.
    Of course his is only true for the same computer. Working with Eclipse (gcc/mingw) on one PC and with Visual Studio on a different one is perfectly possible.
    Hth!
    Tom

    Reply
  16. Matthias Pospiech

    I am trying to compile 4.6.1 with VS 2005, but the necessary setup for the SDK fails. (Compilers not found). I am using Windows 7 and the corresponding SDK (Version 7.0).

    I have set up the path for SDK and VC 8, but still the error remains.
    Any hints anyone?

    Reply
  17. Mike

    Hi,
    I run congifure -platform win32-msvc2008 and Qt configured just fine.
    However, when I try to run nmake I’m getting some fatal errors.

    fatal error C1083: Cannot open include file:’windows.g’: No such file or directory

    NMAKE:fatal error U1077: ‘”C:\Program Files \Microsoft Visual Studio 9.0\VC\BIN\c1.EXE”‘: return code ‘0x2’
    stop

    NMAKE:fatal error U1077: ‘”C:\Program Files \Microsoft Visual Studio 9.0\VC\BIN\nmake.EXE”‘: return code ‘0x2’
    stop

    NMAKE:fatal error U1077: ‘cd’ : return code ‘0x2’
    stop

    Any idea? Thanks.

    Reply
  18. admin Post author

    Hi Mike!
    It looks like there is something wrong with your environment variables.
    Make sure, that the include-variable is set correctly, so windows.h can be found.
    You can print the contents of the include-variable using
    echo %include%
    Also try to use the Qt command prompt or the Visual Studio command prompt (you can find them in the start menu). They should have the environment variable preset correctly.
    Hth
    Tom

    Reply
  19. rprksh

    I run congifure -platform win32-msvc2008 and Qt configured just fine.
    However, when I try to run nmake I’m getting some fatal errors.

    fatal error C1083: Cannot open include file:’windows.g’: No such file or directory

    NMAKE:fatal error U1077: ‘”C:\Program Files \Microsoft Visual Studio 9.0\VC\BIN\c1.EXE”‘: return code ‘0×2′
    stop

    NMAKE:fatal error U1077: ‘”C:\Program Files \Microsoft Visual Studio 9.0\VC\BIN\nmake.EXE”‘: return code ‘0×2′
    stop

    NMAKE:fatal error U1077: ‘cd’ : return code ‘0×2′
    stop

    Reply
  20. admin Post author

    Hi rprksh (is that really your name? 😉
    It seems that the include paths are not set correctly. Try using the “Visual Studio” command prompt that is installed together with Visual Studio.
    To see whether the include paths are set correctly, use the command “set” and look for a variable named “INCLUDE”. If it is not there, than it is not set properly.

    Reply

Leave a Reply to Dhruv Cancel reply

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

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