Showing posts with label Qt. Show all posts
Showing posts with label Qt. Show all posts

2012-06-18

Portable qmake

According to Wikipedia qmake "is an utility that automates the generation of Makefiles [...] It integrates with the Qt framework"

You can also use it without Qt though, this is achieved by adding the following line CONFIG -= qt in the *.pro file.

In order to use qmake in a portable way one needs to:
  1. Copy the qmake executable, which is statically linked, into a folder named bin
  2. Create a qt.conf file near to qmake.exe containing just the following:
  [Paths]
  HostPrefix=..
  3. Copy the mkspecs folder

I have copied all the above in C:\Program Files (x86)\qmake-2.01a\ and put a qmake.cmd in a bin folder which I have in path. Zipping the above folder generated an qmake-2.01a.zip which is just 996 KB in size.

qmake.cmd is nothing more than just: @"c:\Program Files (x86)\qmake-2.01a\bin\qmake.exe" %*

qmake isn't just an utility that automates the generation of Makefiles it can also generate Visual C++ project files. This is as easy as running: qmake -tp vc

You can find all that qmake can do in the qmake Manual.

You can also use qmake without knowing too much about it, it can generate a project (pro) file from existing code.

Let's say you have this hello.cpp

#include <windows.h>

int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
     ::MessageBox(0, L"qmake works!", L"info", MB_OK | MB_ICONINFORMATION);
     
     return 0;
}
 
  1. Put this hello.cpp in a test folder
  2. Type qmake -project, which will generate a test.pro file
  3. Edit test.pro and add CONFIG -= qt
  4. Type qmake -tp vc, which will generate a Visual C++ project file

There you have it, starting from a cpp file and ending up with a Visual C++ project file in three easy steps!

By running release\test.exe on a Windows 7 you will see the following:

The release\test.exe has 7KB. Let's say I want to deploy this executable on other machines, do I have some dependencies? By using dumpbin we can answer this question.

$ dumpbin /imports test.exe | findstr ".dll"
    USER32.dll
    MSVCR90.dll                   96 __dllonexit
    KERNEL32.dll

How do we get rid of the MSVCR90.dll dependency? It seems this is as complicated as editing a text file, namely qmake-2.01a\mkspecs\win32-msvc2008\qmake.conf.  And there change -MD and -MDd into -MT and -MTd. Now release\test.exe has increased to 41KB but there is no MSVCR90.dll dependency.

If you don't feel comfortable with editing this "system" file, you can copy it and then include the modified qmake.conf file in your pro file. Or you can use the "-spec" command line parameter to point qmake to the modified qmake.conf file.

As you can see qmake is very flexible and it can accommodate to a large number of compilers, just have a look in mkspecs folder.

If you like qmake, you will also like jom - the parallel make tool for Windows, which can be obtained from here.

2011-07-29

Starting development with Qt on Windows

The general advice when it comes to start develop using Qt on Windows is to download the Qt SDK. The offline version has 1.7 Gbytes and you can start developing for Desktop (using MinGW or Visual C++ 2008 Express or higher), for Symbian ^1 or ^3 devices, or for MeeGo devices.

People usually complain about the size of the offline Qt SDK. In comparison Visual Studio 2008 Standard Edition (x86) - DVD (English) has 2,8 Gbytes.

Even though the offline Qt SDK has 1.7 GBytes, it doesn't include everything. The precompiled Qt demos are not included in Qt SDK. They are available though through the individual Qt Library installers, or more explicitly Qt libraries 4.7.3 for Windows (VS 2008, 228 MB).

After installation one can use Qt Creator to develop applications. I won't go into much detail here, one can browse through the official documentation - Qt Creator Manual 2.2.1

I'm going to describe how you can compile from command line programs for Desktop - MinGW, Visual C++ 2008, and for Symbian^3.

First we need to get a small "Hello World" example. Google pointed out at Qt 4.3: Qt Tutorial 1.
We need to start up a text editor and paste:
#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QPushButton hello("Hello world!");
    hello.resize(100, 30);

    hello.show();
    return app.exec();
}

From Start Menu -> All Programs -> Qt SDK -> Desktop you can start the command prompt for "Qt 4.7.3 for Desktop (MinGW)". Unfortunately it lands into c:\windows\system32

Since I already use Total Commander for my file browsing I've decided to copy the properties from the Qt Command Prompts into Total Commander's Start Menu.

If we do not specify a "Start path" for an entry in Total Commander's Start Menu, Total Commander will use the current directory as start path, which together with a hot key will free me from useless mouse clicking and of some typing.

"Qt 4.7.3 for Desktop (MinGW)" is a shortcut for "C:\Windows\System32\cmd.exe /A /Q /K C:\QtSDK\Desktop\Qt\4.7.3\mingw\bin\qtenv2.bat". Copying the command line into Total Commander's Start Menu looses one functionality - the title of the command window becomes "c:\windows\system32\cmd.exe" compared with the Qt SDK's "Qt 4.7.3 for Desktop (MinGW)". This can be easily fixed by appending "title Qt 4.7.3 for Desktop (MinGW)" into "C:\QtSDK\Desktop\Qt\4.7.3\mingw\bin\qtenv2.bat".

Now that we have everything in place we can type into "Qt 4.7.3 for Desktop (MinGW)" command window the following commands (in the directory where we saved the "Hello World" source code):
qmake -project
qmake
mingw32-make

The result is presented bellow:

I've done the same for "Qt 4.7.3 for Desktop (MSVC 2008)". The commands to build the "Hello World" application are almost the same, but instead of mingw32-make, nmake is to be used with MSVC 2008.

But after running nmake the following message was displayed:
'nmake' is not recognized as an internal or external command, operable program or batch file.

This is due to the fact that "C:\QtSDK\Desktop\Qt\4.7.3\msvc2008\bin\qtenv2.bat" does not setup the MSVC 2008 compiler. In order to fix this append the lines to the qtenv2.bat file:
call "%programfiles%\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
title Qt 4.7.3 for Desktop (MSVC 2008)

For Symbian^3 instead of making a shortcut to cmd.exe and pass the batch file as argument, there is a shortcut to the batch file: "C:\QtSDK\Symbian\SDKs\Symbian3Qt473\bin\qtenvS3.bat", which in turn makes a call to cmd.exe. Exactly the opposite. Edit qtenvS3.bat and replace the last line "cmd /A /Q /K" with the lines:
popd
title Qt 4.7.3 for Symbian^3 Command Prompt

Compiling the "Hello World" sample for Symbian^3 goes like this (helloqt.cpp needs to be on the same drive where Qt SDK was installed, e.g. c:\):
qmake -project
qmake
make release-gcce
make sis

After installing helloqt.sis on device, the result looks like this (admittedly not a very good looking screen shot):


Compiling Qt applications from command prompt is important in cases in which we have to deal with configure scripts, or when we want to do things "old school" :)

I've opened up a bug report QTSDK-802 with the issues presented in this blog entry.