Wednesday, February 9, 2011

Windows + Netbeans + MinGW + Qt


NetBeans is an excellent IDE developed primarily for Java. For several years now it has also offered a C++ plugin which by using GNU compilers is able to turn it into a C++ IDE. In addition there is also the possibility of integrating Nokia's Qt development suite into NetBeans for GUI's and other visually rich apps. However getting these three components to work together under Windows is very tricky. I've done it several times now on several systems and each time I have to go and trawl through forums to get it all functioning together. So I decided to blog it all here mostly for my own use but also to try and bring all this info together in one place for others. I have followed this procedure successfully on Windows XP, Vista and 7

Download stuff:
First gather your materials. You will need to download:
Java Development Kit 6: Do this from the Oracle website You can download a version with Netbeans 6.9.1 bundled:

Next you need to get the Qt SDK from Nokia. I choose the LGPL version (that's the free one) but there's a commercial one depending on your license requirements. Here you should download
the complete QtSDK for windows - this includes a copy of MinGW 4.4 which has the C++ compilers - more about this later. It also includes Qt Designer which will enable you to do visual development of your Qt forms in Netbeans.

Finally you need MSYS version 1.0.11 (What's that? I'll tell you later). Get it here:
Do not be tempted to follow any of the instructions on that page! We're going to get MinGW from the Qt installation. Okay, so here is the installation order:

Installing Qt/MinGW

First, run the Qt installer. When given the option you should install MinGW as well as Qt and Qt Creator. When the install is finished uncheck run Qt Creator.

Now you should install MSYS. If you don't already know: Although NetBeans IDE has a C++ plugin, all it does is display C++ files prettily and help you navigate and write your code in an easy manner. It does not contain a compiler or linker to actually produce an executable from your code. When we installed Qt we also installed MinGW which is a "Minimal GNU for Windows". GNU is a set of open source compilers and development tools developed for UNIX/Linux systems. MinGW is a Windows port of them - however to work properly they need some UNIX stuff which comes from MSYS - which is a Minimal SYStem for windows - essentially a collection of ported UNIX tools and utilities. Most importantly it contains a version of "make" - the version which is in MinGW is (for some arcane reason I don't understand) incompatible with NetBeans. So - do the MSYS install. Once it has finished it will pop up with a command window telling you about a post-install. You need to do this!

Hit 'y' for yes you want to do the post install, the 'y' for do you have MinGW installed. Then you need to enter where it is installed - go to explorer and have a hunt around in the Qt directory. If you didn't change anything during the Qt installation you should have something like: C:\Qt\2010.05\mingw. You should enter that but using / slashes instead of \ . MSYS then does a bunch of stuff and makes some sarcastic comment about MinGW's version of make. MSYS installation is now complete.

Installing Netbeans / JDK

Now (finally!) you can install Netbeans / JDK. Run the installer you got from Oracle and then - once you answered the default questions go and have a coffee - it's going to be a while! Once it is done you should start NetBeans. The first time you start it, it will almost certainly tell you to update some part of itself via a little icon in the status bar. I usually go ahead and let it do that. This usually requires a restart of the program and usually causes it to want to update something else - again I let it do this until it stops asking me to update things!

Installing C/C++ plugin

As it is, the Netbeans you have installed is purely a Java development environment - we want to get C++ functionality so we need to install the C/C++ plugin. Go to Tools>>Plugins in Netbeans main menu and switch to the available plugins tab. Check the C/C++ plugin (along with any other plugins you fancy!).


Click the Install button. You may have to restart the IDE again.

Configuring C/C++ plugin

In order to get the C++ plugin working you need to tell it where the compilers are. To do this close down NetBeans. Add to your system PATH variable the directories for:
MSYS (c:\msys\1.0\bin)
MingGW (c:\Qt\2010.05\mingw\bin)
Qt (c:\Qt\2010.05\qt\bin)
You will have to check these paths are appropriate for your system. They should be the directories that contain, make.exe, g++.exe and qmake.exe respectively.

Now restart Netbeans and go to Tools>>Options and switch to the C++ tab. Netbeans should now automatically detect your GNU compilers and Qt. If it does not you may need to use "add..." to set up the tool set manually (setting the location of g++.exe as the base directory).

So we're there now yes? WRONG! You can now start a Qt/C++ project okay but there are a couple more steps. Go to the Code Assistance tab in the options Tools>>Options dialog again and within the C++ compiler tab (yes tabs in tabs in tabs!) add the include directory for the Qt core header files (in my case this C:\2010.05\qt\include).

Correcting your qmake specs

You should now be up and running. Probably restart the IDE again here. But then start a new Qt project (under new project dialog choose C/C++ project >> C/C++ Qt Application). If you just build the auto-generated code you should find everything is okay. Likewise if you add any non-Qt code. However there are a few problems. Try to create a new Qt form (choose to create a C++ wrapper class when you do this) in your project and you'll see what I mean - first of all it will open Qt Designer so you can visually design your form - now exit and you will find your C++ wrapper for the form is full of squiggly red lines. This is okay - you need to compile once for the Qt pre-processor to generate all it's magical behind the scenes stuff. But it still wont build - you'll get some weird message like this:
make[2]: c:/Qt/2010.05/qt/binuic.exe: Command not found
Sure enough if you check out your Qt directory you will find there is indeed no binuic.exe , like me you will probably spend some time searching for this exe to find it is nowhere on your disk, then start trawling the internet to see if you can download it from somewhere - it doesn't - there is no such thing. There is however a file called uic.exe in C:/Qt/2010.05/qt/bin the confusion comes about in your "qmake specs".
This is a file which tells qmake (the Qt pre-processor) how to build your Qt project. There is a / vs. \ slash confusion caused by Windows and other OS systems using different slashes as directory separators. Basically all you have to do is navigate to C:/Qt/2010.05/qt/mkspecs/win32-g++ (again the first part of this path - the 2010.05 may be different for you) . There you will find a file called qmake.conf edit it in notepad or whatever and find this section near the bottom:

QMAKE_MOC = $[QT_INSTALL_BINS]${DIR_SEPARATOR}moc.exe
QMAKE_UIC = $[QT_INSTALL_BINS]${DIR_SEPARATOR}uic.exe
QMAKE_IDC = $[QT_INSTALL_BINS]${DIR_SEPARATOR}idc.exe

I have found the easiest thing to do is to just rewrite these three lines as:

QMAKE_MOC = $$[QT_INSTALL_BINS]/moc.exe
QMAKE_UIC = $$[QT_INSTALL_BINS]/uic.exe
QMAKE_IDC = $$[QT_INSTALL_BINS]/idc.exe

And save qmake.conf (you may want to make copy of the old version first) DO NOT diddle with any other part of this file unless you know what you're doing!

Configuring yout Qt/C++ project

You're environment is now completely set up for Qt/C++ development in Netbeans. However, when you start a new project there are a couple of project settings you need to ensure you set.
Start a new Qt/C++ project and once it is created right-click on the project node and choose "properties".
You will get a property tree. First thing is to make sure under the "Build" branch you have the toolset selected that you set up under Configuring the C++ plugin. Next go to "Build/Qt". Right at the bottom under "Expert" you'll see Qmake spec. On that line enter "win32-g++" The qmake spec we just modified.
In the line immediately bellow (Custom Definitions) I advise you enter CONFIG+=console. This is optional but without it things like cout, printf and QDebug will not produce any output - not a problem if you are going 100% GUI but can be a pain while debugging.
If you do want console output you will also need to set Console type to "Output window" under the Run branch of the tree.

And that should do it! Let me know if any of this doesn't work. Next time I may write some tips on getting the QWT scientific plotting library to work under this set up too.

:)