The power of qmake

Published Wednesday April 16th, 2008 | by

Having been a support engineer for over seven years now at Trolltech, I’ve seen my fair share of inquiries about qmake and since Qt 4.x, qmake has become quite powerful and useful. There are a couple of lesser known variables that can be used which can make integrating your own compilers and targets into a makefile a lot easier to achieve.

Let’s say you want to have your own pre-processor to run over some files before building occurs. This is where you can use QMAKE_EXTRA_COMPILERS. In this case, all our preprocessor does is rename the file from .cxx to .cpp.

    PREPROCESS_FILES = preprocess.cxx foobar.cxx more.cxx
    preprocess.name = Renaming .cxx files to .cpp files
    preprocess.input = PREPROCESS_FILES
    preprocess.output = ${QMAKE_FILE_BASE}.cpp
    preprocess.commands = ren ${QMAKE_FILE_IN} ${QMAKE_FILE_BASE}.cpp
    QMAKE_EXTRA_COMPILERS += preprocess

Doing qmake followed by make will show that it is renaming the files, however on inspection of the makefile it will show that its going to consider these files as objects. Since they are still C++ source code files we want to be able to have them compiled first. With the addition of the line:

    preprocess.variable_out = SOURCES

then it indicates that the output should be added to the SOURCES variable which qmake already knows how to handle. In more advanced use cases we could always indicate that the output shouldn’t be put in with the objects because it is used for other reasons and that can be done by adding:

    preprocess.CONFIG += no_link

The other lesser known variable is QMAKE_EXTRA_TARGETS, this is useful when you want to be able to add a custom build target to your Makefile. For example, if you wanted to have a target to update your qm file for your application. Then you would have something like:

    updateqm.commands = lrelease myapp_no.ts
    updateqm.target = updateqm
    QMAKE_EXTRA_TARGETS += updateqm

It is as simple as that, then you can cause it to be invoked by:

    make updateqm

If you have more than one configuration (as is the default on Windows) then if you want to have it inside the configuration specific makefiles (i.e. Makefile.Debug and Makefile.Release) then simply add:

    updateqm.CONFIG += recursive

then you can do:

    make -f Makefile.Debug updateqm

The documentation for these variables is unfortunately minimal for Qt 4.3 and Qt 4.4.0. However, if you check out the snapshots after it is updated tonight then you will see some in-depth documentation on what is available for both these variables.

Did you like this? Share it:
Bookmark and Share

Posted in Qt

6 comments to The power of qmake

Adrien de Sentenac says:

That’s great new, qmake is a very useful tool, but it was really lacking some serious documentation :D

JarJarThomas says:

Do not missunderstand me … i like commandline tools because of their flexibility.

But in reality i never use them at least not with all their power.
Why ?

Because i have no time to memorize thousands of commandline tools.
Commandline tools have only one problem … they are really terrible to learn.
On one window the help, on the other one a large textfile with x values in it. Having a typo here, having a wrong order there … and you need more time setting up the tool than it would have taken you to create a new one especially for your task.
And if you do not need to do this the whole day, the next time you have the same problems.

I really liked as an example the old QT installer where you could modify your build very easily without reading one hour to find out how which flag works. Why is it gone with qt 4 ?
It’s also the same with qmake/nmake/make … i never used it in all it’s power (although i’m sure there are some good things in it) because it takes to much time to use it.
I just created one makefile once … and copies it and try to update it.

In all the years i’ve written code i found 90% developers (on unix systems) which just copy an old makefile … take a part from here, another one from there … and in best case 10% really can use it.
But if they change something it’s useless for all the others because it takes to much time to find out the changes.

I don’t like the way of visual studio either (that’s mostly because their ui is really crappy and not really usable).
Why not an intelligent makefile wizzard.
One that helps you getting your code compiled, helping you editing your makefile and help millions of developers to have a better build environment ?

sorry … just had to write because i needed to do a manual build of qt with all the funny things like compile errors becaues of missing paths, wrong configurations, or just three times recompile and then it goes and so on.
Cost me about one day … unpaid.

Andreas says:

Hi, Thomas. Yeah, IDEs today don’t really have excellent build setup UIs. Wizards are great, but only in the start of a project. What we really need is an intuitive UI for maintaining a complex build. I don’t think any IDE has managed to do this so far.

qmake is about simplicity and capability, and yes, it’s a command line tool. A great one! :) . The huge group of developers who revere IDEs and prefer editors and consoles just love this tool. And I know plenty of IDE users who love it too. I personally think qmake is beautiful in its simplicity and its capabilities. But of course, as soon as you go advanced, it becomes hard to maintain just like the other build tools. Sometimes the solution is to simplify your build, sometimes it’s about using a different build tool (KDE uses cmake, for example).

Maybe we’ll come up with a good build editor in the future. I would really love to see that…

Adam Higerd says:

I’m a longtime fan of qmake. For simple projects it’s a LOT easier than writing makefiles from scratch, and for advanced projects there’s still a ton it can pull off. Qxt uses qmake, with a shell script to offer ./configure, and it can automatically handle almost any configuration without requiring the user to edit a build file or pass arcane options to configure (they’re just there mostly for the sake of package maintainers or people with unusual setups). And qmake’s extensibility is really nice, too; Qxt, once installed, plugs itself in and adding Qxt modules to your project is as easy as adding Qt modules. People tend to underestimate qmake. They shouldn’t.

wysota says:

Hi,

if you don’t know about a great source of qmake tips and docs, take a look here:
http://wiki.qtcentre.org/index.php?title=Undocumented_qmake

The article probably needs some cleanup but even in the current state can be very useful.

Remember – Trolls can edit the wiki too, so feel free to do it :)

Dave T says:

I assume that QMAKE_EXTRA_TARGETS does not work with Mac Xcode projects? Or at least if it does I haven’t figured out a way to get it to work. I tried just the following simple addition to our pro file:

TestTarget.target = TestTarget
TestTarget.commands = open /Applications/TextEdit.app/
QMAKE_EXTRA_TARGETS += TestTarget

And I don’t see any extra Targets in Xcode, nor is the TextEdit application opened after I compile. It sure would be nice if stuff like this worked with Xcode projects. It’s so very frustrating to try an use many of qmake’s documented features only to find out that they don’t work with Xcode projects (INSTALLS and QMAKE_EXTRA_TARGETS just to name a couple). If I just create a Makefile then it appears to work, but Xcode integration is what I’m interested in.

Commenting closed.