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.