Forcing a re-link in complex Qt builds

Got a Qt project with one or more sub-projects compiled as static libraries? (i.e. you have a top level project with several subdirs, one of which is the app, the others being libraries the app depends on) If so, you’ve probably noticed you either need to do a Rebuild-All in Qt Creator, or make an innocuous modification somewhere in the app project, to see changes made in the static libraries appear in the compiled application. If not, then that last sentence made absolutely no sense to you. Sorry.

In my particular case, I was setting up a project in such a way as to allow unit testing. The recommended way to do this (which I’ll go into greater depth with in a later post), is to have the bulk of your application code compiled into a static library, leaving main.cpp to be compiled in it’s own project as an app linking to the library. When starting a build in Qt Creator, it won’t re-link the static library, meaning changes to the code won’t appear in the built application. When some of your code is in it’s own static library, this can be annoying. When you’ve set up your project as above, it’s maddening.

There’s an easy fix for this, thankfully. In your app project file, use the PRE_TARGETDEPS qmake variable, and point it at the static library. The path will be similar to the LIBS variable, except it will include the library file name as well (e.g. ../src/lib/lib.a, rather than just ../src/lib/). This will force re-linking of those libraries on every build, resulting in decreased hair-pulling crazy-making.