Friday, June 20, 2014

Tips and Tricks: Configuration depending on the compilation type when using qmake

Hello!

One of the things almost all projects that use Qt with qmake will end up having is alterations to the .pro file. Depending on the necessities of the project, many of these alterations will have to be different depending on wheter the user is doing a debug or a release compilation. Lets have a look into this.

Default: Scopes

If one have a look in the Qt Assistant files, this "configuration given a certain kind of compilation" should be done by scopes. The "scope mechanism" consists of declaring which king of compilation you're talking about (either "release" or "debug") together with the configuration data inside a group created with the {} symbols. Example:

release {
    LIBS += ...
    }

debug {
    LIBS += ...
    }

A better way

Theorically the scopes method works fine and it should do the trick. The problem, though, is that, by default, a project created in Qt Creator with qmake comes with some configurations to qmake that usually includes both release and debug situations. In other words, normally the scopes mechanism will fail. 

This problem have two known solutions: either you change the default configurations or you change the method used for specific configuration in the .pro file. The last option (that, at the end, should be used either way) consists of using the CONFIG directive in the .pro file this way:

CONFIG(debug, debug|release) {
    LIBS += ...
    }

CONFIG(release, debug|release) {
    LIBS += ...
    }

In the first code, the configuration is related to the debug compilation; in the second, is related to a release compilation. Put your code inside and everything will work without having to worry about the default configurations.

Conclusion

In this small article we saw a better way of writting configuration parameters in the .pro file of one's project using the CONFIG qmake directive instead of the default "scopes method"; an alternative that should be preferred to avoid having to unnecessarily change qmake's default configurations when a new project is created.

God bless you! Have a nice day!

References