Friday, February 2, 2007

Qt4 on Free IDE Visual Studio Express 2005 C++ with Qt code completion

HowTo Setup Qt 4.2.2 With Visual C++ Express (Free) Edition

With Qt Code Completion

Note: An updated version of this is also published on KjellKod.cc

1) Download and install Microsoft Visual C++ Express edition in the installation instructions they mention that you should ALSO install the SDK, don't forget to DO just that. Very Important: Don't forget to follow the instructions on that page. Since they will describe how to setup the SDK to work for this version of Visual Studio. NOTE that in these descriptions some files that may need to be deleted will be HIDDEN so using the search or 'un-hide' files might be good.

NOTE: If you update Visual C++ express with SP1, then you must also apply some hotpatches, since bugs are introduced with the SP1. See http://support.microsoft.com/kb/930198 for more information

2) Download Qt (if you haven't done it already) . Download also a patch for it, patch for 4.2.2 can be found here (not official?)

3) Unpack Qt to a location that does NOT contain spaces. I.e. "C:\Program Files" is NOT a good choice. I used I:\lib\Qt\4.2.2

4) Set System Environment Variables
QTDIR variable as a system environment variable
I.e. My Computer->Advanced->Environment Variables
Variable name: QTDIR
Variable value: I:\lib\Qt\4.2.2

Similarly make another system environment variable QMAKESPEC to win32-msvc2005
Note: If you want to use for example ming-gw you should do this one differently win32-msvc2005 is ONLY if you want to use the compiler & debugger that comes integrated with Visual Studio 2005 Express C++ (damn, that's a long name)

Add also PATH to be the path to your Qt\bin directory i.e. in this case I:\lib\Qt\4.2.2\bin

5) Now test that your system environment variables are working. Open up a prompt and type
echo %QTDIR%
echo %QMAKESPEC%
Both times you should have gotten the corresponding value for that system environment variable.

6) Unpack the Qt patch to where you installed Qt and open a cmd prompt and do cd %QTDIR%
, run installpatch42.bat.

7 a) Now it's time to setup the VC++ compiler and to compile Qt.
First you need to patch the vsvars32.bat script that are found in "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools" . Copy it to a safe location in case you mess up.

Add to the INCLUDE, LIB, and BIN lines according to following:
PATH Original:
@set PATH=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft Visual Studio 8\VC\BIN;C:\Program Files\Microsoft Visual Studio 8\Common7\Tools;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\VCPackages;%PATH%

PATH Changed to:
@set PATH=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft Visual Studio 8\VC\BIN;C:\Program Files\Microsoft Visual Studio 8\Common7\Tools;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\VCPackages;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin;%PATH%

INCLUDE Original:
@set INCLUDE=C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;%INCLUDE%

Include Changed to:
@set INCLUDE=C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include;%INCLUDE%

LIB Original:
@set LIB=C:\Program Files\Microsoft Visual Studio 8\VC\LIB;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;%LIB%

LIB Changed to:
@set LIB=C:\Program Files\Microsoft Visual Studio 8\VC\LIB;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib;%LIB%

Run the vsvars32.bat script which sets up the system environment for the VC++ compiler.
I.e. you are standing in %QTDIR% then you are running WITH the " signs: "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"

7 b) Run qconfigure.bat script as qconfigure.bat msvc2005. Answer Yes to all questions.

7 c) Run nmake which will compile & "install" Qt. This seems more accurate though than only running qconfigure.bat - beware that it'll take some time since your whole Qt installation is compiled.

8) Setup VC++ Environment Configuration

8a) Include files that you need when including Qt into your source code
Go to Tools->Options->Projects and Solutions->VC++ Directories->Include files
Add the following either with the syntax of $(QTDIR)\... or with the full path:
$(QTDIR)\include
$(QTDIR)\include\Qt
$(QTDIR)\include\QtCore
$(QTDIR)\include\QtGui
$(QTDIR)\include\QtNetwork
$(QTDIR)\include\QtSvg
$(QTDIR)\include\QtXml
$(QTDIR)\include\Qt3Support
$(QTDIR)\include\ActiveQt

... etc, for all the include directories that you might need


8b) Library files that you need when including/building Qt & source code
Go to Tools->Options->Projects and Solutions->VC++ Directories->Library files
Add $(QTDIR)\lib

8c ) Source files that you need to enable Intellisense to do Qt code completion
Go to Tools->Options->Projects and Solutions->VC++ Directories->Source files
Add $(QTDIR)\src

If you had an old project than maybe you also need close down VC++ and then delete the IntelliSense file (*.ncb). Don't worry when you start the project again the file will be re-created.

"Don't fix it unless it's broken"
When I tried to compile my first project (completely unrelated, but it was QuickFix) I ran into some trouble due to that the wchar_t was defined in an incompatible way in the old libraries compared to Visual Studio 2005 Express. This could be rectified by setting up the QuickFix project so that the wchar_t should use the code definition rather than the VSC++ definition.
To do this: Right-click on project (or sub-project), choose
References->Configuration Properties->C/C++->Language->
Treat wchar_t as a built-in type change from Yes to No

"Create your first project - A simple Hello World in Qt"
OK, now you're ready to try your first project. Since Qt already comes with a very easy and nice way to manage the make system we're going to use that.
Creating a new Project as such:
File->New Project->Visual C++ ->General->Makefile Project
(set up Name, Location etc according to your liking)

Create an very simple .pro file (these can also be autogenerated)
This will be the basis for your Qt auto-updated Makefile later
It can look something like this (whatever_project_name_you_like.pro)

TEMPLATE += app
CONFIG += qt warn_on
SOURCES += main.cpp
TARGET = whatever_project_name_you_like
win32:debug:CONFIG += console

Add a file also to the project:
(Right click project -> Add new item -> C++ file (call it main.cpp)
Make it look something like this:





Now set up your environment to autogenerate Makefiles and compile according to your .pro file
Configuration Properties -> NMake -> General
Build Command Line qmake && nmake debug
Rebuild Command Line qmake && nmake debug-clean
Clean Command Line nmake debug-clean
Output debug/projectname.exe (Just an example on the output)

Try compiling it, it should work now

Good Luck :-)

-----------------------------------
I moved my blog entry below here so that you folks wouldn't get side-tracked by seeing it first ;-)
--------------------------------------
For the last 5 years I have mostly worked with Linux - developing software using IDEs such as XEmacs and KDevelop.

I think KDevelop is promising but still very buggy. I heard some rumour that they will come out with a cross-platform IDE soon. THAT would really be something.

Anyhow, since I quit Park Air Systems in Norway in 2006 and started working for HiQ in Sweden I have slowly moved from Linux/Unix SW Engineering to now mostly on Windows. Since I still love to use Qt ( Trolltech )'s libraries I wanted a way to integrate them on the IDE's that I can use on Windows.

I have tried QDevelop and Edyuk for my own project (KjellKod) at home but someone at work said that now Visual Studio comes as a FREE (for Open Source only?) IDE version. I've tried it at work and at home and after MINOR problems got it up and running AND being able to use with their Intellisense code completion some third-part code like Qt 4.2.*

I WISH developers for other IDEs would look at Visual Studio Express 2005. It is CLEAN it is EASY to configure AND it does not CRASH (hint to KDevelop poeple)...

Without wanting to start a Flame War I thought I would post how to set it up and what I did to fix some minor quirks that I found. Mind you that the ORIGINAL articles that I followed for a starter were "Building QT 4 with Visual C++ 2005" and "Compiling Qt4 on Windows" I have just added some little tidbits that I thought were missing ... (now WHY haven't they set up their pages to recive feedback?)


37 comments:

Unknown said...

Hi KjellKod, At last I could enable Intellisense!. Great job!Thanks.
I was using VC++ 2005 EE now for half an year and I found it very useful. Just for the records, with the aid of MS Platform SDK and DirectX SDK I could develop (doing some patching work over the samples...) applications that involves DirectShow, DirectSound, and also various console applications. One of these apps is a big project and the main subject of my job at this company.
I also found Qt amazing but still I am a beginner. Recently I made a mix between a GUI made in Qt but in the background it calls (through QProcess) to a console application I made entirely in VC++2005. I am now rewriting the same application but as a Qt GUI with all the code of the console application included.
My comment is for encourage other people to use these powerful tools. They are nice, easy and there are a lot of people like you making things easier for beginners.
Thanks again.

KjellKod said...

Sometimes IntelliSense is out of order it seems, but following step 8c) brings it back to Qt-Code-Completion order again


8c)If you had an old project than maybe you also need close down VC++ and then delete the IntelliSense file (*.ncb). Don't worry when you start the project again the file will be re-created.

Unknown said...

Hi KjellKod,me again. Something went wrong when I tried to post the same comment so maybe this will appear two times.
I have one question that maybe you have already solved:
I built Qt in release mode through:
qconfigure.bat msvc.net -release -no-stl
from instructions in QtNode.
This does not allow me to make debug process in VC2005 EE. So I need the debug mode also. If I follow all the way using the -debug key the process will replace all the modules and dlls or just will add debug versions of them? What is your experience? As I can see you don't use any key at all when run qconfigure...

KjellKod said...

Elpepe,
I would re-configure (& compile) Qt with the -release tag removed.

By default Qt does both. I.e. it'll create two directories release
and debug and put corresponding versions in both of them; you can then very easily in VC++ choose which versiion to run (& debug).

Try it with a simple Hello World application if you're unsure how it works; it IS of course also dependent on how you setup your Make system...

Anonymous said...

HI Kjellkod,
I need to complie qt3 free on Visual Studio express 2005, and like you , I tried to follow the instructions on the QTWIN.SOURCEFOURGE.NET page - with no luck - I need specifically QT3 for an existing project DreaM radio on sourceforge.
any tips on doing this - I am a beginner to C++, so any help appreciated! Thanks, Andy (EI3HG) in Ireland

KjellKod said...

I haven't tried VC++ Express with Qt3 - but I don't think it should be a big difference.

From at least Qt version 3.3.7 you also have the 'multiple compiler' support patch which can be downloaded from http://sourceforge.net/project/showfiles.php?group_id=49109

If you have the possibility to use Trolltechs official Qt4.x.x - I recommend that highly since Qt3 has its share of bugs

Otherwise I belive the latest Qt Free (NOT TROLLTECH) is 3.3.7 from http://sourceforge.net/forum/forum.php?forum_id=655298

If you still want to use Qt free then go ahead and try do it according to my instructions on this blog. If you have any specific problem then you can always post a comment here or at

www.qtcentre.org

Anonymous said...

Hi Kjellkod. I am truing to compile QT-3 using your instructions (which do seem very precise - thanks!) but when I need to run qconfigure.bat, there is no such file in the qt-3 directory, only configure .bat (and several other configure, such as configure-msvc.bat etc.
So I tried tor run configure.bat, but this is all that happened:

C:\>echo %qtdir%
C:\source\qt-3

C:\>echo %QMAKESPEC%
win32-msvc2005

C:\>Cd Source\qt-3

C:\source\qt-3>"D:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars3
2.bat"
Setting environment for using Microsoft Visual Studio 2005 x86 tools.

C:\source\qt-3> configure.bat msvc2005
install headers for bootstrapping...
building header copy tool...
'make' is not recognized as an internal or external command,
operable program or batch file.

An error occured. Configuration aborted.

C:\source\qt-3>
(by the way, I have some of my programs on D:\program files, as well as C:\ program files, but I am careful to refer to them properly in paths and when setting environment variables

By the way, the town I live in, Waterford (Wasserfjiord?)has a very strong viking History, with many Vikink parts of the old city walls still standing!

Thanks, Andy
EI3HG

KjellKod said...

Hmmm.

Since you're using Visual Studio it would be 'nmake' and not make that you should be using.

It seems that your configure script tries to use maybe 'mingw' instead!

1) Make sure that all the path's are up and running. Before running the configure script test them AGAIN by printing stuff like 'nmake', 'echo %QTDIR', 'echo %QMAKESPEC% (which I have to win32-msvc2005). Make sure that all those 3 work.

2) Check how fare your configuration script goes. If it crashes when its calling 'make' maybe you can just edit the script and put in 'nmake' instead of make. Or do it manually -i.e. a)configure b) nmake

But seriously. I really recommend qtcentre.org where there probably are tons of people that have already done what you're trying to do. You should post your questions there as well

richard said...

Top notch 'howto'. Thanks.

Mateusz Loskot said...

Hi KjellKod,
I added link to your great post to my Building QT 4 with Visual C++ 2005 HOWTO about as an external reference.

Educational Technology and Training said...

Thanks for this how-to! There seems to be a very minor typo, however. In step 8b, you wrote:

Go to Tools->Options->VC++
Directories->Include files

I think you meant to write "Library files", instead of "Include files".

Marito said...

Hi Kjellkod,
I wrote some pages for those who are trying to use MS VC++ 2005 Express Edition. It includes the installation, the use of MS PSDK, Qt and also some work around for dealing with DirectX SDK DirectShow samples. This document comprises many of the troubles I had to deal with after installed MS VC++ 2005 Express Edition. Finally I have everything working so I want to share the doc with other people. I wonder if you can distribute it (and also take a look to see if there's something missed). If you cannot, maybe you know where it could be published

KjellKod said...

Marito,

I don't mind providing a link to your document on my blog. Otherwise a good place for it could be to contact the admin of QtCentre.org - maybe they are interested in such a HowTo

If you just want a place on the WWW to put your document I suggest docs.google.com where you also can publish the document so that it is viewable for the public

Marito said...

Hi, following your suggestion I published at : http://docs.google.com/Doc?id=ddtcbbcp_3ghccdk
If you can, please take a look at it to see if there's something I forgot to include, or something is wrong. Additionally if know someone else that can be interested in this subject please give them this link. Thanks and regards.

KjellKod said...

Looks OK Marito

Anonymous said...

Hi KjellKod,
I try to setup Qt With Visual C++ Express according to your instruction, but I have problem in step 7 c). When I run nmake, cmd write "NMAKE: fatal error U1064: MAKEFILE not found and no target specified". (To be honest I also didn't find any makefile.) Can you help me?
Thanks!

btw I'm sorry for this stupid questions, but I'm just beginner in this area :)

KjellKod said...

Hi Libor,

All the other steps went OK? From what you've already guessed yourself you seem to lack the Makefile - or MAYBE you're not in the right location?

I would re-do it from step 2) (install Qt) since you're having problem with Qt. Make sure to closely follow the output from the install scrips.

Verify that the following is set up correctly
* (4) i.e. "PATH to be the path to your Qt\bin directory" , (5), (6), especially (7b)

Anonymous said...

Hi KjellKod,

I try re-do it from step 2 ... everything seemed to be ok now. But after few minutes nmake running, cmd wrote this:
"NMAKE: fatal error U1073: don't know how to make 'C:\program'
Stop."
and so on.
(I guess it probably wanted to nmake some file in program files, but i don't know which and why ...)

Thanks

KjellKod said...

Libor,

almost impossible to help you without seeing the full printout of the error.

You can e-mail me this and I will take a look, I can also recommend the Qt forums (qtcentre.org) since your question will then be exposed to many people that may be able to help you.

Anonymous said...

KjellKod,

Thank you for your blog!!! I could not have built Qt without you.

Now could you help me with one last thing?

I followed your instructions for setting up VC2005 EE and could get the intellisense working, but when I tried to build a very simple project with a QString in it, I got a link error "unresolved external symbol" on QString.

I double checked that I have $(QTDIR)\lib in Tools->Options->Library files.

Thank you for your help.

JJ

KjellKod said...

Hi JJ.

Have you double checked all the steps? It's easy to miss something which could screw it up.

It sounds like VS doesn't find your files correctly (during linking), maybe something to do with Qt's pre-processor (MOC)?

You could always send me the code that you're trying to test. And I can try to figure it out

Anonymous said...

KjellKod,

Well I tried to keep it very simple:
#include "stdafx.h"

#include <QString>

int _tmain(int argc, _TCHAR* argv[])

{

QString qString;

return 0;

}

I don't get any compile errors so I know that the include file for QString is found OK in $(QTDIR)\include\QtCore. I can see the QtCore4.dll, along with its .exp, . dll.manifest, .lib, ,prl, .pdb, in my $(QTDIR)\lib which I have verified is listed in the tools->options->VC++ Directories->Library files.

I assume I don't need to use make or fool with a Makefile or anything.

Thank you for your help.

JJ

KjellKod said...

JJ - send me your e-mail address in a post. I won't publish it on the blog. That way we can communicate without messing up the blog space.

/ Kjell (a.k.a. KjellKod)

KjellKod said...

JJ - I added the solution to be part of the instruction of how to set up Qt - so now there's a simple Qt Hello World to try for newbies

Unknown said...

A couple questions about your "Hello World" example...I think you have some typos, shouldn't it read:

Build Command Line: qmake && nmake debug
Rebuild Command Line: qmake && nmake debug
Clean Command Line: qmake && nmake debug-clean

also, can't you prepend "qmake -project &&" to the beginning of each of these lines so that your .pro file is self maintaining?

I've found these solutions through the command line. I'm actually having problems with this section of your instructions. When I go to "project->properties->configuration properties", I don't have a "nmake" option. Any idea why?

KjellKod said...

Hi Josh. Thanks for your comments I have updated the example.

Yes you can certainly do this to automatically update the .pro file automatically. However for larger projects I prefer personally to edit the pro file manually after awhile.

If you miss the 'NMAKE' option then you didn't created a makefile project according to the instructions. I.e. "File->New Project->Visual C++ ->General->Makefile Project"

I've tested it on VC++ and VC++ Express and it's the same on both.

Unknown said...

Hi

First of all...it is great thing that this kind of instructions can be found from Internet!

Now...my problem is that when I try to build this example project, the executions stops right a way and say:

"'qmake' is not recognized as an internal or external command,
operable program or batch file."

I don't understand! if I type "qmake" in cmd, it works (i.e. its in PATH and it's working). I have ran vsvars32 several times to ensure that everything is OK. Also environment variables should be ok:

%QTDIR%: C:\QT\4.2.3
%QMAKESPEC%: %QTDIR%\mkspecs\win32-msvc.net

So I'm using older, VS.net 2003. Is there some major differences regarding actions what should be done to achieve same as with 2005.

KjellKod said...

Hi Norrit.

I have not tried it with VS 2003, but I have tried it with VS 2005 and VS 2005 Express. Both of which worked fine. It DOES sound strange that it works with the environment variables sometimes and sometimes not.

I would first of all check with Qt - do they support it for VS 2003? Then go through all the installation instructions again and verify every step *boring, but necessary*

If you still cannot find it then write me a comment again with your email address for direct help. I won't publish your address - but since your blogger profile isn't "open" I cannot contact you directly as things stand now.

If you solve the problem w/out me then that is of course interesting to know too ;-)

Unknown said...

Thanks for replying KjellKod!

Okay...I didn't have energy to fight with that strange problem, so I just installed vs C++ 2005 express. After some struggle, I managed to get everything to work.

One thing that wasn't mentioned here I must now say. If you install SP1 to VS, then problems will occure in QT compilation. The workaround for this exists and you can start looking it from here:

http://trolltech.com/company/newsroom/announcements/press.2006-12-18.5819317947

Unfortunately I can't remember the error message anymore (just for better googling for other having same problem).

Rayudu said...

Hi KjellKod,

I am very much thankful to you as i could install Qt for Visual Studio2005 but i have one doubt.What is this QuickFIX and what is its functionality with Qt and Visual Studio?How to compile it?The page you directed in your blog is confusing.What to do with this QuickFIX?How to proceed with this?Does it have any influence on Qt or c++ projcets?

KjellKod said...

Hi "thirupathi".

No QuickFIX has nothing at all to do with this HOWTO or with Qt. Ignore it!

(It just happened to be what I was working with when I used VC++ 2005 the first time)

KjellKod said...

"thirupathi" ... and all other people that send me requests to help them fix debugging problems

1) Try first my simple
"Create your first project - A simple Hello World in Qt", or something else REALLY simple

2) If that doesn't work and you cannot find help at QtCentre.org or in the forums and documentation at www.trolltech.com then by all means drop me an e-mail

NOTE HOWEVER that if you need help with something you MUST put in your email or other www contact information since I will NOT use this blog to communicate all questions of more or less relavence.

Rest assurce, I will not publish posts that contain your contact information; but I will read them and get back to you.

Anonymous said...

Hi Kjell,

I use Qt4.3.3 and acs-4.3.x-patch3 as the patch file. But I have problem in step 6 running installpatch43.bat!

Patching some files, it shows something like 'Hunk #1 FAILED AT 401' and similar messages with different numbers. But anyway it passes over and finishes. Then in step 7, just before 'Going to compile qmake now', it says 'perl.exe' was not found and compiling qmake crashes too.

Would you please help me?
Thanks.

KjellKod said...

Hi,

I haven't tested with the latest version of Qt. BUT I believe that it may not be necessary to use the patch for most platforms.

Try from the beginning again, but skip the Qt patch part and see how it goes. If it doesn't work then please get back to me (& if it works too!)

/ KjellKod

PS: Please please please if you get back to me then post your email address so I can contact you directly. This forum isn't suitable for this kind of help.

I will not post your follow up post with e-mail address!

Anonymous said...

Well said.

Anonymous said...

Using this post, I've compiled some instructions to integrate Qt 4.5.0 with Visual Studio C++ 2008 Express Edition

http://sites.google.com/site/seatibizasporttdi130/Home/software/

I hope this can help you!

Stefano

KjellKod said...

Thank you :) I guess you mean my homepage www.kjellkod.cc? since I only very rarely update my blog